diff --git a/src/Application/Routers/RouteList.php b/src/Application/Routers/RouteList.php index 4c1adbbf3..878913915 100644 --- a/src/Application/Routers/RouteList.php +++ b/src/Application/Routers/RouteList.php @@ -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) { @@ -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 diff --git a/src/Bridges/ApplicationDI/RoutingExtension.php b/src/Bridges/ApplicationDI/RoutingExtension.php index ea594d146..93f7e0e55 100644 --- a/src/Bridges/ApplicationDI/RoutingExtension.php +++ b/src/Bridges/ApplicationDI/RoutingExtension.php @@ -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)); } }