Skip to content

Commit

Permalink
RoutingPanel: redesigned [Closes #285]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 6, 2023
1 parent 9c27bc6 commit b08ddd5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 80 deletions.
41 changes: 14 additions & 27 deletions src/Bridges/ApplicationTracy/RoutingPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ final class RoutingPanel implements Tracy\IBarPanel
/** @var Nette\Application\IPresenterFactory */
private $presenterFactory;

/** @var \stdClass[] */
private $routers = [];
/** @var array|\stdClass */
private $routes;

/** @var array|null */
private $matched;
Expand All @@ -57,7 +57,7 @@ public function __construct(
*/
public function getTab(): string
{
$this->analyse($this->router, $this->httpRequest);
$this->routes = $this->analyse($this->router, $this->httpRequest);
return Nette\Utils\Helpers::capture(function () {
$matched = $this->matched;
require __DIR__ . '/templates/RoutingPanel.tab.phtml';
Expand All @@ -72,9 +72,8 @@ public function getPanel(): string
{
return Nette\Utils\Helpers::capture(function () {
$matched = $this->matched;
$routers = $this->routers;
$routes = $this->routes;
$source = $this->source;
$hasModule = (bool) array_filter($routers, function (\stdClass $rq): string { return $rq->module; });
$url = $this->httpRequest->getUrl();
$method = $this->httpRequest->getMethod();
require __DIR__ . '/templates/RoutingPanel.panel.phtml';
Expand All @@ -88,18 +87,18 @@ public function getPanel(): string
private function analyse(
Routing\Router $router,
?Nette\Http\IRequest $httpRequest,
string $module = '',
string $path = '',
?\Closure $afterMatch = null,
int $level = -1,
int $flag = 0
): void
{
) {
$afterMatch = $afterMatch ?? function ($params) { return $params; };

if ($router instanceof Routing\RouteList) {
$path .= $router->getPath();
$module .= ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : '');
$info = [
'path' => $router->getPath(),
'domain' => $router->getDomain(),
'module' => ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : ''),
'routes' => [],
];

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

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

if ($info = $this->routers[$next] ?? null) {
$info->gutterTop = abs(max(0, $level) - $info->level);
$info['routes'][] = $this->analyse($innerRouter, $httpRequest, $afterMatch, $flags[$i]);
}

if ($info = end($this->routers)) {
$info->gutterBottom = abs(max(0, $level) - $info->level);
}

return;
return $info;
}

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

$this->routers[] = (object) [
'level' => max(0, $level),
return (object) [
'matched' => $matched,
'class' => get_class($router),
'defaults' => $router instanceof Routing\Route || $router instanceof Routing\SimpleRouter ? $router->getDefaults() : [],
'mask' => $router instanceof Routing\Route ? $router->getMask() : null,
'params' => $params,
'module' => rtrim($module, ':'),
'path' => $path,
'error' => $e,
];
}
Expand Down
158 changes: 105 additions & 53 deletions src/Bridges/ApplicationTracy/templates/RoutingPanel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,67 @@ use Tracy\Helpers;

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

#tracy-debug .nette-RoutingPanel tr {
border: #d6ceb0 0 solid;
#tracy-debug .nette-RoutingPanel-grid-inner,
#tracy-debug .nette-RoutingPanel-grid-columns {
grid-column: 1 / span 4;
display: grid;
grid-template-columns: subgrid;
}

#tracy-debug .nette-RoutingPanel .yes td {
#tracy-debug .nette-RoutingPanel-grid-columns:nth-child(2n) {
background: rgba(0,0,0,0.02);
}

#tracy-debug .nette-RoutingPanel-grid-group-header {
grid-column: 2 / span 3;
font-size: 90%;
}

#tracy-debug .nette-RoutingPanel-grid-header {
color: #655E5E;
background: #F4F3F1;
font-size: 90%;
font-weight: bold;
}

#tracy-debug .nette-RoutingPanel-grid-inner .nette-RoutingPanel-grid-inner {
background: #0000000d;
padding: 2px 3px;
}

#tracy-debug .nette-RoutingPanel-grid-columns > div {
border-bottom: 1px solid #E6DFBF;
border-right: 1px solid #E6DFBF;
padding: 2px 5px;
}

#tracy-debug .nette-RoutingPanel-status-yes {
background: #BDE678 !important;
}

#tracy-debug .nette-RoutingPanel .may td {
#tracy-debug .nette-RoutingPanel-status-may {
background: #C1D3FF !important;
}

#tracy-debug .nette-RoutingPanel .error td {
#tracy-debug .nette-RoutingPanel-status-error {
background: #ffd2c3 !important;
}

