Skip to content

Commit dae132b

Browse files
committed
RoutingPanel: redesigned [Closes #285]
1 parent 7f87154 commit dae132b

File tree

3 files changed

+387
-378
lines changed

3 files changed

+387
-378
lines changed

src/Bridges/ApplicationTracy/RoutingPanel.php

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ final class RoutingPanel implements Tracy\IBarPanel
3131
/** @var Nette\Application\IPresenterFactory */
3232
private $presenterFactory;
3333

34-
/** @var \stdClass[] */
35-
private $routers = [];
34+
/** @var (array|\stdClass)[] */
35+
private $routes;
3636

3737
/** @var array|null */
3838
private $matched;
3939

40-
/** @var \ReflectionClass|\ReflectionMethod */
41-
private $source;
42-
4340

4441
public function __construct(
4542
Routing\Router $router,
@@ -57,7 +54,7 @@ public function __construct(
5754
*/
5855
public function getTab(): string
5956
{
60-
$this->analyse(
57+
$this->routes = $this->analyse(
6158
$this->router instanceof Routing\RouteList
6259
? $this->router
6360
: (new Routing\RouteList)->add($this->router),
@@ -77,42 +74,31 @@ public function getPanel(): string
7774
{
7875
return Nette\Utils\Helpers::capture(function () {
7976
$matched = $this->matched;
80-
$routers = $this->routers;
81-
$source = $this->source;
82-
$hasModule = (bool) array_filter($routers, function (\stdClass $rq): string { return $rq->module; });
77+
$routes = $this->routes;
78+
$source = $this->matched ? $this->findSource() : null;
8379
$url = $this->httpRequest->getUrl();
8480
$method = $this->httpRequest->getMethod();
8581
require __DIR__ . '/templates/RoutingPanel.panel.phtml';
8682
});
8783
}
8884

8985

90-
private function analyse(
91-
Routing\RouteList $router,
92-
?Nette\Http\IRequest $httpRequest,
93-
string $module = '',
94-
string $path = '',
95-
int $level = 0
96-
): void
86+
private function analyse(Routing\RouteList $router, ?Nette\Http\IRequest $httpRequest): array
9787
{
98-
$path .= $router->getPath();
99-
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
88+
$res = [
89+
'path' => $router->getPath(),
90+
'domain' => $router->getDomain(),
91+
'module' => ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : ''),
92+
'routes' => [],
93+
];
10094
$httpRequest = $httpRequest
10195
? (function () use ($httpRequest) { return $this->prepareRequest($httpRequest); })->bindTo($router, Routing\RouteList::class)()
10296
: null;
10397
$flags = $router->getFlags();
10498

10599
foreach ($router->getRouters() as $i => $innerRouter) {
106100
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);
111-
}
112-
113-
if ($info = end($this->routers)) {
114-
$info->gutterBottom = abs($level - $info->level);
115-
}
101+
$res['routes'][] = $this->analyse($innerRouter, $httpRequest);
116102
continue;
117103
}
118104

@@ -127,37 +113,35 @@ private function analyse(
127113
$matched = 'may';
128114
if ($this->matched === null) {
129115
$this->matched = $params;
130-
$this->findSource();
131116
$matched = 'yes';
132117
}
133118
}
134119
} catch (\Throwable $e) {
135120
$matched = 'error';
136121
}
137122

138-
$this->routers[] = (object) [
139-
'level' => $level,
123+
$res['routes'][] = (object) [
140124
'matched' => $matched,
141125
'class' => get_class($innerRouter),
142126
'defaults' => $innerRouter instanceof Routing\Route || $innerRouter instanceof Routing\SimpleRouter ? $innerRouter->getDefaults() : [],
143127
'mask' => $innerRouter instanceof Routing\Route ? $innerRouter->getMask() : null,
144128
'params' => $params,
145-
'module' => rtrim($module, ':'),
146-
'path' => $path,
147129
'error' => $e,
148130
];
149131
}
132+
return $res;
150133
}
151134

152135

153-
private function findSource(): void
136+
/** @return \ReflectionClass|\ReflectionMethod|null */
137+
private function findSource()
154138
{
155139
$params = $this->matched;
156140
$presenter = $params['presenter'] ?? '';
157141
try {
158142
$class = $this->presenterFactory->getPresenterClass($presenter);
159143
} catch (Nette\Application\InvalidPresenterException $e) {
160-
return;
144+
return null;
161145
}
162146

163147
$rc = new \ReflectionClass($class);
@@ -175,7 +159,7 @@ private function findSource(): void
175159
}
176160
}
177161

178-
$this->source = isset($method) && $rc->hasMethod($method)
162+
return isset($method) && $rc->hasMethod($method)
179163
? $rc->getMethod($method)
180164
: $rc;
181165
}

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

