|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +require __DIR__ . '/../src/tracy.php'; |
| 6 | + |
| 7 | +use Tracy\Debugger; |
| 8 | +use Tracy\IBarPanel; |
| 9 | + |
| 10 | +// For security reasons, Tracy is visible only on localhost. |
| 11 | +// You may force Tracy to run in development mode by passing the Debugger::Development instead of Debugger::Detect. |
| 12 | +Debugger::enable(Debugger::Detect, __DIR__ . '/log'); |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Example: A normal (eager) panel — getPanel() is called during the request. |
| 17 | + */ |
| 18 | +class NormalPanel implements IBarPanel |
| 19 | +{ |
| 20 | + public function getTab(): string |
| 21 | + { |
| 22 | + return '<span title="Normal Panel">⚡ Normal</span>'; |
| 23 | + } |
| 24 | + |
| 25 | + public function getPanel(): string |
| 26 | + { |
| 27 | + return '<h1>Normal Panel</h1>' |
| 28 | + . '<div class="tracy-inner">' |
| 29 | + . '<p>This panel was rendered <strong>during the request</strong> (eager).</p>' |
| 30 | + . '<p>Time: ' . date('H:i:s') . '</p>' |
| 31 | + . '</div>'; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Example: A "heavy" panel that simulates expensive computation. |
| 38 | + * When registered with lazy: true, getPanel() is NOT called during the request. |
| 39 | + * Instead, it is rendered in the shutdown function and served via AJAX on click. |
| 40 | + */ |
| 41 | +class HeavyPanel implements IBarPanel |
| 42 | +{ |
| 43 | + public function getTab(): string |
| 44 | + { |
| 45 | + return '<span title="Heavy Panel (lazy)">🐢 Heavy</span>'; |
| 46 | + } |
| 47 | + |
| 48 | + public function getPanel(): string |
| 49 | + { |
| 50 | + // Simulate expensive operation (e.g., database profiling, API calls) |
| 51 | + usleep(500_000); // 500ms delay |
| 52 | + |
| 53 | + return '<h1>Heavy Panel (lazy loaded)</h1>' |
| 54 | + . '<div class="tracy-inner">' |
| 55 | + . '<p>This panel was rendered <strong>after the response</strong> (lazy).</p>' |
| 56 | + . '<p>It simulates a 500ms expensive computation.</p>' |
| 57 | + . '<p>Time: ' . date('H:i:s') . '</p>' |
| 58 | + . '<table><tr><th>Key</th><th>Value</th></tr>' |
| 59 | + . '<tr><td>PHP Version</td><td>' . PHP_VERSION . '</td></tr>' |
| 60 | + . '<tr><td>Memory Peak</td><td>' . number_format(memory_get_peak_usage() / 1024 / 1024, 2) . ' MB</td></tr>' |
| 61 | + . '<tr><td>Extensions</td><td>' . count(get_loaded_extensions()) . ' loaded</td></tr>' |
| 62 | + . '</table>' |
| 63 | + . '</div>'; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +/** |
| 69 | + * Example: Another lazy panel showing database-like profiling info. |
| 70 | + */ |
| 71 | +class DatabasePanel implements IBarPanel |
| 72 | +{ |
| 73 | + public function getTab(): string |
| 74 | + { |
| 75 | + return '<span title="Database Panel (lazy)">🗄️ DB</span>'; |
| 76 | + } |
| 77 | + |
| 78 | + public function getPanel(): string |
| 79 | + { |
| 80 | + usleep(300_000); // 300ms delay |
| 81 | + |
| 82 | + $queries = [ |
| 83 | + ['SELECT * FROM users WHERE id = 1', '0.5ms'], |
| 84 | + ['SELECT * FROM posts WHERE user_id = 1 ORDER BY created_at DESC LIMIT 10', '2.1ms'], |
| 85 | + ['UPDATE users SET last_login = NOW() WHERE id = 1', '0.3ms'], |
| 86 | + ]; |
| 87 | + |
| 88 | + $html = '<h1>Database Panel (lazy loaded)</h1>' |
| 89 | + . '<div class="tracy-inner">' |
| 90 | + . '<p>Simulated database queries — rendered lazily after the response was sent.</p>' |
| 91 | + . '<table><tr><th>#</th><th>Query</th><th>Time</th></tr>'; |
| 92 | + |
| 93 | + foreach ($queries as $i => [$query, $time]) { |
| 94 | + $html .= '<tr><td>' . ($i + 1) . '</td><td><code>' . htmlspecialchars($query) . '</code></td><td>' . $time . '</td></tr>'; |
| 95 | + } |
| 96 | + |
| 97 | + $html .= '</table></div>'; |
| 98 | + return $html; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +// Register panels: |
| 104 | +// Normal panel (eager) — rendered during the request |
| 105 | +Debugger::getBar()->addPanel(new NormalPanel, 'example-normal'); |
| 106 | + |
| 107 | +// Heavy panel — lazy: true means getPanel() is deferred to shutdown function |
| 108 | +Debugger::getBar()->addPanel(new HeavyPanel, 'example-heavy', lazy: true); |
| 109 | + |
| 110 | +// Database panel — also lazy |
| 111 | +Debugger::getBar()->addPanel(new DatabasePanel, 'example-database', lazy: true); |
| 112 | + |
| 113 | +?> |
| 114 | +<!DOCTYPE html><html class=arrow><link rel="stylesheet" href="assets/style.css"> |
| 115 | + |
| 116 | +<h1>Tracy: Lazy Panel Loading Demo</h1> |
| 117 | + |
| 118 | +<h2>How it works</h2> |
| 119 | +<p>This demo shows the <code>lazy: true</code> parameter for <code>Debugger::getBar()->addPanel()</code>.</p> |
| 120 | + |
| 121 | +<ul> |
| 122 | + <li><strong>⚡ Normal</strong> — A regular panel. Its <code>getPanel()</code> is called during the request.</li> |
| 123 | + <li><strong>🐢 Heavy</strong> — A lazy panel simulating a 500ms expensive operation. Content loads on click.</li> |
| 124 | + <li><strong>🗄️ DB</strong> — A lazy panel simulating database query profiling. Content loads on click.</li> |
| 125 | +</ul> |
| 126 | + |
| 127 | +<h2>Usage</h2> |
| 128 | +<pre><code>// Register a lazy panel — getPanel() is NOT called during the request |
| 129 | +Debugger::getBar()->addPanel(new MyExpensivePanel, 'my-panel', lazy: true); |
| 130 | +</code></pre> |
| 131 | + |
| 132 | +<p>Lazy panels have their <code>getTab()</code> called normally (so the tab is always visible), |
| 133 | +but <code>getPanel()</code> is deferred to a shutdown function. The content is stored in the session |
| 134 | +and fetched via AJAX when you click or hover over the panel tab.</p> |
| 135 | + |
| 136 | +<p>This is useful for panels that perform expensive operations like database profiling, |
| 137 | +API call logging, or heavy data analysis — they won't slow down your page response time.</p> |
| 138 | + |
| 139 | +<?php |
| 140 | + |
| 141 | +if (Debugger::$productionMode) { |
| 142 | + echo '<p><b>For security reasons, Tracy is visible only on localhost. Look into the source code to see how to enable Tracy.</b></p>'; |
| 143 | +} |
0 commit comments