Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RoutingExtension: added support for custom single-route classes. #162

Merged
merged 1 commit into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 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,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') {
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]);
});