Lines changed: 108 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,70 @@ use Tracy\Helpers;
1010

1111
?>
1212
<style class="tracy-debug">
13-
#tracy-debug .nette-RoutingPanel table {
14-
font: 9pt/1.5 Consolas, monospace;
13+
#tracy-debug .nette-RoutingPanel-grid {
14+
background: #FDF5CE;
15+
display: grid;
16+
grid-template-columns: auto 1fr auto auto;
17+
border: 1px solid #E6DFBF;
1518
}
1619

17-
#tracy-debug .nette-RoutingPanel tr {
18-
border: #d6ceb0 0 solid;
20+
#tracy-debug .nette-RoutingPanel-grid-inner,
21+
#tracy-debug .nette-RoutingPanel-grid-columns {
22+
grid-column: 1 / span 4;
23+
display: grid;
24+
grid-template-columns: subgrid;
1925
}
2026

21-
#tracy-debug .nette-RoutingPanel .yes td {
27+
#tracy-debug .nette-RoutingPanel-grid-columns:nth-child(2n) {
28+
background: rgba(0,0,0,0.02);
29+
}
30+
31+
#tracy-debug .nette-RoutingPanel-grid-header {
32+
color: #655E5E;
33+
background: #F4F3F1;
34+
font-size: 90%;
35+
font-weight: bold;
36+
}
37+
38+
#tracy-debug .nette-RoutingPanel-grid-group-header {
39+
grid-column: 1 / span 4;
40+
font-size: 90%;
41+
font-weight: bold;
42+
text-align: center;
43+
}
44+
45+
#tracy-debug .nette-RoutingPanel-grid-inner .nette-RoutingPanel-grid-inner {
46+
background: #23180007;
47+
box-shadow: 0 1px 20px 0px #00000040;
48+
border-right: 8px solid #0000002e;
49+
}
50+
51+
#tracy-debug .nette-RoutingPanel-grid-columns > div {
52+
border-bottom: 1px solid #95770026;
53+
border-right: 1px solid #95770026;
54+
padding: 2px 5px;
55+
}
56+
57+
#tracy-debug .nette-RoutingPanel-status-yes {
2258
background: #BDE678 !important;
2359
}
2460

25-
#tracy-debug .nette-RoutingPanel .may td {
61+
#tracy-debug .nette-RoutingPanel-status-may {
2662
background: #C1D3FF !important;
2763
}
2864

29-
#tracy-debug .nette-RoutingPanel .error td {
65+
#tracy-debug .nette-RoutingPanel-status-error {
3066
background: #ffd2c3 !important;
3167
}
3268

33-
#tracy-debug .nette-RoutingPanel td.symbol {
34-
text-align: center;
35-
}
36-
37-
#tracy-debug .nette-RoutingPanel td:first-child {
38-
width: 20px;
69+
#tracy-debug .nette-RoutingPanel-symbol {
70+
text-align: right;
3971
}
4072

41-
#tracy-debug .nette-RoutingPanel td:nth-child(2) {
42-
white-space: nowrap;
73+
#tracy-debug .nette-RoutingPanel .tracy-dump.tracy-dump {
74+
padding: 0;
75+
margin: 0;
76+
border: none;
4377
}
4478

4579
#tracy-debug .nette-RoutingPanel pre, #tracy-debug .nette-RoutingPanel code {
@@ -72,54 +106,75 @@ use Tracy\Helpers;
72106
</div>
73107

74108
<div class="tracy-inner-container">
75-
<?php if (empty($routers)): ?>
76-
<p>No routers defined.</p>
109+
<?php if (empty($routes)): ?>
110+
<p>No routes defined.</p>
77111

