Skip to content

Commit 5a9249f

Browse files
committed
RouteList, RoutingPanel: reimplemented using prepareRequest() & completeParameters()
1 parent 583a4f3 commit 5a9249f

File tree

3 files changed

+55
-87
lines changed

3 files changed

+55
-87
lines changed

src/Application/Routers/RouteList.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ public function __construct(?string $module = null)
3232

3333

3434
/**
35-
* Maps HTTP request to an array.
35+
* Support for modules.
3636
*/
37-
public function match(Nette\Http\IRequest $httpRequest): ?array
37+
protected function completeParameters(array $params): ?array
3838
{
39-
$params = parent::match($httpRequest);
40-
4139
$presenter = $params[self::PresenterKey] ?? null;
4240
if (is_string($presenter) && strncmp($presenter, 'Nette:', 6)) {
4341
$params[self::PresenterKey] = $this->module . $presenter;

src/Bridges/ApplicationTracy/RoutingPanel.php

Lines changed: 53 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public function __construct(
5757
*/
5858
public function getTab(): string
5959
{
60-
$this->analyse($this->router, $this->httpRequest);
60+
$this->analyse(
61+
$this->router instanceof Routing\RouteList
62+
? $this->router
63+
: (new Routing\RouteList)->add($this->router),
64+
$this->httpRequest
65+
);
6166
return Nette\Utils\Helpers::capture(function () {
6267
$matched = $this->matched;
6368
require __DIR__ . '/templates/RoutingPanel.tab.phtml';
@@ -82,91 +87,66 @@ public function getPanel(): string
8287
}
8388

8489

85-
/**
86-
* Analyses simple route.
87-
*/
8890
private function analyse(
89-
Routing\Router $router,
91+
Routing\RouteList $router,
9092
?Nette\Http\IRequest $httpRequest,
9193
string $module = '',
9294
string $path = '',
93-
int $level = -1,
94-
int $flag = 0
95+
int $level = 0
9596
): void
9697
{
97-
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;
98+
$path .= $router->getPath();
99+
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
100+
$httpRequest = $httpRequest
101+
? (function () use ($httpRequest) { return $this->prepareRequest($httpRequest); })->bindTo($router, Routing\RouteList::class)()
102+
: null;
103+
$flags = $router->getFlags();
104+
105+
foreach ($router->getRouters() as $i => $innerRouter) {
106+
if ($innerRouter instanceof Routing\RouteList) {
107+
$next = count($this->routers);
108+
$this->analyse($innerRouter, $httpRequest, $module, $path, $level + 1);
109+
if ($info = $this->routers[$next] ?? null) {
110+
$info->gutterTop = abs($level - $info->level);
103111
}
104-
}
105-
106-
$prop = (new \ReflectionProperty(Routing\RouteList::class, 'path'));
107-
$prop->setAccessible(true);
108-
$path .= $pathPrefix = $prop->getValue($router);
109-
if ($httpRequest && $pathPrefix) {
110-
$url = $httpRequest->getUrl();
111-
$url = $url->getRelativePath() . '/' === $pathPrefix
112-
? $url->withPath($url->getPath() . '/')
113-
: $url->withPath($url->getPath(), $url->getBasePath() . $pathPrefix);
114-
$httpRequest = $httpRequest->withUrl($url);
115-
}
116-
117-
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
118-
119-
$next = count($this->routers);
120-
$flags = $router->getFlags();
121-
foreach ($router->getRouters() as $i => $subRouter) {
122-
$this->analyse($subRouter, $httpRequest, $module, $path, $level + 1, $flags[$i]);
123-
}
124-
125-
if ($info = $this->routers[$next] ?? null) {
126-
$info->gutterTop = abs(max(0, $level) - $info->level);
127-
}
128112

129-
if ($info = end($this->routers)) {
130-
$info->gutterBottom = abs(max(0, $level) - $info->level);
113+
if ($info = end($this->routers)) {
114+
$info->gutterBottom = abs($level - $info->level);
115+
}
116+
continue;
131117
}
132118

133-
return;
134-
}
135-
136-
$matched = $flag & Routing\RouteList::ONE_WAY ? 'oneway' : 'no';
137-
$params = $e = null;
138-
try {
139-
$params = $httpRequest
140-
? $router->match($httpRequest)
141-
: null;
142-
} catch (\Throwable $e) {
143-
$matched = 'error';
144-
}
145-
146-
if ($params !== null) {
147-
if ($module) {
148-
$params['presenter'] = $module . ($params['presenter'] ?? '');
119+
$matched = $flags[$i] & $router::ONE_WAY ? 'oneway' : 'no';
120+
$params = $e = null;
121+
try {
122+
if (
123+
$httpRequest
124+
&& ($params = $innerRouter->match($httpRequest)) !== null
125+
&& ($params = (function () use ($params) { return $this->completeParameters($params); })->bindTo($router, Routing\RouteList::class)()) !== null
126+
) {
127+
$matched = 'may';
128+
if ($this->matched === null) {
129+
$this->matched = $params;
130+
$this->findSource();
131+
$matched = 'yes';
132+
}
133+
}
134+
} catch (\Throwable $e) {
135+
$matched = 'error';
149136
}
150137

151-
$matched = 'may';
152-
if ($this->matched === null) {
153-
$this->matched = $params;
154-
$this->findSource();
155-
$matched = 'yes';
156-
}
138+
$this->routers[] = (object) [
139+
'level' => max(0, $level),
140+
'matched' => $matched,
141+
'class' => get_class($innerRouter),
142+
'defaults' => $innerRouter instanceof Routing\Route || $innerRouter instanceof Routing\SimpleRouter ? $innerRouter->getDefaults() : [],
143+
'mask' => $innerRouter instanceof Routing\Route ? $innerRouter->getMask() : null,
144+
'params' => $params,
145+
'module' => rtrim($module, ':'),
146+
'path' => $path,
147+
'error' => $e,
148+
];
157149
}
158-
159-
$this->routers[] = (object) [
160-
'level' => max(0, $level),
161-
'matched' => $matched,
162-
'class' => get_class($router),
163-
'defaults' => $router instanceof Routing\Route || $router instanceof Routing\SimpleRouter ? $router->getDefaults() : [],
164-
'mask' => $router instanceof Routing\Route ? $router->getMask() : null,
165-
'params' => $params,
166-
'module' => rtrim($module, ':'),
167-
'path' => $path,
168-
'error' => $e,
169-
];
170150
}
171151

172152

tests/Bridges.Tracy/RoutingPanel.analyse.withPath.phpt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ test('URL: /', function () use ($router) {
4242
'module' => '',
4343
'path' => '',
4444
'error' => null,
45-
'gutterTop' => 0,
4645
],
4746
(object) [
4847
'level' => 1,
@@ -89,7 +88,6 @@ test('URL: /', function () use ($router) {
8988
'module' => '',
9089
'path' => '',
9190
'error' => null,
92-
'gutterBottom' => 0,
9391
],
9492
], $res);
9593
});
@@ -116,7 +114,6 @@ test('URL: /foo', function () use ($router) {
116114
'module' => '',
117115
'path' => '',
118116
'error' => null,
119-
'gutterTop' => 0,
120117
],
121118
(object) [
122119
'level' => 1,
@@ -163,7 +160,6 @@ test('URL: /foo', function () use ($router) {
163160
'module' => '',
164161
'path' => '',
165162
'error' => null,
166-
'gutterBottom' => 0,
167163
],
168164
], $res);
169165
});
@@ -190,7 +186,6 @@ test('URL: /admin', function () use ($router) {
190186
'module' => '',
191187
'path' => '',
192188
'error' => null,
193-
'gutterTop' => 0,
194189
],
195190
(object) [
196191
'level' => 1,
@@ -237,7 +232,6 @@ test('URL: /admin', function () use ($router) {
237232
'module' => '',
238233
'path' => '',
239234
'error' => null,
240-
'gutterBottom' => 0,
241235
],
242236
], $res);
243237
});
@@ -264,7 +258,6 @@ test('URL: /admin/', function () use ($router) {
264258
'module' => '',
265259
'path' => '',
266260
'error' => null,
267-
'gutterTop' => 0,
268261
],
269262
(object) [
270263
'level' => 1,
@@ -311,7 +304,6 @@ test('URL: /admin/', function () use ($router) {
311304
'module' => '',
312305
'path' => '',
313306
'error' => null,
314-
'gutterBottom' => 0,
315307
],
316308
], $res);
317309
});
@@ -338,7 +330,6 @@ test('URL: /admin/foo', function () use ($router) {
338330
'module' => '',
339331
'path' => '',
340332
'error' => null,
341-
'gutterTop' => 0,
342333
],
343334
(object) [
344335
'level' => 1,
@@ -385,7 +376,6 @@ test('URL: /admin/foo', function () use ($router) {
385376
'module' => '',
386377
'path' => '',
387378
'error' => null,
388-
'gutterBottom' => 0,
389379
],
390380
], $res);
391381
});

0 commit comments

Comments
 (0)