1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2022-2023 Rafael San José Tovar <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Core\Singletons; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Core\Singletons\DebugBarCollectors\PhpCollector; |
22
|
|
|
use Alxarafe\Core\Singletons\DebugBarCollectors\MonologCollector; |
23
|
|
|
use Alxarafe\Core\Singletons\DebugBarCollectors\TranslatorCollector; |
24
|
|
|
use DebugBar\DataCollector\DataCollectorInterface; |
25
|
|
|
use DebugBar\DataCollector\MessagesCollector; |
26
|
|
|
use DebugBar\DebugBar; |
27
|
|
|
use DebugBar\DebugBarException; |
28
|
|
|
use DebugBar\JavascriptRenderer; |
29
|
|
|
use DebugBar\StandardDebugBar; |
30
|
|
|
|
31
|
|
|
abstract class Debug |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Private logger instance |
35
|
|
|
* |
36
|
|
|
* @var Logger |
37
|
|
|
*/ |
38
|
|
|
// public static Logger $logger; |
39
|
|
|
/** |
40
|
|
|
* Private render instance |
41
|
|
|
* |
42
|
|
|
* @var JavascriptRenderer |
43
|
|
|
*/ |
44
|
|
|
private static JavascriptRenderer $render; |
45
|
|
|
/** |
46
|
|
|
* DebugBar instance |
47
|
|
|
* |
48
|
|
|
* @var StandardDebugBar |
49
|
|
|
*/ |
50
|
|
|
private static StandardDebugBar $debugBar; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* DebugTool constructor. |
54
|
|
|
* |
55
|
|
|
* @throws DebugBarException |
56
|
|
|
*/ |
57
|
|
|
public static function load() |
58
|
|
|
{ |
59
|
|
|
$shortName = 'Debug'; |
60
|
|
|
|
61
|
|
|
self::$debugBar = new StandardDebugBar(); |
62
|
|
|
self::startTimer($shortName, $shortName . ' DebugTool Constructor'); |
63
|
|
|
|
64
|
|
|
self::addCollector(new MessagesCollector('SQL')); |
65
|
|
|
self::addCollector(new PhpCollector()); |
66
|
|
|
self::addCollector(new MessagesCollector('Deprecated')); |
67
|
|
|
self::addCollector(new MonologCollector(Logger::getLogger())); |
68
|
|
|
self::addCollector(new TranslatorCollector()); |
69
|
|
|
|
70
|
|
|
$baseUrl = constant('VENDOR_URI') . '/maximebf/debugbar/src/DebugBar/Resources'; |
71
|
|
|
self::$render = self::getDebugBar()->getJavascriptRenderer($baseUrl, constant('BASE_DIR')); |
72
|
|
|
|
73
|
|
|
self::stopTimer($shortName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function addCollector(DataCollectorInterface $collector): DebugBar |
77
|
|
|
{ |
78
|
|
|
return self::$debugBar->addCollector($collector); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Initialize the timer |
83
|
|
|
* |
84
|
|
|
* @param string $name |
85
|
|
|
* @param string $message |
86
|
|
|
*/ |
87
|
|
|
public static function startTimer(string $name, string $message): void |
88
|
|
|
{ |
89
|
|
|
if (!isset(self::$debugBar)) { |
90
|
|
|
self::$debugBar = new StandardDebugBar(); |
91
|
|
|
} |
92
|
|
|
self::$debugBar['time']->startMeasure($name, $message); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the internal debug instance for get the html code. |
97
|
|
|
* |
98
|
|
|
* TODO: Analizar qué funciones harían falta para el html y retornar el html. |
99
|
|
|
* Tal y como está ahora mismo sería dependiente de DebugBar. DebugBar debería |
100
|
|
|
* de quedar TOTALMENTE encapsulado en esta clase. |
101
|
|
|
* |
102
|
|
|
* @return StandardDebugBar |
103
|
|
|
* @throws DebugBarException |
104
|
|
|
*/ |
105
|
|
|
public static function getDebugBar(): StandardDebugBar |
106
|
|
|
{ |
107
|
|
|
return self::$debugBar; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Stop the timer |
112
|
|
|
* |
113
|
|
|
* @param string $name |
114
|
|
|
*/ |
115
|
|
|
public static function stopTimer(string $name): void |
116
|
|
|
{ |
117
|
|
|
self::$debugBar['time']->stopMeasure($name); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the necessary calls to include the debug bar in the page header |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public static function getRenderHeader(): string |
126
|
|
|
{ |
127
|
|
|
if (constant('DEBUG') !== true) { |
128
|
|
|
return '<!-- Debug is disabled -->'; |
129
|
|
|
} |
130
|
|
|
return self::$render->renderHead(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets the necessary calls to include the debug bar in the page footer |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public static function getRenderFooter(): string |
139
|
|
|
{ |
140
|
|
|
if (constant('DEBUG') !== true) { |
141
|
|
|
return '<!-- Debug is disabled -->'; |
142
|
|
|
} |
143
|
|
|
return self::$render->render(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add an exception to the exceptions tab of the debug bar. |
148
|
|
|
* |
149
|
|
|
* TODO: addException is deprecated! |
150
|
|
|
* |
151
|
|
|
* @param $exception |
152
|
|
|
*/ |
153
|
|
|
public static function addException($exception): void |
154
|
|
|
{ |
155
|
|
|
if (constant('DEBUG') !== true) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0]; |
159
|
|
|
$caller['file'] = substr($caller['file'], strlen(BASE_DIR)); |
160
|
|
|
self::$debugBar['exceptions']->addException($exception); // Use addThrowable instead! |
|
|
|
|
161
|
|
|
// Logger::info('Exception: ' . $exception->getMessage()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function message(string $message): void |
165
|
|
|
{ |
166
|
|
|
self::addMessage('messages', $message); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public static function sqlMessage(string $message): void |
170
|
|
|
{ |
171
|
|
|
self::addMessage('SQL', $message); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Write a message in a channel (tab) of the debug bar. |
176
|
|
|
* |
177
|
|
|
* @param string $channel |
178
|
|
|
* @param string $message |
179
|
|
|
*/ |
180
|
|
|
private static function addMessage(string $channel, string $message): void |
181
|
|
|
{ |
182
|
|
|
if (!defined('DEBUG') || constant('DEBUG') !== true) { |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0]; |
186
|
|
|
$caller['file'] = substr($caller['file'], strlen(BASE_DIR)); |
187
|
|
|
self::$debugBar[$channel]->addMessage($caller['file'] . ' (' . $caller['line'] . '): ' . $message); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|