diff --git a/src/Application/IRouter.php b/src/Application/IRouter.php index 456f53540..c78c7f375 100644 --- a/src/Application/IRouter.php +++ b/src/Application/IRouter.php @@ -35,4 +35,10 @@ function match(Nette\Http\IRequest $httpRequest); */ function constructUrl(Request $appRequest, Nette\Http\Url $refUrl); + /** + * Returns list of possible target presenters or NULL if the list is dynamic. + * @return string[]|NULL + */ + // function getTargetPresenters(); + } diff --git a/src/Application/Routers/Route.php b/src/Application/Routers/Route.php index 79762baf4..090d52f13 100644 --- a/src/Application/Routers/Route.php +++ b/src/Application/Routers/Route.php @@ -652,13 +652,12 @@ public function getFlags() /** * Proprietary cache aim. - * @internal - * @return string|FALSE + * @return string[]|NULL */ - public function getTargetPresenter() + public function getTargetPresenters() { if ($this->flags & self::ONE_WAY) { - return FALSE; + return array(); } $m = $this->metadata; @@ -673,7 +672,7 @@ public function getTargetPresenter() } if (isset($m[self::PRESENTER_KEY]['fixity']) && $m[self::PRESENTER_KEY]['fixity'] === self::CONSTANT) { - return $module . $m[self::PRESENTER_KEY][self::VALUE]; + return array($module . $m[self::PRESENTER_KEY][self::VALUE]); } return NULL; } diff --git a/src/Application/Routers/RouteList.php b/src/Application/Routers/RouteList.php index d612bc97b..ba23d78cf 100644 --- a/src/Application/Routers/RouteList.php +++ b/src/Application/Routers/RouteList.php @@ -57,33 +57,6 @@ 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) { - $presenter = $route instanceof Route ? $route->getTargetPresenter() : NULL; - - if ($presenter === FALSE) { - continue; - } - - if (is_string($presenter)) { - if (!isset($routes[$presenter])) { - $routes[$presenter] = $routes['*']; - } - $routes[$presenter][] = $route; - - } else { - foreach ($routes as $id => $foo) { - $routes[$id][] = $route; - } - } - } - - $this->cachedRoutes = $routes; - } - if ($this->module) { if (strncmp($tmp = $appRequest->getPresenterName(), $this->module, strlen($this->module)) === 0) { $appRequest = clone $appRequest; @@ -94,6 +67,10 @@ public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\U } $presenter = $appRequest->getPresenterName(); + if ($this->cachedRoutes === NULL) { + $this->cachedRoutes = $this->buildCache(); + } + if (!isset($this->cachedRoutes[$presenter])) { $presenter = '*'; } @@ -132,4 +109,45 @@ public function getModule() return $this->module; } + + /** + * Returns list of possible target presenters or NULL if the list is dynamic. + * + * @return string[]|NULL + */ + public function getTargetPresenters() + { + if ($this->cachedRoutes === NULL) { + $this->cachedRoutes = $this->buildCache(); + } + + if (empty($this->cachedRoutes['*'])) { + $presenters = array_keys($this->cachedRoutes); + unset($presenters[0]); // remove '*' + return $presenters; + } + + return NULL; + } + + + /** + * @return array + */ + private function buildCache() + { + $routes = array('*' => array()); + foreach ($this as $route) { + $presenters = method_exists($route, 'getTargetPresenters') ? $route->getTargetPresenters() : NULL; + $keys = is_array($presenters) ? $presenters : array_keys($routes); + foreach ($keys as $key) { + if (!isset($routes[$key])) { + $routes[$key] = $routes['*']; + } + $routes[$key][] = $route; + } + } + return $routes; + } + }