Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalized Route::getTargetPresenter() #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();

}
3 changes: 1 addition & 2 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,7 @@ public function getFlags()


/**
* Proprietary cache aim.
* @internal
* Returns list of possible target presenters or NULL if the list is dynamic.
* @return string[]|NULL
*/
public function getTargetPresenters()
Expand Down
64 changes: 45 additions & 19 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +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 = [];
$routes['*'] = [];

foreach ($this as $route) {
$presenters = $route instanceof Route && is_array($tmp = $route->getTargetPresenters())
? $tmp : array_keys($routes);

foreach ($presenters as $presenter) {
if (!isset($routes[$presenter])) {
$routes[$presenter] = $routes['*'];
}
$routes[$presenter][] = $route;
}
}

$this->cachedRoutes = $routes;
}

if ($this->module) {
if (strncmp($tmp = $appRequest->getPresenterName(), $this->module, strlen($this->module)) === 0) {
$appRequest = clone $appRequest;
Expand All @@ -86,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 All @@ -111,6 +96,8 @@ public function offsetSet($index, $route)
{
if (!$route instanceof Nette\Application\IRouter) {
throw new Nette\InvalidArgumentException('Argument must be IRouter descendant.');
} elseif ($this->cachedRoutes !== NULL) {
throw new \LogicException('No!'); // TODO: we need some LogicException from Nette namespace
}
parent::offsetSet($index, $route);
}
Expand All @@ -124,4 +111,43 @@ 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);
return array_slice($presenters, 1); // remove '*'
}

return NULL;
}


/**
* @return array
*/
private function buildCache()
{
$routes = ['*' => []];
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;
}

}
53 changes: 53 additions & 0 deletions tests/Application.Routers/RouteList.getTargetPresenters().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Test: Nette\Application\Routers\RouteList getTargetPresenters().
*/

use Nette\Application\Routers\RouteList;
use Nette\Application\Routers\Route;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

require __DIR__ . '/Route.inc';


test(function () {
$list = new RouteList();
Assert::same([], $list->getTargetPresenters());
});


test(function () {
$list = new RouteList();
$list[] = new Route('about', 'About:default');
Assert::same(['About'], $list->getTargetPresenters());
});


test(function () {
$list = new RouteList();
$list[] = new Route('about', 'About:default');
$list[] = new Route('<presenter>/<action>');
Assert::same(NULL, $list->getTargetPresenters());
});


test(function () {
$list = new RouteList();
$list[] = new Route('about', 'About:default');
$list[] = new RouteList();
$list[1][] = new Route('homepage', 'Homepage:default');
Assert::same(['About', 'Homepage'], $list->getTargetPresenters());
});


test(function () {
$list = new RouteList();
$list[] = new Route('about', 'About:default');
$list[] = new RouteList();
$list[1][] = new Route('<presenter>/<action>');
Assert::same(NULL, $list->getTargetPresenters());
});