Skip to content

Commit

Permalink
RoutingExtension: added support for custom single-route classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastavesely committed May 17, 2017
1 parent f0ae586 commit 1ee81ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/Bridges/ApplicationDI/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class RoutingExtension extends Nette\DI\CompilerExtension
public $defaults = [
'debugger' => NULL,
'routes' => [], // of [mask => action]
'routeClass' => NULL,
'cache' => FALSE,
];

/** @var bool */
private $debugMode;


public function __construct($debugMode = FALSE)
public function __construct(bool $debugMode = FALSE)
{
$this->defaults['debugger'] = interface_exists(Tracy\IBarPanel::class);
$this->debugMode = $debugMode;
Expand All @@ -42,10 +43,6 @@ 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]);
}

if ($this->name === 'routing') {
$builder->addAlias('router', $this->prefix('router'));
}
Expand All @@ -55,6 +52,13 @@ public function loadConfiguration()
public function beforeCompile()
{
$builder = $this->getContainerBuilder();
$router = $builder->getDefinition($this->prefix('router'));

$class = $this->config['routeClass'] ?: 'Nette\Application\Routers\Route';

foreach ($this->config['routes'] as $mask => $action) {
$router->addSetup('$service[] = new ' . $class . '(?, ?)', [$mask, $action]);
}

if ($this->debugMode && $this->config['debugger'] && $application = $builder->getByType(Nette\Application\Application::class)) {
$builder->getDefinition($application)->addSetup('@Tracy\Bar::addPanel', [
Expand Down
34 changes: 32 additions & 2 deletions tests/Bridges.DI/RoutingExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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('
Expand All @@ -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/<id>', $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/<id>: 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]);
});

0 comments on commit 1ee81ed

Please sign in to comment.