Skip to content

Commit

Permalink
IRoute: added commented out getTargetPresenters()
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Jan 18, 2015
1 parent 2d1e6b9 commit f813271
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
6 changes: 6 additions & 0 deletions src/Application/IRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
9 changes: 4 additions & 5 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
72 changes: 45 additions & 27 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = '*';
}
Expand Down Expand Up @@ -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;
}

}

0 comments on commit f813271

Please sign in to comment.