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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2021 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PDO\PDOCollector;
use DebugBar\DataCollector\PDO\TraceablePDO;
use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\JavascriptRenderer;
use DebugBar\StandardDebugBar;
use Fisharebest\Webtrees\DebugBar\ViewCollector;
use PDO;
/**
* A static wrapper for maximebf/php-debugbar.
*
* @see https://github.com/maximebf/php-debugbar
*/
class DebugBar
{
/** @var StandardDebugbar|null */
private static $debugbar;
/** @var JavascriptRenderer */
private static $renderer;
/**
* Initialize the Debugbar.
*
* @param string $base_url - Will only be valid on dev builds.
*
* @return void
*/
public static function enable(string $base_url): void
{
self::$debugbar = new StandardDebugBar();
self::$debugbar->addCollector(new ViewCollector());
$base_url .= '/vendor/maximebf/debugbar/src/DebugBar/Resources/';
self::$renderer = self::$debugbar->getJavascriptRenderer($base_url);
}
/**
* Initialize the PDO collector.
*
* @param PDO $pdo
*
* @return PDO
*/
public static function initPDO(PDO $pdo): PDO
{
if (self::$debugbar instanceof StandardDebugBar) {
$traceable_pdo = new TraceablePDO($pdo);
self::$debugbar->addCollector(new PDOCollector($traceable_pdo));
}
return $pdo;
}
/**
* Render the body content.
*
* @return string
*/
public static function render(): string
{
if (self::$debugbar instanceof StandardDebugBar) {
return self::$renderer->render();
}
return '';
}
/**
* Render the head content.
*
* @return string
*/
public static function renderHead(): string
{
if (self::$debugbar instanceof StandardDebugBar) {
return self::$renderer->renderHead();
}
return '';
}
/**
* For POST/redirect responses, we "stack" the data onto the next GET request.
*
* @return void
*/
public static function stackData(): void
{
if (self::$debugbar instanceof StandardDebugBar) {
self::$debugbar->stackData();
}
}
/**
* For JSON responses, we send the data in HTTP headers.
*
* @return void
*/
public static function sendDataInHeaders(): void
{
if (self::$debugbar instanceof StandardDebugBar) {
self::$debugbar->sendDataInHeaders();
}
}
/**
* Add a message.
*
* @param string $message
* @param string $label
* @param bool $isString
*
* @return void
*/
public static function addMessage(string $message, $label = 'info', $isString = true): void
{
if (self::$debugbar instanceof StandardDebugBar) {
$collector = self::$debugbar->getCollector('messages');
if ($collector instanceof MessagesCollector) {
$collector->addMessage($message, $label, $isString);
}
}
}
/**
* Start a timer.
*
* @param string $name
*
* @return void
*/
public static function startMeasure(string $name): void
{
if (self::$debugbar instanceof StandardDebugBar) {
$collector = self::$debugbar->getCollector('time');
if ($collector instanceof TimeDataCollector) {
$collector->startMeasure($name);
}
}
}
/**
* Stop a timer.
*
* @param string $name
*
* @return void
*/
public static function stopMeasure(string $name): void
{
if (self::$debugbar instanceof StandardDebugBar) {
$collector = self::$debugbar->getCollector('time');
if ($collector instanceof TimeDataCollector) {
$collector->stopMeasure($name);
}
}
}
/**
* Log an exception/throwable
*
* @param string $view
* @param mixed[] $data
*
* @return void
*/
public static function addView(string $view, array $data): void
{
if (self::$debugbar instanceof StandardDebugBar) {
$collector = self::$debugbar->getCollector('views');
if ($collector instanceof ViewCollector) {
$collector->addView($view, $data);
}
}
}
}
|