|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Debugger; |
|
16
|
|
|
|
|
17
|
|
|
use DebugBar\DataCollector\RequestDataCollector; |
|
18
|
|
|
use DebugBar\DataCollector\TimeDataCollector; |
|
19
|
|
|
use DebugBar\DataCollector\MessagesCollector; |
|
20
|
|
|
use DebugBar\DataCollector\PhpInfoCollector; |
|
21
|
|
|
use DebugBar\DataCollector\MemoryCollector; |
|
22
|
|
|
use DebugBar\JavascriptRenderer; |
|
23
|
|
|
use DebugBar\DebugBarException; |
|
24
|
|
|
use DebugBar\DebugBar; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Debugger |
|
28
|
|
|
* @package Quantum\Debugger |
|
29
|
|
|
* @uses DebugBar |
|
30
|
|
|
*/ |
|
31
|
|
|
class Debugger |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Messages tab |
|
36
|
|
|
*/ |
|
37
|
|
|
const MESSAGES = 'messages'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Queries tab |
|
41
|
|
|
*/ |
|
42
|
|
|
const QUERIES = 'queries'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Routes tab |
|
46
|
|
|
*/ |
|
47
|
|
|
const ROUTES = 'routes'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Hooks tab |
|
51
|
|
|
*/ |
|
52
|
|
|
const HOOKS = 'hooks'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Mails tab |
|
56
|
|
|
*/ |
|
57
|
|
|
const MAILS = 'mails'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Store |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
private $store; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var Debugger |
|
67
|
|
|
*/ |
|
68
|
|
|
private static $instance; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* DebugBar instance |
|
72
|
|
|
* @var DebugBar |
|
73
|
|
|
*/ |
|
74
|
|
|
private $debugBar; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Assets url |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
private $assetsUrl = '/assets/DebugBar/Resources'; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Custom CSS |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
private $customCss = 'custom_debugbar.css'; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Debugger constructor. |
|
90
|
|
|
* @throws DebugBarException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __construct(DebuggerStore $store, DebugBar $debugBar, array $collectors = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->store = $store; |
|
|
|
|
|
|
95
|
|
|
$this->debugBar = $debugBar; |
|
96
|
|
|
|
|
97
|
|
|
foreach ($collectors as $collector) { |
|
98
|
|
|
$this->debugBar->addCollector($collector); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param DebuggerStore|null $store |
|
104
|
|
|
* @param DebugBar|null $debugBar |
|
105
|
|
|
* @param array|null $collectors |
|
106
|
|
|
* @return Debugger |
|
107
|
|
|
* @throws DebugBarException |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function getInstance(DebuggerStore $store = null, DebugBar $debugBar = null, ?array $collectors = []): Debugger |
|
110
|
|
|
{ |
|
111
|
|
|
if (self::$instance === null) { |
|
112
|
|
|
$debugBar = $debugBar ?? new DebugBar(); |
|
113
|
|
|
$store = $store ?? new DebuggerStore(); |
|
114
|
|
|
$collectors = $collectors ?: self::getDefaultCollectors(); |
|
115
|
|
|
|
|
116
|
|
|
self::$instance = new self($store, $debugBar, $collectors); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return self::$instance; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Checks if debug bar enabled |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function isEnabled(): bool |
|
127
|
|
|
{ |
|
128
|
|
|
return filter_var(config()->get('debug'), FILTER_VALIDATE_BOOLEAN); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
|
|
public function initStore() |
|
135
|
|
|
{ |
|
136
|
|
|
$this->store->init([ |
|
137
|
|
|
Debugger::MESSAGES, |
|
138
|
|
|
Debugger::QUERIES, |
|
139
|
|
|
Debugger::ROUTES, |
|
140
|
|
|
Debugger::HOOKS, |
|
141
|
|
|
Debugger::MAILS |
|
142
|
|
|
]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Adds data to the store cell |
|
147
|
|
|
* @param string $cell |
|
148
|
|
|
* @param string $level |
|
149
|
|
|
* @param mixed $data |
|
150
|
|
|
*/ |
|
151
|
|
|
public function addToStoreCell(string $cell, string $level, $data) |
|
152
|
|
|
{ |
|
153
|
|
|
if (!empty($data)) { |
|
154
|
|
|
$this->store->set($cell, [$level => $data]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $cell |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getStoreCell(string $cell): array |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->store->get($cell); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Clears the store cell |
|
169
|
|
|
* @param string $cell |
|
170
|
|
|
*/ |
|
171
|
|
|
public function clearStoreCell(string $cell) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->store->delete($cell); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function resetStore() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->store->flush(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Renders the debug bar |
|
186
|
|
|
* @return string |
|
187
|
|
|
* @throws DebugBarException |
|
188
|
|
|
*/ |
|
189
|
|
|
public function render(): string |
|
190
|
|
|
{ |
|
191
|
|
|
foreach ([self::MESSAGES, self::QUERIES, self::ROUTES, self::HOOKS, self::MAILS] as $tab) { |
|
192
|
|
|
$this->createTab($tab); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$renderer = $this->getRenderer(); |
|
196
|
|
|
|
|
197
|
|
|
return $renderer->renderHead() . $renderer->render(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Creates a tab |
|
202
|
|
|
* @param string $type |
|
203
|
|
|
* @throws DebugBarException |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function createTab(string $type) |
|
206
|
|
|
{ |
|
207
|
|
|
if (!$this->debugBar->hasCollector($type)) { |
|
208
|
|
|
$this->debugBar->addCollector(new MessagesCollector($type)); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$messages = $this->store->get($type); |
|
212
|
|
|
|
|
213
|
|
|
if (count($messages)) { |
|
214
|
|
|
foreach ($messages as $message) { |
|
215
|
|
|
$fn = key($message); |
|
216
|
|
|
$this->debugBar[$type]->$fn($message[$fn]); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Gets the renderer |
|
223
|
|
|
* @return JavascriptRenderer |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getRenderer(): JavascriptRenderer |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->debugBar |
|
228
|
|
|
->getJavascriptRenderer() |
|
229
|
|
|
->setBaseUrl(base_url() . $this->assetsUrl) |
|
230
|
|
|
->addAssets([$this->customCss], []); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return array |
|
235
|
|
|
*/ |
|
236
|
|
|
protected static function getDefaultCollectors(): array |
|
237
|
|
|
{ |
|
238
|
|
|
return [ |
|
239
|
|
|
new PhpInfoCollector(), |
|
240
|
|
|
new MessagesCollector(), |
|
241
|
|
|
new RequestDataCollector(), |
|
242
|
|
|
new TimeDataCollector(), |
|
243
|
|
|
new MemoryCollector(), |
|
244
|
|
|
]; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..