Skip to content

Commit

Permalink
Do not modify current router, wrap it instead
Browse files Browse the repository at this point in the history
[Closes #38]
  • Loading branch information
Andrewsville authored and fprochazka committed May 1, 2016
1 parent 0c752d1 commit 5fddef9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
30 changes: 0 additions & 30 deletions src/Kdyby/Console/CliRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,4 @@ public function constructUrl(Request $appRequest, Nette\Http\Url $refUrl)
return NULL;
}



/**
* @param \Nette\Application\IRouter $router
* @param CliRouter $cliRouter
* @throws \Nette\Utils\AssertionException
* @return \Nette\Application\Routers\RouteList
*/
public static function prependTo(Nette\Application\IRouter &$router, self $cliRouter)
{
if (!$router instanceof RouteList || $router->getModule()) {
throw new Nette\Utils\AssertionException(
'If you want to use Kdyby/Console then your main router ' .
'must be an instance of Nette\Application\Routers\RouteList without module'
);
}

$router[] = $cliRouter; // need to increase the array size

$lastKey = count($router) - 1;
foreach ($router as $i => $route) {
if ($i === $lastKey) {
break;
}
$router[$i + 1] = $route;
}

$router[0] = $cliRouter;
}

}
12 changes: 10 additions & 2 deletions src/Kdyby/Console/DI/ConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,16 @@ protected function beforeCompileHookApplication(array $config)
'}', [FALSE, 254, $this->prefix('application')]);
}

$builder->getDefinition($builder->getByType('Nette\Application\IRouter') ?: 'router')
->addSetup('Kdyby\Console\CliRouter::prependTo($service, ?)', [$this->prefix('@router')]);
$routerServiceName = $builder->getByType('Nette\Application\IRouter') ?: 'router';
$builder->addDefinition($this->prefix('originalRouter'), $builder->getDefinition($routerServiceName))
->setAutowired(FALSE);

$builder->removeDefinition($routerServiceName);

$builder->addDefinition($routerServiceName)
->setClass('Nette\Application\Routers\RouteList')
->addSetup('offsetSet', [NULL, $this->prefix('@router')])
->addSetup('offsetSet', [NULL, $this->prefix('@originalRouter')]);

$builder->getDefinition($builder->getByType('Nette\Application\IPresenterFactory') ?: 'nette.presenterFactory')
->addSetup('if (method_exists($service, ?)) { $service->setMapping([? => ?]); } ' .
Expand Down

0 comments on commit 5fddef9

Please sign in to comment.