Skip to content

Commit ecb63d7

Browse files
committed
RouteList: added buildCache()
1 parent 6274e7d commit ecb63d7

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

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 = [];
59-
$routes['*'] = [];
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->buildCache();
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 buildCache()
88+
{
89+
$routes = [];
90+
$routes['*'] = [];
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
@@ -69,13 +69,17 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
6969
if (!empty($this->config['cache'])) {
7070
$method = $class->getMethod(Nette\DI\Container::getMethodName($this->prefix('router')));
7171
try {
72-
$router = serialize(eval($method->getBody()));
72+
$router = eval($method->getBody());
73+
if ($router instanceof Nette\Application\Routers\RouteList) {
74+
$router->buildCache();
75+
}
76+
$s = serialize($router);
7377
} catch (\Throwable $e) {
7478
throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
7579
} catch (\Exception $e) {
7680
throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
7781
}
78-
$method->setBody('return unserialize(?);', [$router]);
82+
$method->setBody('return unserialize(?);', [$s]);
7983
}
8084
}
8185

0 commit comments

Comments
 (0)