-
-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathBar.php
More file actions
176 lines (141 loc) · 4.16 KB
/
Bar.php
File metadata and controls
176 lines (141 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* This file is part of the Tracy (https://tracy.nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Tracy;
/**
* Debug Bar.
*/
class Bar
{
/** @var array<string, IBarPanel> */
private array $panels = [];
private bool $loaderRendered = false;
/** @var ?callable(string, string): int */
private $panelSorter;
/**
* Add custom panel.
* @return static
*/
public function addPanel(IBarPanel $panel, ?string $id = null): self
{
if ($id === null) {
$c = 0;
do {
$id = $panel::class . ($c++ ? "-$c" : '');
} while (isset($this->panels[$id]));
}
$this->panels[$id] = $panel;
return $this;
}
/**
* @param callable(string, string): int $sorter
*/
public function replacePanelSorter(callable $sorter): void
{
$this->panelSorter = $sorter;
}
/**
* Returns panel with given id
*/
public function getPanel(string $id): ?IBarPanel
{
return $this->panels[$id] ?? null;
}
/**
* Renders loading <script>
* @internal
*/
public function renderLoader(DeferredContent $defer): void
{
if (!$defer->isAvailable()) {
throw new \LogicException('Start session before Tracy is enabled.');
}
$this->loaderRendered = true;
$requestId = $defer->getRequestId();
$nonce = Helpers::getNonce();
$async = true;
require __DIR__ . '/assets/loader.phtml';
}
/**
* Renders debug bar.
*/
public function render(DeferredContent $defer): void
{
$redirectQueue = &$defer->getItems('redirect');
$requestId = $defer->getRequestId();
if (Helpers::isAjax()) {
if ($defer->isAvailable()) {
$defer->addSetup('Tracy.Debug.loadAjax', $this->renderPartial('ajax', '-ajax:' . $requestId));
}
} elseif (Helpers::isRedirect()) {
if ($defer->isAvailable()) {
$redirectQueue[] = ['content' => $this->renderPartial('redirect', '-r' . count($redirectQueue)), 'time' => time()];
}
} elseif (Helpers::isHtmlMode()) {
if (preg_match('#^Content-Length:#im', implode("\n", headers_list()))) {
Debugger::log(new \LogicException('Tracy cannot display the Bar because the Content-Length header is being sent'), Debugger::EXCEPTION);
}
$content = $this->renderPartial('main');
foreach (array_reverse($redirectQueue) as $item) {
$content['bar'] .= $item['content']['bar'];
$content['panels'] .= $item['content']['panels'];
}
$redirectQueue = null;
$content = '<div id=tracy-debug-bar>' . $content['bar'] . '</div>' . $content['panels'];
if ($this->loaderRendered) {
$defer->addSetup('Tracy.Debug.init', $content);
} else {
$nonce = Helpers::getNonce();
$async = false;
Debugger::removeOutputBuffers(false);
require __DIR__ . '/assets/loader.phtml';
}
}
}
private function renderPartial(string $type, string $suffix = ''): array
{
$panels = $this->renderPanels($suffix);
return [
'bar' => Helpers::capture(function () use ($type, $panels) {
require __DIR__ . '/assets/bar.phtml';
}),
'panels' => Helpers::capture(function () use ($type, $panels) {
require __DIR__ . '/assets/panels.phtml';
}),
];
}
private function renderPanels(string $suffix = ''): array
{
set_error_handler(function (int $severity, string $message, string $file, int $line) {
if (error_reporting() & $severity) {
throw new \ErrorException($message, 0, $severity, $file, $line);
}
});
$obLevel = ob_get_level();
$panels = [];
if($this->panelSorter !== null) {
uksort($this->panels, $this->panelSorter);
}
foreach ($this->panels as $id => $panel) {
$idHtml = preg_replace('#[^a-z0-9]+#i', '-', $id) . $suffix;
try {
$tab = (string) $panel->getTab();
$panelHtml = $tab ? $panel->getPanel() : null;
} catch (\Throwable $e) {
while (ob_get_level() > $obLevel) { // restore ob-level if broken
ob_end_clean();
}
$idHtml = "error-$idHtml";
$tab = "Error in $id";
$panelHtml = "<h1>Error: $id</h1><div class='tracy-inner'>" . nl2br(Helpers::escapeHtml($e)) . '</div>';
unset($e);
}
$panels[] = (object) ['id' => $idHtml, 'tab' => $tab, 'panel' => $panelHtml];
}
restore_error_handler();
return $panels;
}
}