Skip to content

Commit

Permalink
RoutingPanel: reimplemented using prepareRequest() & completeParamete…
Browse files Browse the repository at this point in the history
…rs()
  • Loading branch information
dg committed Oct 9, 2023
1 parent da67f67 commit 4ebd4a9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 106 deletions.
125 changes: 54 additions & 71 deletions src/Bridges/ApplicationTracy/RoutingPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public function __construct(
*/
public function getTab(): string
{
$this->analyse($this->router, $this->httpRequest);
$this->analyse(
$this->router instanceof Routing\RouteList
? $this->router
: (new Routing\RouteList)->add($this->router),
$this->httpRequest
);
return Nette\Utils\Helpers::capture(function () {
$matched = $this->matched;
require __DIR__ . '/templates/RoutingPanel.tab.phtml';
Expand All @@ -82,88 +87,66 @@ public function getPanel(): string
}


/**
* Analyses simple route.
*/
private function analyse(
Routing\Router $router,
Routing\RouteList $router,
?Nette\Http\IRequest $httpRequest,
string $module = '',
?string $path = null,
int $level = -1,
int $flag = 0
string $path = '',
int $level = 0
): void
{
if ($router instanceof Routing\RouteList) {
if ($httpRequest) {
try {
$httpRequest = $router->match($httpRequest) === null ? null : $httpRequest;
} catch (\Throwable $e) {
$httpRequest = null;
$path .= $router->getPath();
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
$httpRequest = $httpRequest
? (function () use ($httpRequest) { return $this->prepareRequest($httpRequest); })->bindTo($router, Routing\RouteList::class)()
: null;
$flags = $router->getFlags();

foreach ($router->getRouters() as $i => $innerRouter) {
if ($innerRouter instanceof Routing\RouteList) {
$next = count($this->routers);
$this->analyse($innerRouter, $httpRequest, $module, $path, $level + 1);
if ($info = $this->routers[$next] ?? null) {
$info->gutterTop = abs($level - $info->level);
}
}

$prop = (new \ReflectionProperty(Routing\RouteList::class, 'path'));
$prop->setAccessible(true);
if ($httpRequest && ($pathPrefix = $prop->getValue($router))) {
$path .= $pathPrefix;
$url = $httpRequest->getUrl();
$httpRequest = $httpRequest->withUrl($url->withPath($url->getPath(), $url->getBasePath() . $pathPrefix));
}

$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');

$next = count($this->routers);
$flags = $router->getFlags();
foreach ($router->getRouters() as $i => $subRouter) {
$this->analyse($subRouter, $httpRequest, $module, $path, $level + 1, $flags[$i]);
}

if ($info = $this->routers[$next] ?? null) {
$info->gutterTop = abs(max(0, $level) - $info->level);
}

if ($info = end($this->routers)) {
$info->gutterBottom = abs(max(0, $level) - $info->level);
if ($info = end($this->routers)) {
$info->gutterBottom = abs($level - $info->level);
}
continue;
}

return;
}

$matched = $flag & Routing\RouteList::ONE_WAY ? 'oneway' : 'no';
$params = $e = null;
try {
$params = $httpRequest
? $router->match($httpRequest)
: null;
} catch (\Throwable $e) {
$matched = 'error';
}

if ($params !== null) {
if ($module) {
$params['presenter'] = $module . ($params['presenter'] ?? '');
$matched = $flags[$i] & $router::ONE_WAY ? 'oneway' : 'no';
$params = $e = null;
try {
if (
$httpRequest
&& ($params = $innerRouter->match($httpRequest)) !== null
&& ($params = (function () use ($params) { return $this->completeParameters($params); })->bindTo($router, Routing\RouteList::class)()) !== null
) {
$matched = 'may';
if ($this->matched === null) {
$this->matched = $params;
$this->findSource();
$matched = 'yes';
}
}
} catch (\Throwable $e) {
$matched = 'error';
}

$matched = 'may';
if ($this->matched === null) {
$this->matched = $params;
$this->findSource();
$matched = 'yes';
}
$this->routers[] = (object) [
'level' => max(0, $level),
'matched' => $matched,
'class' => get_class($innerRouter),
'defaults' => $innerRouter instanceof Routing\Route || $innerRouter instanceof Routing\SimpleRouter ? $innerRouter->getDefaults() : [],
'mask' => $innerRouter instanceof Routing\Route ? $innerRouter->getMask() : null,
'params' => $params,
'module' => rtrim($module, ':'),
'path' => $path,
'error' => $e,
];
}

$this->routers[] = (object) [
'level' => max(0, $level),
'matched' => $matched,
'class' => get_class($router),
'defaults' => $router instanceof Routing\Route || $router instanceof Routing\SimpleRouter ? $router->getDefaults() : [],
'mask' => $router instanceof Routing\Route ? $router->getMask() : null,
'params' => $params,
'module' => rtrim($module, ':'),
'path' => $path,
'error' => $e,
];
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use Tracy\Helpers;
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$router->matched] ?></td>

<td><code title="<?= Helpers::escapeHtml($router->class) ?>"><?=
isset($router->path) ? '<small>' . Helpers::escapeHtml($router->path) . '</small>' : '',
$router->path === '' ? '' : '<small>' . Helpers::escapeHtml($router->path) . '</small>',
isset($router->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], Helpers::escapeHtml($router->mask)) : str_replace('\\', '<wbr>\\', Helpers::escapeHtml($router->class))
?></code></td>

Expand Down
58 changes: 24 additions & 34 deletions tests/Bridges.Tracy/RoutingPanel.analyse.withPath.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ test('URL: /', function () use ($router) {
'mask' => 'sign-in',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterTop' => 0,
],
(object) [
'level' => 1,
Expand All @@ -52,7 +51,7 @@ test('URL: /', function () use ($router) {
'mask' => '',
'params' => null,
'module' => '',
'path' => null,
'path' => 'admin/',
'error' => null,
'gutterTop' => 1,
],
Expand All @@ -64,7 +63,7 @@ test('URL: /', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => null,
'module' => '',
'path' => null,
'path' => 'admin/',
'error' => null,
'gutterBottom' => 1,
],
Expand All @@ -76,7 +75,7 @@ test('URL: /', function () use ($router) {
'mask' => '',
'params' => ['presenter' => 'Article', 'action' => 'homepage'],
'module' => '',
'path' => null,
'path' => '',
'error' => null,
],
(object) [
Expand All @@ -87,9 +86,8 @@ test('URL: /', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterBottom' => 0,
],
], $res);
});
Expand All @@ -114,9 +112,8 @@ test('URL: /foo', function () use ($router) {
'mask' => 'sign-in',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterTop' => 0,
],
(object) [
'level' => 1,
Expand All @@ -126,7 +123,7 @@ test('URL: /foo', function () use ($router) {
'mask' => '',
'params' => null,
'module' => '',
'path' => null,
'path' => 'admin/',
'error' => null,
'gutterTop' => 1,
],
Expand All @@ -138,7 +135,7 @@ test('URL: /foo', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => null,
'module' => '',
'path' => null,
'path' => 'admin/',
'error' => null,
'gutterBottom' => 1,
],
Expand All @@ -150,7 +147,7 @@ test('URL: /foo', function () use ($router) {
'mask' => '',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
],
(object) [
Expand All @@ -161,9 +158,8 @@ test('URL: /foo', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => ['presenter' => 'Foo', 'action' => 'default'],
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterBottom' => 0,
],
], $res);
});
Expand All @@ -188,19 +184,18 @@ test('URL: /admin', function () use ($router) {
'mask' => 'sign-in',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterTop' => 0,
],
(object) [
'level' => 1,
'matched' => 'no',
'matched' => 'yes',
'class' => 'Nette\Application\Routers\Route',
'defaults' => ['presenter' => 'Admin:Home', 'action' => 'default'],
'mask' => '',
'params' => null,
'params' => ['presenter' => 'Admin:Home', 'action' => 'default'],
'module' => '',
'path' => null,
'path' => 'admin/',
'error' => null,
'gutterTop' => 1,
],
Expand All @@ -212,7 +207,7 @@ test('URL: /admin', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => null,
'module' => '',
'path' => null,
'path' => 'admin/',
'error' => null,
'gutterBottom' => 1,
],
Expand All @@ -224,20 +219,19 @@ test('URL: /admin', function () use ($router) {
'mask' => '',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
],
(object) [
'level' => 0,
'matched' => 'yes',
'matched' => 'may',
'class' => 'Nette\Application\Routers\Route',
'defaults' => ['action' => 'default'],
'mask' => '<presenter>/<action=default>',
'params' => ['presenter' => 'Admin', 'action' => 'default'],
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterBottom' => 0,
],
], $res);
});
Expand All @@ -262,9 +256,8 @@ test('URL: /admin/', function () use ($router) {
'mask' => 'sign-in',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterTop' => 0,
],
(object) [
'level' => 1,
Expand Down Expand Up @@ -298,7 +291,7 @@ test('URL: /admin/', function () use ($router) {
'mask' => '',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
],
(object) [
Expand All @@ -309,9 +302,8 @@ test('URL: /admin/', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => ['presenter' => 'Admin', 'action' => 'default'],
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterBottom' => 0,
],
], $res);
});
Expand All @@ -336,9 +328,8 @@ test('URL: /admin/foo', function () use ($router) {
'mask' => 'sign-in',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterTop' => 0,
],
(object) [
'level' => 1,
Expand Down Expand Up @@ -372,7 +363,7 @@ test('URL: /admin/foo', function () use ($router) {
'mask' => '',
'params' => null,
'module' => '',
'path' => null,
'path' => '',
'error' => null,
],
(object) [
Expand All @@ -383,9 +374,8 @@ test('URL: /admin/foo', function () use ($router) {
'mask' => '<presenter>/<action=default>',
'params' => ['presenter' => 'Admin', 'action' => 'foo'],
'module' => '',
'path' => null,
'path' => '',
'error' => null,
'gutterBottom' => 0,
],
], $res);
});

0 comments on commit 4ebd4a9

Please sign in to comment.