Skip to content

Commit 9c27bc6

Browse files
committed
RoutingPanel: reimplemented using beforeMatch() & afterMatch()
1 parent 0638243 commit 9c27bc6

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/Bridges/ApplicationTracy/RoutingPanel.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,33 @@ private function analyse(
8989
Routing\Router $router,
9090
?Nette\Http\IRequest $httpRequest,
9191
string $module = '',
92-
?string $path = null,
92+
string $path = '',
93+
?\Closure $afterMatch = null,
9394
int $level = -1,
9495
int $flag = 0
9596
): void
9697
{
98+
$afterMatch = $afterMatch ?? function ($params) { return $params; };
99+
97100
if ($router instanceof Routing\RouteList) {
98-
if ($httpRequest) {
99-
try {
100-
$httpRequest = $router->match($httpRequest) === null ? null : $httpRequest;
101-
} catch (\Throwable $e) {
102-
$httpRequest = null;
103-
}
104-
}
101+
$path .= $router->getPath();
102+
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
105103

106-
$prop = (new \ReflectionProperty(Routing\RouteList::class, 'path'));
107-
$prop->setAccessible(true);
108-
if ($httpRequest && ($pathPrefix = $prop->getValue($router))) {
109-
$path .= $pathPrefix;
110-
$url = $httpRequest->getUrl();
111-
$httpRequest = $httpRequest->withUrl($url->withPath($url->getPath(), $url->getBasePath() . $pathPrefix));
112-
}
104+
$httpRequest = $httpRequest
105+
? (new \ReflectionMethod($router, 'beforeMatch'))->invoke($router, $httpRequest)
106+
: null;
113107

114-
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
108+
$afterMatch = function ($params) use ($router, $afterMatch) {
109+
$params = $params === null
110+
? null
111+
: (new \ReflectionMethod($router, 'afterMatch'))->invoke($router, $params);
112+
return $afterMatch($params);
113+
};
115114

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

122121
if ($info = $this->routers[$next] ?? null) {
@@ -133,18 +132,12 @@ private function analyse(
133132
$matched = $flag & Routing\RouteList::ONE_WAY ? 'oneway' : 'no';
134133
$params = $e = null;
135134
try {
136-
$params = $httpRequest
137-
? $router->match($httpRequest)
138-
: null;
135+
$params = $httpRequest ? $afterMatch($router->match($httpRequest)) : null;
139136
} catch (\Throwable $e) {
140137
$matched = 'error';
141138
}
142139

143140
if ($params !== null) {
144-
if ($module) {
145-
$params['presenter'] = $module . ($params['presenter'] ?? '');
146-
}
147-
148141
$matched = 'may';
149142
if ($this->matched === null) {
150143
$this->matched = $params;

src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use Tracy\Helpers;
9494
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$router->matched] ?></td>
9595

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

0 commit comments

Comments
 (0)