Skip to content

Commit b08ddd5

Browse files
committed
RoutingPanel: redesigned [Closes #285]
1 parent 9c27bc6 commit b08ddd5

File tree

2 files changed

+119
-80
lines changed

2 files changed

+119
-80
lines changed

src/Bridges/ApplicationTracy/RoutingPanel.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ 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;
@@ -57,7 +57,7 @@ public function __construct(
5757
*/
5858
public function getTab(): string
5959
{
60-
$this->analyse($this->router, $this->httpRequest);
60+
$this->routes = $this->analyse($this->router, $this->httpRequest);
6161
return Nette\Utils\Helpers::capture(function () {
6262
$matched = $this->matched;
6363
require __DIR__ . '/templates/RoutingPanel.tab.phtml';
@@ -72,9 +72,8 @@ public function getPanel(): string
7272
{
7373
return Nette\Utils\Helpers::capture(function () {
7474
$matched = $this->matched;
75-
$routers = $this->routers;
75+
$routes = $this->routes;
7676
$source = $this->source;
77-
$hasModule = (bool) array_filter($routers, function (\stdClass $rq): string { return $rq->module; });
7877
$url = $this->httpRequest->getUrl();
7978
$method = $this->httpRequest->getMethod();
8079
require __DIR__ . '/templates/RoutingPanel.panel.phtml';
@@ -88,18 +87,18 @@ public function getPanel(): string
8887
private function analyse(
8988
Routing\Router $router,
9089
?Nette\Http\IRequest $httpRequest,
91-
string $module = '',
92-
string $path = '',
9390
?\Closure $afterMatch = null,
94-
int $level = -1,
9591
int $flag = 0
96-
): void
97-
{
92+
) {
9893
$afterMatch = $afterMatch ?? function ($params) { return $params; };
9994

10095
if ($router instanceof Routing\RouteList) {
101-
$path .= $router->getPath();
102-
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
96+
$info = [
97+
'path' => $router->getPath(),
98+
'domain' => $router->getDomain(),
99+
'module' => ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : ''),
100+
'routes' => [],
101+
];
103102

104103
$httpRequest = $httpRequest
105104
? (new \ReflectionMethod($router, 'beforeMatch'))->invoke($router, $httpRequest)
@@ -112,21 +111,12 @@ private function analyse(
112111
return $afterMatch($params);
113112
};
114113

115-
$next = count($this->routers);
116114
$flags = $router->getFlags();
117115
foreach ($router->getRouters() as $i => $innerRouter) {
118-
$this->analyse($innerRouter, $httpRequest, $module, $path, $afterMatch, $level + 1, $flags[$i]);
119-
}
120-
121-
if ($info = $this->routers[$next] ?? null) {
122-
$info->gutterTop = abs(max(0, $level) - $info->level);
116+
$info['routes'][] = $this->analyse($innerRouter, $httpRequest, $afterMatch, $flags[$i]);
123117
}
124118

125-
if ($info = end($this->routers)) {
126-
$info->gutterBottom = abs(max(0, $level) - $info->level);
127-
}
128-
129-
return;
119+
return $info;
130120
}
131121

132122
$matched = $flag & Routing\RouteList::ONE_WAY ? 'oneway' : 'no';
@@ -146,15 +136,12 @@ private function analyse(
146136
}
147137
}
148138

149-
$this->routers[] = (object) [
150-
'level' => max(0, $level),
139+
return (object) [
151140
'matched' => $matched,
152141
'class' => get_class($router),
153142
'defaults' => $router instanceof Routing\Route || $router instanceof Routing\SimpleRouter ? $router->getDefaults() : [],
154143
'mask' => $router instanceof Routing\Route ? $router->getMask() : null,
155144
'params' => $params,
156-
'module' => rtrim($module, ':'),
157-
'path' => $path,
158145
'error' => $e,
159146
];
160147
}

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

Lines changed: 105 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,67 @@ 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-group-header {
32+
grid-column: 2 / span 3;
33+
font-size: 90%;
34+
}
35+
36+
#tracy-debug .nette-RoutingPanel-grid-header {
37+
color: #655E5E;
38+
background: #F4F3F1;
39+
font-size: 90%;
40+
font-weight: bold;
41+
}
42+
43+
#tracy-debug .nette-RoutingPanel-grid-inner .nette-RoutingPanel-grid-inner {
44+
background: #0000000d;
45+
padding: 2px 3px;
46+
}
47+
48+
#tracy-debug .nette-RoutingPanel-grid-columns > div {
49+
border-bottom: 1px solid #E6DFBF;
50+
border-right: 1px solid #E6DFBF;
51+
padding: 2px 5px;
52+
}
53+
54+
#tracy-debug .nette-RoutingPanel-status-yes {
2255
background: #BDE678 !important;
2356
}
2457

25-
#tracy-debug .nette-RoutingPanel .may td {
58+
#tracy-debug .nette-RoutingPanel-status-may {
2659
background: #C1D3FF !important;
2760
}
2861

29-
#tracy-debug .nette-RoutingPanel .error td {
62+
#tracy-debug .nette-RoutingPanel-status-error {
3063
background: #ffd2c3 !important;
3164
}
3265

33-
#tracy-debug .nette-RoutingPanel td.symbol {
34-
text-align: center;
66+
#tracy-debug .nette-RoutingPanel-symbol {
67+
text-align: right;
3568
}
3669