#tracy-debug .nette-RoutingPanel td.symbol {
text-align: center;
#tracy-debug .nette-RoutingPanel-symbol {
text-align: right;
}

#tracy-debug .nette-RoutingPanel td:first-child {
width: 20px;
}

#tracy-debug .nette-RoutingPanel td:nth-child(2) {
white-space: nowrap;
#tracy-debug .nette-RoutingPanel .tracy-dump.tracy-dump {
padding: 0;
margin: 0;
border: none;
}

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

<div class="tracy-inner-container">
<?php if (empty($routers)): ?>
<p>No routers defined.</p>
<?php if (empty($routes)): ?>
<p>No routes defined.</p>

<?php else: ?>
<table>
<thead>
<tr>
<th></th>
<th>Mask / Class</th>
<th>Defaults</th>
<?php if ($hasModule): ?><th>Module</th><?php endif ?>
<th>Matched as</th>
</tr>
</thead>

<tbody>
<?php foreach ($routers as $router): ?>
<tr class="<?= $router->matched ?>" style="border-width: <?=($router->gutterTop ?? 0) * 3?>px 0 <?=($router->gutterBottom ?? 0) * 3?>px <?=$router->level * 6?>px">
<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]) ?>"
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$router->matched] ?></td>

<td><code title="<?= Helpers::escapeHtml($router->class) ?>"><?=
$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>

<td><code>
<?php foreach ($router->defaults as $key => $value): ?>
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br />' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
<?php endforeach ?>
</code></td>
<div class="nette-RoutingPanel-grid">
<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-grid-header">
<div></div>
<div>Mask / Class</div>
<div>Defaults</div>
<div>Matched as</div>
</div>
<?php

<?php if ($hasModule): ?><td><code><?= Helpers::escapeHtml($router->module) ?></code></td><?php endif ?>
$show = function ($info, $path = '') use (&$show) {
if (is_array($info)) {
?>
<div class="nette-RoutingPanel-grid-inner">
<?php if ($info['domain'] || $info['module']): ?>
<div class="nette-RoutingPanel-grid-group-header">
<?= $info['domain'] ? 'domain = ' . Helpers::escapeHtml($info['domain']) : '' ?>
<?= $info['module'] ? ' module = ' . Helpers::escapeHtml($info['module']) : '' ?>
</div>
<?php endif ?>
<?php
$path .= $info['path'];
foreach ($info['routes'] as $route) {
$show($route, $path);
}
?>
</div>
<?php
return;
}

<td><?php if ($router->params): ?><code>
<?php $params = $router->params; ?>
$route = $info;
?>
<div class="nette-RoutingPanel-grid-columns nette-RoutingPanel-status-<?= $route->matched ?>">
<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]) ?>"
><?= ['yes' => '', 'may' => '', 'no' => '', 'oneway' => '', 'error' => ''][$route->matched] ?></div>

<div><code title="<?= Helpers::escapeHtml($route->class) ?>"><?=
$path === '' ? '' : '<small>' . Helpers::escapeHtml($path) . '</small>',
isset($route->mask) ? str_replace(['/', '-'], ['<wbr>/', '<wbr>-'], Helpers::escapeHtml($route->mask)) : str_replace('\\', '<wbr>\\', Helpers::escapeHtml($route->class))
?></code></div>

<div><code>
<?php foreach ($route->defaults as $key => $value): ?>
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
<?php endforeach ?>
</code></div>

<div><?php if ($route->params): ?><code>
<?php $params = $route->params; ?>
<?php if (isset($params[Presenter::PresenterKey])): ?>
<strong><?= Helpers::escapeHtml($params['presenter'] . ':' . (isset($params[Presenter::ActionKey]) ? $params[Presenter::ActionKey] : Presenter::DefaultAction)) ?></strong><br />
<?php unset($params[Presenter::PresenterKey], $params[Presenter::ActionKey]) ?>
<?php endif ?>
<?php foreach ($params as $key => $value): ?>
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br />' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
<?= Helpers::escapeHtml($key), '&nbsp;=&nbsp;', is_string($value) ? Helpers::escapeHtml($value) . '<br>' : Dumper::toHtml($value, [Dumper::COLLAPSE => true, Dumper::LIVE => true]) ?>
<?php endforeach ?>
</code><?php elseif ($router->error): ?><strong><?= Helpers::escapeHtml($router->error->getMessage()) ?></strong><?php endif ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</code><?php elseif ($route->error): ?><strong><?= Helpers::escapeHtml($route->error->getMessage()) ?></strong><?php endif ?></div>
</div>
<?php
};

$show($routes);

?>
</div>
<?php endif ?>
</div>
</div>

0 comments on commit b08ddd5

Please sign in to comment.