Skip to content

Commit

Permalink
RouteList: added warmupCache()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 6, 2016
1 parent c292b85 commit bd834c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
40 changes: 24 additions & 16 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,7 @@ public function match(Nette\Http\IRequest $httpRequest)
public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl)
{
if ($this->cachedRoutes === NULL) {
$routes = array();
$routes['*'] = array();

foreach ($this as $route) {
$presenters = $route instanceof Route && is_array($tmp = $route->getTargetPresenters())
? $tmp : array_keys($routes);

foreach ($presenters as $presenter) {
if (!isset($routes[$presenter])) {
$routes[$presenter] = $routes['*'];
}
$routes[$presenter][] = $route;
}
}

$this->cachedRoutes = $routes;
$this->warmupCache();
}

if ($this->module) {
Expand Down Expand Up @@ -98,6 +83,29 @@ public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\U
}


/** @internal */
public function warmupCache()
{
$routes = array();
$routes['*'] = array();

foreach ($this as $route) {
$presenters = $route instanceof Route && is_array($tmp = $route->getTargetPresenters())
? $tmp
: array_keys($routes);

foreach ($presenters as $presenter) {
if (!isset($routes[$presenter])) {
$routes[$presenter] = $routes['*'];
}
$routes[$presenter][] = $route;
}
}

$this->cachedRoutes = $routes;
}


/**
* Adds the router.
* @param mixed
Expand Down
8 changes: 6 additions & 2 deletions src/Bridges/ApplicationDI/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
if (!empty($this->config['cache'])) {
$method = $class->getMethod(Nette\DI\Container::getMethodName($this->prefix('router')));
try {
$router = serialize(eval($method->getBody()));
$router = eval($method->getBody());
if ($router instanceof Nette\Application\Routers\RouteList) {
$router->warmupCache();
}
$s = serialize($router);
} catch (\Throwable $e) {
throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
} catch (\Exception $e) {
throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
}
$method->setBody('return unserialize(?);', array($router));
$method->setBody('return unserialize(?);', array($s));
}
}

Expand Down

0 comments on commit bd834c6

Please sign in to comment.