37-
#tracy-debug .nette-RoutingPanel td:first-child {
38-
width: 20px;
39-
}
40-
41-
#tracy-debug .nette-RoutingPanel td:nth-child(2) {
42-
white-space: nowrap;
70+
#tracy-debug .nette-RoutingPanel .tracy-dump.tracy-dump {
71+
padding: 0;
72+
margin: 0;
73+
border: none;
4374
}
4475

4576
#tracy-debug .nette-RoutingPanel pre, #tracy-debug .nette-RoutingPanel code {
@@ -72,54 +103,75 @@ use Tracy\Helpers;
72103
</div>
73104

74105
<div class="tracy-inner-container">
75-
<?php if (empty($routers)): ?>
76-
<p>No routers defined.</p>
106+
<?php if (empty($routes)): ?>
107+
<p>No routes defined.</p>
77108

78109
<?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>
110+
<div class="nette-RoutingPanel-grid">
111+
<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-grid-header">
112+
<div></div>
113+
<div>Mask / Class</div>
114+
<div>Defaults</div>
115+
<div>Matched as</div>
116+
</div>
117+
<?php
106118

107-
<?php if ($hasModule): ?><td><code><?= Helpers::escapeHtml($router->module) ?></code></td><?php endif ?>
119+
$show = function ($info, $path = '') use (&$show) {
120+
if (is_array($info)) {
121+
?>
122+
<div class="nette-RoutingPanel-grid-inner">
123+
<?php if ($info['domain'] || $info['module']): ?>
124+
<div class="nette-RoutingPanel-grid-group-header">
125+
<?= $info['domain'] ? 'domain = ' . Helpers::escapeHtml($info['domain']) : '' ?>
126+
<?= $info['module'] ? ' module = ' . Helpers::escapeHtml($info['module']) : '' ?>
127+
</div>
128+
<?php endif ?>
129+
<?php
130+
$path .= $info['path'];
131+
foreach ($info['routes'] as $route) {
132+
$show($route, $path);
133+
}
134+
?>
135+
</div>
136+
<?php
137+
return;
138+
}
108139

109-
<td><?php if ($router->params): ?><code>
110-
<?php $params = $router->params; ?>
140+
$route = $info;
141+
?>
142+
<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-status-<?= $route->matched ?>">
143+
<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]) ?>"
144+
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$route->matched] ?></div>
145+
146+
<div><code title="<?= Helpers::escapeHtml($route->class) ?>"><?=
147+
$path === '' ? '' : '<small>' . Helpers::escapeHtml($path) . '</small>',
148+
isset($route->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], Helpers::escapeHtml($route->mask)) : str_replace('\\', '<wbr>\\', Helpers::escapeHtml($route->class))
149+
?></code></div>
150+
151+
<div><code>
152+
<?php foreach ($route->defaults as $key => $value): ?>
153+
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
154+
<?php endforeach ?>
155+
</code></div>
156+
157+
<div><?php if ($route->params): ?><code>
158+
<?php $params = $route->params; ?>
111159
<?php if (isset($params[Presenter::PresenterKey])): ?>
112160
<strong><?= Helpers::escapeHtml($params['presenter'] . ':' . (isset($params[Presenter::ActionKey]) ? $params[Presenter::ActionKey] : Presenter::DefaultAction)) ?></strong><br />
113161
<?php unset($params[Presenter::PresenterKey], $params[Presenter::ActionKey]) ?>
114162
<?php endif ?>
115163
<?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]) ?>
164+
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
117165
<?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>
166+
</code><?php elseif ($route->error): ?><strong><?= Helpers::escapeHtml($route->error->getMessage()) ?></strong><?php endif ?></div>
167+
</div>
168+
<?php
169+
};
170+
171+
$show($routes);
172+
173+
?>
174+
</div>
123175
<?php endif ?>
124176
</div>
125177
</div>

0 commit comments

Comments
 (0)