1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Alxarafe. Development of PHP applications in a flash! |
4
|
|
|
* Copyright (C) 2018-2019 Alxarafe <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Alxarafe\Core\Providers; |
8
|
|
|
|
9
|
|
|
use Alxarafe\Core\DebugBarCollectors\PhpCollector; |
10
|
|
|
use Alxarafe\Core\DebugBarCollectors\TranslatorCollector; |
11
|
|
|
use Alxarafe\Core\Helpers\Utils\ClassUtils; |
12
|
|
|
use DebugBar\Bridge\MonologCollector; |
13
|
|
|
use DebugBar\DataCollector\MessagesCollector; |
14
|
|
|
use DebugBar\DebugBarException; |
15
|
|
|
use DebugBar\JavascriptRenderer; |
16
|
|
|
use DebugBar\StandardDebugBar; |
17
|
|
|
use Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DebugTool |
21
|
|
|
* |
22
|
|
|
* @package Alxarafe\Core\Providers |
23
|
|
|
*/ |
24
|
|
|
class DebugTool |
25
|
|
|
{ |
26
|
|
|
use Singleton { |
27
|
|
|
getInstance as getInstanceTrait; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The debug bar. |
32
|
|
|
* |
33
|
|
|
* @var StandardDebugBar |
34
|
|
|
*/ |
35
|
|
|
private $debugTool; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The JS renderer. |
39
|
|
|
* |
40
|
|
|
* @var JavascriptRenderer |
41
|
|
|
*/ |
42
|
|
|
private $jsRender; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The logger. |
46
|
|
|
* |
47
|
|
|
* @var Logger |
48
|
|
|
*/ |
49
|
|
|
private $logger; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* DebugTool constructor. |
53
|
|
|
*/ |
54
|
|
|
public function __construct() |
55
|
|
|
{ |
56
|
|
|
if (!isset($this->debugTool)) { |
57
|
|
|
$shortName = ClassUtils::getShortName($this, static::class); |
58
|
|
|
$this->initSingleton(); |
59
|
|
|
if (!defined('DEBUG')) { |
60
|
|
|
define('DEBUG', false); |
61
|
|
|
} |
62
|
|
|
$this->debugTool = new StandardDebugBar(); |
63
|
|
|
$this->startTimer($shortName, $shortName . ' DebugTool Constructor'); |
64
|
|
|
$this->logger = Logger::getInstance(); |
65
|
|
|
try { |
66
|
|
|
$logger = Logger::getInstance(); |
67
|
|
|
$this->debugTool->addCollector(new MonologCollector($logger->getLogger())); |
68
|
|
|
$this->debugTool->addCollector(new MessagesCollector('SQL')); |
69
|
|
|
$this->debugTool->addCollector(new PhpCollector()); |
70
|
|
|
$translator = Translator::getInstance(); |
71
|
|
|
$this->debugTool->addCollector(new TranslatorCollector($translator)); |
72
|
|
|
} catch (DebugBarException $e) { |
73
|
|
|
$this->logger::exceptionHandler($e); |
74
|
|
|
} |
75
|
|
|
$baseUrl = baseUrl('vendor/maximebf/debugbar/src/DebugBar/Resources'); |
76
|
|
|
$this->jsRender = $this->debugTool->getJavascriptRenderer($baseUrl, basePath()); |
77
|
|
|
$this->stopTimer($shortName); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Start a timer by name and message |
83
|
|
|
* |
84
|
|
|
* @param string $name |
85
|
|
|
* @param string $message |
86
|
|
|
*/ |
87
|
314 |
|
public function startTimer(string $name, string $message = 'Timer started'): void |
88
|
|
|
{ |
89
|
|
|
try { |
90
|
314 |
|
if (!$this->debugTool->getCollector('time')->/** @scrutinizer ignore-call */ |
91
|
314 |
|
hasStartedMeasure($name)) { |
92
|
314 |
|
$this->debugTool->getCollector('time')->/** @scrutinizer ignore-call */ |
93
|
314 |
|
startMeasure($name, $message); |
94
|
|
|
} else { |
95
|
314 |
|
$this->addMessage('messages', "Timer '" . $name . "' yet started and trying to start it again."); |
96
|
|
|
} |
97
|
|
|
} catch (DebugBarException $e) { |
98
|
|
|
Logger::getInstance()::exceptionHandler($e); |
99
|
|
|
} |
100
|
314 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Write a message in a channel (tab) of the debug bar. |
104
|
|
|
* |
105
|
|
|
* @param string $channel |
106
|
|
|
* @param string $message |
107
|
|
|
*/ |
108
|
1 |
|
public function addMessage(string $channel, string $message): void |
109
|
|
|
{ |
110
|
1 |
|
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0]; |
111
|
1 |
|
$caller['file'] = substr($caller['file'], strlen(basePath())); |
112
|
|
|
try { |
113
|
1 |
|
$this->debugTool->getCollector($channel)->/** @scrutinizer ignore-call */ |
114
|
1 |
|
addMessage($caller['file'] . ' (' . $caller['line'] . '): ' . $message); |
115
|
|
|
} catch (DebugBarException $e) { |
116
|
|
|
Logger::getInstance()::exceptionHandler($e); |
117
|
|
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Stop a timer by name. |
122
|
|
|
* |
123
|
|
|
* @param string $name |
124
|
|
|
*/ |
125
|
314 |
|
public function stopTimer(string $name): void |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
314 |
|
if ($this->debugTool->getCollector('time')->/** @scrutinizer ignore-call */ |
129
|
314 |
|
hasStartedMeasure($name)) { |
130
|
314 |
|
$this->debugTool->getCollector('time')->/** @scrutinizer ignore-call */ |
131
|
314 |
|
stopMeasure($name); |
132
|
|
|
} else { |
133
|
314 |
|
$this->addMessage('messages', "Timer '" . $name . "' not yet started and trying to stop it."); |
134
|
|
|
} |
135
|
|
|
} catch (DebugBarException $e) { |
136
|
|
|
Logger::getInstance()::exceptionHandler($e); |
137
|
|
|
} |
138
|
314 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return this instance. |
142
|
|
|
* |
143
|
|
|
* @return self |
144
|
|
|
*/ |
145
|
314 |
|
public static function getInstance(): self |
146
|
|
|
{ |
147
|
314 |
|
return self::getInstanceTrait(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return default values |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public static function getDefaultValues(): array |
156
|
|
|
{ |
157
|
|
|
// Not really needed |
158
|
|
|
return []; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Add a new exception to the debug bar. |
163
|
|
|
* |
164
|
|
|
* @param Exception $e |
165
|
|
|
*/ |
166
|
|
|
public function addException($e): void |
167
|
|
|
{ |
168
|
|
|
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[0]; |
169
|
|
|
$caller['file'] = substr($caller['file'], strlen(basePath())); |
170
|
|
|
try { |
171
|
|
|
$this->debugTool->getCollector('exceptions')->/** @scrutinizer ignore-call */ |
172
|
|
|
addException($e); |
173
|
|
|
} catch (DebugBarException $e) { |
174
|
|
|
Logger::getInstance()::exceptionHandler($e); |
175
|
|
|
} |
176
|
|
|
$this->logger::exceptionHandler($e); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return the internal debug instance. |
181
|
|
|
* |
182
|
|
|
* @return StandardDebugBar |
183
|
|
|
*/ |
184
|
|
|
public function getDebugTool(): StandardDebugBar |
185
|
|
|
{ |
186
|
|
|
return $this->debugTool; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return the render header needed when debug is enabled. Otherwise return an empty string. |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
5 |
|
public function getRenderHeader(): string |
195
|
|
|
{ |
196
|
5 |
|
if (constant('DEBUG')) { |
197
|
|
|
return $this->jsRender->renderHead(); |
198
|
|
|
} |
199
|
5 |
|
return ''; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return the render footer needed when debug is enabled. Otherwise return an empty string. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
5 |
|
public function getRenderFooter(): string |
208
|
|
|
{ |
209
|
5 |
|
if (constant('DEBUG')) { |
210
|
|
|
return $this->jsRender->render(); |
211
|
|
|
} |
212
|
5 |
|
return ''; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|