Skip to content

Commit

Permalink
Route: getTargetPresenter() generalized to interface IFixedTargetPres…
Browse files Browse the repository at this point in the history
…enters
  • Loading branch information
JanTvrdik committed Nov 2, 2014
1 parent c2d1f55 commit a962376
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
27 changes: 27 additions & 0 deletions src/Application/IFixedTargetPresenters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/

namespace Nette\Application;

use Nette;


/**
* Router with fixed target presenters.
*
* @author Jan Tvrdik
*/
interface IFixedTargetPresenters extends IRouter
{

/**
* Returns list of possible target presenters or NULL if the list is dynamic.
* @return string[]|NULL
*/
function getTargetPresenters();

}
11 changes: 5 additions & 6 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @property-read int $flags
* @property-read string|FALSE $targetPresenter
*/
class Route extends Nette\Object implements Application\IRouter
class Route extends Nette\Object implements Application\IRouter, Application\IFixedTargetPresenters
{
const PRESENTER_KEY = 'presenter';
const MODULE_KEY = 'module';
Expand Down Expand Up @@ -661,13 +661,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 @@ -682,7 +681,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
20 changes: 9 additions & 11 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Nette\Application\Routers;

use Nette;
use Nette\Application\IFixedTargetPresenters;


/**
Expand Down Expand Up @@ -62,18 +63,15 @@ public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\U
$routes['*'] = array();

foreach ($this as $route) {
$presenter = $route instanceof Route ? $route->getTargetPresenter() : NULL;

if ($presenter === FALSE) {
continue;
}

if (is_string($presenter)) {
$presenter = strtolower($presenter);
if (!isset($routes[$presenter])) {
$routes[$presenter] = $routes['*'];
$presenters = $route instanceof IFixedTargetPresenters ? $route->getTargetPresenters() : NULL;
if (is_array($presenters)) {
foreach ($presenters as $presenter) {
$presenter = strtolower($presenter);
if (!isset($routes[$presenter])) {
$routes[$presenter] = $routes['*'];
}
$routes[$presenter][] = $route;
}
$routes[$presenter][] = $route;

} else {
foreach ($routes as $id => $foo) {
Expand Down

0 comments on commit a962376

Please sign in to comment.