From 05bd3239aea0bd153204cb6ad2e1d18ce041032c Mon Sep 17 00:00:00 2001 From: vlastavesely Date: Mon, 3 Oct 2016 15:00:00 +0200 Subject: [PATCH] RoutingExtension: added support for custom single-route classes. --- .../ApplicationDI/RoutingExtension.php | 9 +++-- tests/Bridges.DI/RoutingExtension.basic.phpt | 34 +++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Bridges/ApplicationDI/RoutingExtension.php b/src/Bridges/ApplicationDI/RoutingExtension.php index 17de9d0dd..91d552dde 100644 --- a/src/Bridges/ApplicationDI/RoutingExtension.php +++ b/src/Bridges/ApplicationDI/RoutingExtension.php @@ -19,6 +19,7 @@ class RoutingExtension extends Nette\DI\CompilerExtension public $defaults = [ 'debugger' => NULL, 'routes' => [], // of [mask => action] + 'routeClass' => NULL, 'cache' => FALSE, ]; @@ -26,7 +27,7 @@ class RoutingExtension extends Nette\DI\CompilerExtension private $debugMode; - public function __construct($debugMode = FALSE) + public function __construct(bool $debugMode = FALSE) { $this->defaults['debugger'] = interface_exists(Tracy\IBarPanel::class); $this->debugMode = $debugMode; @@ -42,8 +43,10 @@ public function loadConfiguration() ->setClass(Nette\Application\IRouter::class) ->setFactory(Nette\Application\Routers\RouteList::class); - foreach ($config['routes'] as $mask => $action) { - $router->addSetup('$service[] = new Nette\Application\Routers\Route(?, ?);', [$mask, $action]); + $routeClass = $this->config['routeClass'] ?: 'Nette\Application\Routers\Route'; + + foreach ($this->config['routes'] as $mask => $action) { + $router->addSetup('$service[] = new ' . $routeClass . '(?, ?)', [$mask, $action]); } if ($this->name === 'routing') { diff --git a/tests/Bridges.DI/RoutingExtension.basic.phpt b/tests/Bridges.DI/RoutingExtension.basic.phpt index a1571e9d7..1f1717cc4 100644 --- a/tests/Bridges.DI/RoutingExtension.basic.phpt +++ b/tests/Bridges.DI/RoutingExtension.basic.phpt @@ -12,6 +12,10 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; +class Route extends Nette\Application\Routers\Route +{} + + test(function () { $loader = new DI\Config\Loader; $config = $loader->load(Tester\FileMock::create(' @@ -23,13 +27,39 @@ test(function () { $compiler = new DI\Compiler; $compiler->addExtension('routing', new RoutingExtension(FALSE)); - $code = $compiler->addConfig($config)->compile(); + $code = $compiler->addConfig($config)->setClassName('Container_basic')->compile(); eval($code); - $container = new Container; + $container = new Container_basic; $router = $container->getService('router'); Assert::type(Nette\Application\Routers\RouteList::class, $router); Assert::count(2, $router); Assert::same('index.php', $router[0]->getMask()); Assert::same('item/', $router[1]->getMask()); + + Assert::type(Nette\Application\Routers\RouteList::class, $router); + Assert::type(Nette\Application\Routers\Route::class, $router[0]); +}); + + +test(function () { + $loader = new DI\Config\Loader; + $config = $loader->load(Tester\FileMock::create(' + routing: + routeClass: + Route + routes: + item/: Homepage:detail + ', 'neon')); + + $compiler = new DI\Compiler; + $compiler->addExtension('routing', new RoutingExtension(FALSE)); + $code = $compiler->addConfig($config)->setClassName('Container_customRoute')->compile(); + eval($code); + + $container = new Container_customRoute; + $router = $container->getService('router'); + + Assert::type(Nette\Application\Routers\RouteList::class, $router); + Assert::type(Route::class, $router[0]); });