Skip to content

Commit

Permalink
RoutingPanel: reimplemented using beforeMatch() & afterMatch()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 5, 2023
1 parent 0638243 commit c0db46b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
43 changes: 18 additions & 25 deletions src/Bridges/ApplicationTracy/RoutingPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,33 @@ private function analyse(
Routing\Router $router,
?Nette\Http\IRequest $httpRequest,
string $module = '',
?string $path = null,
string $path = '',
?\Closure $afterMatch = null,
int $level = -1,
int $flag = 0
): void
{
$afterMatch = $afterMatch ?? function ($params) { return $params; };

if ($router instanceof Routing\RouteList) {
if ($httpRequest) {
try {
$httpRequest = $router->match($httpRequest) === null ? null : $httpRequest;
} catch (\Throwable $e) {
$httpRequest = null;
}
}
$path .= (function () { return $this->path; })->bindTo($router, Routing\RouteList::class)();
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');

$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));
}
$httpRequest = $httpRequest
? (new \ReflectionMethod($router, 'beforeMatch'))->invoke($router, $httpRequest)
: null;

$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
$afterMatch = function ($params) use ($router, $afterMatch) {
$params = $params === null
? null
: (new \ReflectionMethod($router, 'afterMatch'))->invoke($router, $params);
return $afterMatch($params);
};

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

if ($info = $this->routers[$next] ?? null) {
Expand All @@ -133,18 +132,12 @@ private function analyse(
$matched = $flag & Routing\RouteList::ONE_WAY ? 'oneway' : 'no';
$params = $e = null;
try {
$params = $httpRequest
? $router->match($httpRequest)
: null;
$params = $httpRequest ? $afterMatch($router->match($httpRequest)) : null;
} catch (\Throwable $e) {
$matched = 'error';
}

if ($params !== null) {
if ($module) {
$params['presenter'] = $module . ($params['presenter'] ?? '');
}

$matched = 'may';
if ($this->matched === null) {
$this->matched = $params;
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

0 comments on commit c0db46b

Please sign in to comment.