Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd834c6

Browse files
committed
RouteList: added warmupCache()
1 parent c292b85 commit bd834c6

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

src/Application/Routers/RouteList.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,7 @@ public function match(Nette\Http\IRequest $httpRequest)
5555
public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl)
5656
{
5757
if ($this->cachedRoutes === NULL) {
58-
$routes = array();
59-
$routes['*'] = array();
60-
61-
foreach ($this as $route) {
62-
$presenters = $route instanceof Route && is_array($tmp = $route->getTargetPresenters())
63-
? $tmp : array_keys($routes);
64-
65-
foreach ($presenters as $presenter) {
66-
if (!isset($routes[$presenter])) {
67-
$routes[$presenter] = $routes['*'];
68-
}
69-
$routes[$presenter][] = $route;
70-
}
71-
}
72-
73-
$this->cachedRoutes = $routes;
58+
$this->warmupCache();
7459
}
7560

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

10085

86+
/** @internal */
87+
public function warmupCache()
88+
{
89+
$routes = array();
90+
$routes['*'] = array();
91+
92+
foreach ($this as $route) {
93+
$presenters = $route instanceof Route && is_array($tmp = $route->getTargetPresenters())
94+
? $tmp
95+
: array_keys($routes);
96+
97+
foreach ($presenters as $presenter) {
98+
if (!isset($routes[$presenter])) {
99+
$routes[$presenter] = $routes['*'];
100+
}
101+
$routes[$presenter][] = $route;
102+
}
103+
}
104+
105+
$this->cachedRoutes = $routes;
106+
}
107+
108+
101109
/**
102110
* Adds the router.
103111
* @param mixed

src/Bridges/ApplicationDI/RoutingExtension.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
6868
if (!empty($this->config['cache'])) {
6969
$method = $class->getMethod(Nette\DI\Container::getMethodName($this->prefix('router')));
7070
try {
71-
$router = serialize(eval($method->getBody()));
71+
$router = eval($method->getBody());
72+
if ($router instanceof Nette\Application\Routers\RouteList) {
73+
$router->warmupCache();
74+
}
75+
$s = serialize($router);
7276
} catch (\Throwable $e) {
7377
throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
7478
} catch (\Exception $e) {
7579
throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
7680
}
77-
$method->setBody('return unserialize(?);', array($router));
81+
$method->setBody('return unserialize(?);', array($s));
7882
}
7983
}
8084

0 commit comments

Comments
 (0)