diff --git a/src/Bridges/ApplicationTracy/RoutingPanel.php b/src/Bridges/ApplicationTracy/RoutingPanel.php index 381fcb69a..c17994545 100644 --- a/src/Bridges/ApplicationTracy/RoutingPanel.php +++ b/src/Bridges/ApplicationTracy/RoutingPanel.php @@ -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'; @@ -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, - ]; } diff --git a/src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml b/src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml index 3c51efe9b..0c4fb25f6 100644 --- a/src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml +++ b/src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml @@ -94,7 +94,7 @@ use Tracy\Helpers; > '✓', 'may' => '≈', 'no' => '', 'oneway' => '⛔', 'error' => '❌'][$router->matched] ?> path) ? '' . Helpers::escapeHtml($router->path) . '' : '', + $router->path === '' ? '' : '' . Helpers::escapeHtml($router->path) . '', isset($router->mask) ? str_replace(['/', '-'], ['/', '-'], Helpers::escapeHtml($router->mask)) : str_replace('\\', '\\', Helpers::escapeHtml($router->class)) ?> diff --git a/tests/Bridges.Tracy/RoutingPanel.analyse.withPath.phpt b/tests/Bridges.Tracy/RoutingPanel.analyse.withPath.phpt index aab2479fa..5b1f0b668 100644 --- a/tests/Bridges.Tracy/RoutingPanel.analyse.withPath.phpt +++ b/tests/Bridges.Tracy/RoutingPanel.analyse.withPath.phpt @@ -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, @@ -52,7 +51,7 @@ test('URL: /', function () use ($router) { 'mask' => '', 'params' => null, 'module' => '', - 'path' => null, + 'path' => 'admin/', 'error' => null, 'gutterTop' => 1, ], @@ -64,7 +63,7 @@ test('URL: /', function () use ($router) { 'mask' => '/', 'params' => null, 'module' => '', - 'path' => null, + 'path' => 'admin/', 'error' => null, 'gutterBottom' => 1, ], @@ -76,7 +75,7 @@ test('URL: /', function () use ($router) { 'mask' => '', 'params' => ['presenter' => 'Article', 'action' => 'homepage'], 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, ], (object) [ @@ -87,9 +86,8 @@ test('URL: /', function () use ($router) { 'mask' => '/', 'params' => null, 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, - 'gutterBottom' => 0, ], ], $res); }); @@ -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, @@ -126,7 +123,7 @@ test('URL: /foo', function () use ($router) { 'mask' => '', 'params' => null, 'module' => '', - 'path' => null, + 'path' => 'admin/', 'error' => null, 'gutterTop' => 1, ], @@ -138,7 +135,7 @@ test('URL: /foo', function () use ($router) { 'mask' => '/', 'params' => null, 'module' => '', - 'path' => null, + 'path' => 'admin/', 'error' => null, 'gutterBottom' => 1, ], @@ -150,7 +147,7 @@ test('URL: /foo', function () use ($router) { 'mask' => '', 'params' => null, 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, ], (object) [ @@ -161,9 +158,8 @@ test('URL: /foo', function () use ($router) { 'mask' => '/', 'params' => ['presenter' => 'Foo', 'action' => 'default'], 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, - 'gutterBottom' => 0, ], ], $res); }); @@ -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, ], @@ -212,7 +207,7 @@ test('URL: /admin', function () use ($router) { 'mask' => '/', 'params' => null, 'module' => '', - 'path' => null, + 'path' => 'admin/', 'error' => null, 'gutterBottom' => 1, ], @@ -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' => '/', 'params' => ['presenter' => 'Admin', 'action' => 'default'], 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, - 'gutterBottom' => 0, ], ], $res); }); @@ -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, @@ -298,7 +291,7 @@ test('URL: /admin/', function () use ($router) { 'mask' => '', 'params' => null, 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, ], (object) [ @@ -309,9 +302,8 @@ test('URL: /admin/', function () use ($router) { 'mask' => '/', 'params' => ['presenter' => 'Admin', 'action' => 'default'], 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, - 'gutterBottom' => 0, ], ], $res); }); @@ -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, @@ -372,7 +363,7 @@ test('URL: /admin/foo', function () use ($router) { 'mask' => '', 'params' => null, 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, ], (object) [ @@ -383,9 +374,8 @@ test('URL: /admin/foo', function () use ($router) { 'mask' => '/', 'params' => ['presenter' => 'Admin', 'action' => 'foo'], 'module' => '', - 'path' => null, + 'path' => '', 'error' => null, - 'gutterBottom' => 0, ], ], $res); });