78112
<?php else: ?>
79-
<table>
80-
<thead>
81-
<tr>
82-
<th></th>
83-
<th>Mask / Class</th>
84-
<th>Defaults</th>
85-
<?php if ($hasModule): ?><th>Module</th><?php endif ?>
86-
<th>Matched as</th>
87-
</tr>
88-
</thead>
89-
90-
<tbody>
91-
<?php foreach ($routers as $router): ?>
92-
<tr class="<?= $router->matched ?>" style="border-width: <?=($router->gutterTop ?? 0) * 3?>px 0 <?=($router->gutterBottom ?? 0) * 3?>px <?=$router->level * 6?>px">
93-
<td class="symbol" title="<?= Helpers::escapeHtml(['yes' => 'matched as first', 'may' => 'suitable but not matched as first', 'no' => '', 'oneway' => 'one-way', 'error' => 'error'][$router->matched]) ?>"
94-
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$router->matched] ?></td>
95-
96-
<td><code title="<?= Helpers::escapeHtml($router->class) ?>"><?=
97-
$router->path === '' ? '' : '<small>' . Helpers::escapeHtml($router->path) . '</small>',
98-
isset($router->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], Helpers::escapeHtml($router->mask)) : str_replace('\\', '<wbr>\\', Helpers::escapeHtml($router->class))
99-
?></code></td>
100-
101-
<td><code>
102-
<?php foreach ($router->defaults as $key => $value): ?>
103-
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br />' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
104-
<?php endforeach ?>
105-
</code></td>
113+
<div class="nette-RoutingPanel-grid">
114+
<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-grid-header">
115+
<div></div>
116+
<div>Mask / Class</div>
117+
<div>Defaults</div>
118+
<div>Matched as</div>
119+
</div>
120+
<?php
121+
122+
$show = function ($info, $path = '') use (&$show) {
123+
if (is_array($info)) {
124+
?>
125+
<div class="nette-RoutingPanel-grid-inner">
126+
<?php if ($info['domain'] || $info['module']): ?>
127+
<div class="nette-RoutingPanel-grid-group-header">
128+
<?= $info['domain'] ? 'domain = ' . Helpers::escapeHtml($info['domain']) : '' ?>
129+
<?= $info['module'] ? ' module = ' . Helpers::escapeHtml($info['module']) : '' ?>
130+
</div>
131+
<?php endif ?>
132+
<?php
133+
$path .= $info['path'];
134+
foreach ($info['routes'] as $route) {
135+
$show($route, $path);
136+
}
137+
?>
138+
</div>
139+
<?php
140+
return;
141+
}
106142

107-
<?php if ($hasModule): ?><td><code><?= Helpers::escapeHtml($router->module) ?></code></td><?php endif ?>
143+
$route = $info;
144+
?>
145+
<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-status-<?= $route->matched ?>">
146+
<div class="nette-RoutingPanel-symbol" title="<?= Helpers::escapeHtml(['yes' => 'matched as first', 'may' => 'suitable but not matched as first', 'no' => '', 'oneway' => 'one-way', 'error' => 'error'][$route->matched]) ?>"
147+
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$route->matched] ?></div>
148+
149+
<div><code title="<?= Helpers::escapeHtml($route->class) ?>"><?=
150+
$path === '' ? '' : '<small>' . Helpers::escapeHtml($path) . '</small>',
151+
isset($route->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], Helpers::escapeHtml($route->mask)) : str_replace('\\', '<wbr>\\', Helpers::escapeHtml($route->class))
152+
?></code></div>
153+
154+
<div><code>
155+
<?php foreach ($route->defaults as $key => $value): ?>
156+
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
157+
<?php endforeach ?>
158+
</code></div>
108159

109-
<td><?php if ($router->params): ?><code>
110-
<?php $params = $router->params; ?>
160+
<div><?php if ($route->params): ?><code>
161+
<?php $params = $route->params; ?>
111162
<?php if (isset($params[Presenter::PresenterKey])): ?>
112163
<strong><?= Helpers::escapeHtml($params['presenter'] . ':' . (isset($params[Presenter::ActionKey]) ? $params[Presenter::ActionKey] : Presenter::DefaultAction)) ?></strong><br />
113164
<?php unset($params[Presenter::PresenterKey], $params[Presenter::ActionKey]) ?>
114165
<?php endif ?>
115166
<?php foreach ($params as $key => $value): ?>
116-
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br />' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
167+
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
117168
<?php endforeach ?>
118-
</code><?php elseif ($router->error): ?><strong><?= Helpers::escapeHtml($router->error->getMessage()) ?></strong><?php endif ?></td>
119-
</tr>
120-
<?php endforeach ?>
121-
</tbody>
122-
</table>
169+
</code><?php elseif ($route->error): ?><strong><?= Helpers::escapeHtml($route->error->getMessage()) ?></strong><?php endif ?></div>
170+
</div>
171+
<?php
172+
};
173+
174+
$show($routes);
175+
176+
?>
177+
</div>
123178
<?php endif ?>
124179
</div>
125180
</div>

0 commit comments

Comments
 (0)