Панель отладки: не перекрывает содержимое, показывает маршрут, пользователя и все запросы
This commit is contained in:
1 parent
b8af28c181
commit
18dcae02d1
6 files changed
+156
-4
No files matched your search
@@ -41,6 +41,9 @@ final class Router
|
||||
/** Защита от повторного входа: файл маршрутов сам может спросить адрес */
|
||||
private static bool $booting = false;
|
||||
|
||||
/** Маршрут, который обслуживает текущий запрос — панели отладки нужно его имя */
|
||||
private static ?Route $current = null;
|
||||
|
||||
/**
|
||||
* Подключает файл маршрутов. Файл возвращает функцию, принимающую роутер.
|
||||
*/
|
||||
@@ -177,6 +180,8 @@ final class Router
|
||||
continue;
|
||||
}
|
||||
|
||||
self::$current = $route;
|
||||
|
||||
return $this->run($route, $request, $params);
|
||||
}
|
||||
|
||||
@@ -228,6 +233,16 @@ final class Router
|
||||
return $base . self::url($name, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Маршрут, который обслуживает текущий запрос, — null вне HTTP (консоль,
|
||||
* воркер) или если он ещё не найден (запрос не дошёл до dispatch()).
|
||||
* Нужен панели отладки: без этого показать имя маршрута было бы неоткуда.
|
||||
*/
|
||||
public static function current(): ?Route
|
||||
{
|
||||
return self::$current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Есть ли такой маршрут — вьюхе иногда нужно спросить, не падая.
|
||||
*/
|
||||
|
||||
@@ -18,6 +18,7 @@ declare(strict_types=1);
|
||||
|
||||
use Rsgrinko\Proton\Models\UserNotification;
|
||||
use Rsgrinko\Proton\Support\Config;
|
||||
use Rsgrinko\Proton\Support\Profiler;
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
$bare = $bare ?? false;
|
||||
@@ -244,14 +245,23 @@ $unread = $viewer->isGuest() ? 0 : UserNotification::unreadFor($viewer->id());
|
||||
|
||||
.bulk-bar { margin-bottom: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; }
|
||||
|
||||
/* Панель отладки: видна только при APP_DEBUG и не мешает содержимому */
|
||||
/* Панель отладки: видна только при APP_DEBUG. Свёрнутая, она занимает
|
||||
только строку summary — но position:fixed есть у неё всегда, поэтому
|
||||
main.has-profiler ниже держит для этой строки готовое место, а не
|
||||
перекрывает последнюю строку таблицы или кнопку внизу страницы */
|
||||
.profiler { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; max-height: 60vh; overflow-y: auto;
|
||||
background: var(--panel); border-top: 1px solid var(--border); padding: 6px 12px; font-size: 12px; }
|
||||
.profiler summary { cursor: pointer; color: var(--muted); }
|
||||
.profiler[open] summary { border-bottom: 1px solid var(--border); padding-bottom: 6px; margin-bottom: 6px; }
|
||||
.profiler .warn { color: var(--warn); }
|
||||
.profiler h3 { font-size: 12px; margin: 10px 0 4px; }
|
||||
.profiler table { margin-bottom: 4px; }
|
||||
.profiler td { padding: 2px 6px; border: none; vertical-align: top; }
|
||||
.profiler td.count { white-space: nowrap; color: var(--muted); }
|
||||
.profiler details { margin-top: 8px; }
|
||||
.profiler details summary { color: var(--text); }
|
||||
|
||||
main.has-profiler { padding-bottom: 34px; }
|
||||
|
||||
a.sort { color: inherit; text-decoration: none; white-space: nowrap; }
|
||||
a.sort:hover { text-decoration: underline; }
|
||||
@@ -417,7 +427,8 @@ $unread = $viewer->isGuest() ? 0 : UserNotification::unreadFor($viewer->id());
|
||||
</header>
|
||||
<?php } ?>
|
||||
|
||||
<main<?= $bare ? ' class="auth"' : '' ?>>
|
||||
<?php $mainClass = trim(($bare ? 'auth ' : '') . (Profiler::enabled() ? 'has-profiler' : '')); ?>
|
||||
<main<?= $mainClass !== '' ? ' class="' . $mainClass . '"' : '' ?>>
|
||||
<?php foreach ($flash as $item) { ?>
|
||||
<div class="flash <?= View::e($item['type']) ?>"><?= View::e($item['message']) ?></div>
|
||||
<?php } ?>
|
||||
|
||||
@@ -4,13 +4,21 @@ declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Панель отладки внизу страницы: сколько запросов к базе, сколько времени,
|
||||
* что повторялось. Показывается только при APP_DEBUG — на бою её нет вовсе.
|
||||
* что повторялось, какой маршрут и кто сейчас смотрит. Показывается только при
|
||||
* APP_DEBUG — на бою её нет вовсе.
|
||||
*
|
||||
* Повторы наверху нарочно: один и тот же SELECT двадцать раз — это N+1,
|
||||
* и заметить его надо раньше, чем он доедет до боя.
|
||||
*
|
||||
* Свёрнутая, панель занимает только строку `<summary>` — но она `position:
|
||||
* fixed`, поэтому место под неё каркас резервирует сам (`main.has-profiler`
|
||||
* в layouts/main.php), иначе она перекрывала бы последнюю строку таблицы
|
||||
* или кнопку внизу страницы.
|
||||
*/
|
||||
|
||||
use Rsgrinko\Proton\Http\Router;
|
||||
use Rsgrinko\Proton\Support\Profiler;
|
||||
use Rsgrinko\Proton\Support\Str;
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
if (!Profiler::enabled()) {
|
||||
@@ -19,17 +27,43 @@ if (!Profiler::enabled()) {
|
||||
|
||||
$repeats = Profiler::repeats(3);
|
||||
$slowest = Profiler::slowest(5);
|
||||
$route = Router::current();
|
||||
$viewer = View::viewer();
|
||||
?>
|
||||
<details class="profiler">
|
||||
<summary>
|
||||
база: <?= Profiler::count() ?> запр. / <?= View::e((string) Profiler::time()) ?> мс
|
||||
· страница: <?= View::e((string) Profiler::elapsed()) ?> мс
|
||||
· память: <?= View::e(\Rsgrinko\Proton\Support\Str::bytes(memory_get_peak_usage(true))) ?>
|
||||
· память: <?= View::e(Str::bytes(memory_get_peak_usage(true))) ?>
|
||||
<?php if ($route?->name !== null) { ?>
|
||||
· <?= View::e((string) $route->name) ?>
|
||||
<?php } ?>
|
||||
<?php if ($repeats !== []) { ?>
|
||||
· <span class="warn">повторов: <?= count($repeats) ?></span>
|
||||
<?php } ?>
|
||||
</summary>
|
||||
|
||||
<h3>Запрос</h3>
|
||||
<dl class="props">
|
||||
<dt>Маршрут</dt>
|
||||
<dd><?= $route !== null ? View::e((string) ($route->name ?? '(без имени)') . ' — ' . $route->pattern) : '—' ?></dd>
|
||||
|
||||
<dt>Прослойки</dt>
|
||||
<dd><?= $route !== null && $route->middleware !== [] ? View::e(implode(', ', $route->middleware)) : '—' ?></dd>
|
||||
|
||||
<dt>Кто смотрит</dt>
|
||||
<dd>
|
||||
<?php if ($viewer->isGuest()) { ?>
|
||||
гость
|
||||
<?php } else { ?>
|
||||
<?= View::e($viewer->name()) ?> (id <?= $viewer->id() ?>)
|
||||
<?php if (View::isImpersonating()) { ?>
|
||||
— вместо <?= View::e((string) View::impersonator()?->login) ?>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</dd>
|
||||
</dl>
|
||||
|
||||
<?php if ($repeats !== []) { ?>
|
||||
<h3>Повторяющиеся запросы</h3>
|
||||
|
||||
@@ -50,9 +84,27 @@ $slowest = Profiler::slowest(5);
|
||||
<?php foreach ($slowest as $query) { ?>
|
||||
<tr>
|
||||
<td class="count"><?= View::e((string) $query['ms']) ?> мс</td>
|
||||
<td class="count"><?= (int) $query['rows'] ?> стр.</td>
|
||||
<td class="mono"><?= View::e(mb_substr($query['sql'], 0, 300)) ?></td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</table>
|
||||
<?php } ?>
|
||||
|
||||
<?php if (Profiler::count() > 0) { ?>
|
||||
<details>
|
||||
<summary>Все запросы по порядку (<?= Profiler::count() ?>)</summary>
|
||||
|
||||
<table>
|
||||
<?php foreach (Profiler::queries() as $index => $query) { ?>
|
||||
<tr>
|
||||
<td class="count"><?= $index + 1 ?></td>
|
||||
<td class="count"><?= View::e((string) $query['ms']) ?> мс</td>
|
||||
<td class="count"><?= (int) $query['rows'] ?> стр.</td>
|
||||
<td class="mono"><?= View::e(mb_substr($query['sql'], 0, 300)) ?></td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</table>
|
||||
</details>
|
||||
<?php } ?>
|
||||
</details>
|
||||
Reference in new issue
Block a user