|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\DebugBar; |
|
4
|
|
|
|
|
5
|
|
|
use DebugBar\Bridge\MonologCollector; |
|
6
|
|
|
use DebugBar\DebugBar as BaseDebugBar; |
|
7
|
|
|
use DebugBar\DataCollector\MemoryCollector; |
|
8
|
|
|
use DebugBar\DataCollector\PDO\PDOCollector; |
|
9
|
|
|
use DebugBar\DataCollector\PDO\TraceablePDO; |
|
10
|
|
|
use DebugBar\DataCollector\PhpInfoCollector; |
|
11
|
|
|
use DebugBar\Storage\FileStorage; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
use LeKoala\DebugBar\Collector\DatabaseCollector; |
|
14
|
|
|
use LeKoala\DebugBar\Collector\SilverStripeCollector; |
|
15
|
|
|
use LeKoala\DebugBar\Collector\TimeDataCollector; |
|
16
|
|
|
use LeKoala\DebugBar\Extension\ControllerExtension; |
|
17
|
|
|
use LeKoala\DebugBar\Messages\LogFormatter; |
|
18
|
|
|
use LeKoala\DebugBar\Proxy\DatabaseProxy; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
use Monolog\Logger; |
|
21
|
|
|
use ReflectionObject; |
|
22
|
|
|
use SilverStripe\Admin\LeftAndMain; |
|
23
|
|
|
use SilverStripe\Control\Controller as BaseController; |
|
24
|
|
|
use SilverStripe\Control\Director; |
|
25
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
26
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
27
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
28
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
29
|
|
|
use SilverStripe\ORM\Connect\PDOConnector; |
|
30
|
|
|
use SilverStripe\ORM\DB; |
|
31
|
|
|
use SilverStripe\View\Requirements; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* A simple helper |
|
35
|
|
|
*/ |
|
36
|
|
|
class DebugBar |
|
37
|
|
|
{ |
|
38
|
|
|
use Configurable; |
|
39
|
|
|
use Injectable; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var DebugBar\DebugBar |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static $debugbar; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public static $bufferingEnabled = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var DebugBar\JavascriptRenderer |
|
53
|
|
|
*/ |
|
54
|
|
|
protected static $renderer; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected static $showQueries = false; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var HTTPRequest |
|
63
|
|
|
*/ |
|
64
|
|
|
protected static $request; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the Debug Bar instance |
|
68
|
|
|
* @return \DebugBar\StandardDebugBar |
|
69
|
|
|
* @throws Exception |
|
70
|
|
|
* @global array $databaseConfig |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getDebugBar() |
|
73
|
|
|
{ |
|
74
|
|
|
if (self::$debugbar !== null) { |
|
75
|
|
|
return self::$debugbar; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!Director::isDev() || self::isDisabled() || self::vendorNotInstalled() || |
|
79
|
|
|
self::notLocalIp() || Director::is_cli() || self::isDevUrl() || |
|
80
|
|
|
(self::isAdminUrl() && !self::config()->enabled_in_admin) |
|
81
|
|
|
) { |
|
82
|
|
|
self::$debugbar = false; // no need to check again |
|
|
|
|
|
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
self::initDebugBar(); |
|
87
|
|
|
|
|
88
|
|
|
if (!self::$debugbar) { |
|
89
|
|
|
throw new Exception("Failed to initialize the DebugBar"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return self::$debugbar; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Init the debugbar instance |
|
97
|
|
|
* |
|
98
|
|
|
* @global array $databaseConfig |
|
99
|
|
|
* @return DebugBar\StandardDebugBar |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function initDebugBar() |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
// Prevent multiple inits |
|
104
|
|
|
if (self::$debugbar) { |
|
105
|
|
|
return self::$debugbar; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
self::$debugbar = $debugbar = new BaseDebugBar; |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if (isset($_REQUEST['showqueries'])) { |
|
111
|
|
|
self::setShowQueries(true); |
|
112
|
|
|
echo "The queries above have been run before we started DebugBar"; |
|
113
|
|
|
echo '<hr>'; |
|
114
|
|
|
unset($_REQUEST['showqueries']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$debugbar->addCollector(new PhpInfoCollector); |
|
118
|
|
|
$debugbar->addCollector(new TimeDataCollector); |
|
119
|
|
|
$debugbar->addCollector(new MemoryCollector); |
|
120
|
|
|
|
|
121
|
|
|
$connector = DB::get_connector(); |
|
122
|
|
|
if (!self::config()->force_proxy && $connector instanceof PDOConnector) { |
|
123
|
|
|
// Use a little bit of magic to replace the pdo instance |
|
124
|
|
|
$refObject = new ReflectionObject($connector); |
|
125
|
|
|
$refProperty = $refObject->getProperty('pdoConnection'); |
|
126
|
|
|
$refProperty->setAccessible(true); |
|
127
|
|
|
$traceablePdo = new TraceablePDO($refProperty->getValue($connector)); |
|
128
|
|
|
$refProperty->setValue($connector, $traceablePdo); |
|
129
|
|
|
|
|
130
|
|
|
$debugbar->addCollector(new PDOCollector($traceablePdo)); |
|
131
|
|
|
} else { |
|
132
|
|
|
DB::set_conn($db = new DatabaseProxy(DB::get_conn())); |
|
133
|
|
|
$db->setShowQueries(self::getShowQueries()); |
|
134
|
|
|
$debugbar->addCollector(new DatabaseCollector); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Add message collector last so other collectors can send messages to the console using it |
|
138
|
|
|
$logger = Injector::inst()->get(LoggerInterface::class); |
|
139
|
|
|
$logCollector = new MonologCollector($logger, Logger::DEBUG, true, 'messages'); |
|
140
|
|
|
$logCollector->setFormatter(new LogFormatter); |
|
141
|
|
|
|
|
142
|
|
|
$debugbar->addCollector($logCollector); |
|
143
|
|
|
|
|
144
|
|
|
// Add some SilverStripe specific infos |
|
145
|
|
|
$debugbar->addCollector(new SilverStripeCollector); |
|
146
|
|
|
|
|
147
|
|
|
if (self::config()->enable_storage) { |
|
148
|
|
|
$debugbar->setStorage(new FileStorage(TEMP_FOLDER . '/debugbar')); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Since we buffer everything, why not enable all dev options ? |
|
152
|
|
|
if (self::config()->auto_debug) { |
|
153
|
|
|
$_REQUEST['debug'] = true; |
|
154
|
|
|
$_REQUEST['debug_request'] = true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (isset($_REQUEST['debug']) || isset($_REQUEST['debug_request'])) { |
|
158
|
|
|
self::$bufferingEnabled = true; |
|
159
|
|
|
ob_start(); // We buffer everything until we have called an action |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $debugbar; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public static function clearDebugBar() |
|
166
|
|
|
{ |
|
167
|
|
|
self::$debugbar = null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public static function getShowQueries() |
|
171
|
|
|
{ |
|
172
|
|
|
return self::$showQueries; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public static function setShowQueries($showQueries) |
|
176
|
|
|
{ |
|
177
|
|
|
self::$showQueries = $showQueries; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public static function includeRequirements() |
|
181
|
|
|
{ |
|
182
|
|
|
$debugbar = self::getDebugBar(); |
|
183
|
|
|
|
|
184
|
|
|
if (!$debugbar) { |
|
185
|
|
|
return; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// Already called |
|
189
|
|
|
if (self::$renderer) { |
|
190
|
|
|
return; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$renderer = $debugbar->getJavascriptRenderer(); |
|
194
|
|
|
|
|
195
|
|
|
// We don't need the true path since we are going to use Requirements API that appends the BASE_PATH |
|
196
|
|
|
$renderer->setBasePath(DEBUGBAR_DIR . '/assets'); |
|
197
|
|
|
$renderer->setBaseUrl(DEBUGBAR_DIR . '/assets'); |
|
198
|
|
|
|
|
199
|
|
|
$includeJquery = self::config()->include_jquery; |
|
200
|
|
|
// In CMS, jQuery is already included |
|
201
|
|
|
if (self::isAdminController()) { |
|
202
|
|
|
$includeJquery = false; |
|
203
|
|
|
} |
|
204
|
|
|
// If jQuery is already included, set to false |
|
205
|
|
|
$js = Requirements::backend()->getJavascript(); |
|
206
|
|
|
foreach ($js as $url) { |
|
207
|
|
|
$name = basename($url); |
|
208
|
|
|
if ($name == 'jquery.js' || $name == 'jquery.min.js') { |
|
209
|
|
|
$includeJquery = false; |
|
210
|
|
|
break; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if ($includeJquery) { |
|
215
|
|
|
$renderer->setEnableJqueryNoConflict(true); |
|
216
|
|
|
} else { |
|
217
|
|
|
$renderer->disableVendor('jquery'); |
|
218
|
|
|
$renderer->setEnableJqueryNoConflict(false); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if (DebugBar::config()->enable_storage) { |
|
222
|
|
|
$renderer->setOpenHandlerUrl('__debugbar'); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
foreach ($renderer->getAssets('css') as $cssFile) { |
|
226
|
|
|
Requirements::css(ltrim($cssFile, '/')); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
foreach ($renderer->getAssets('js') as $jsFile) { |
|
230
|
|
|
Requirements::javascript(ltrim($jsFile, '/')); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
self::$renderer = $renderer; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public static function renderDebugBar() |
|
237
|
|
|
{ |
|
238
|
|
|
if (!self::$renderer) { |
|
239
|
|
|
return; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// Requirements may have been cleared (CMS iframes...) or not set (Security...) |
|
243
|
|
|
$js = Requirements::backend()->getJavascript(); |
|
244
|
|
|
if (!array_key_exists('debugbar/assets/debugbar.js', $js)) { |
|
245
|
|
|
return; |
|
246
|
|
|
} |
|
247
|
|
|
$initialize = true; |
|
248
|
|
|
if (Director::is_ajax()) { |
|
249
|
|
|
$initialize = false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$script = self::$renderer->render($initialize); |
|
253
|
|
|
return $script; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Determine why DebugBar is disabled |
|
258
|
|
|
* |
|
259
|
|
|
* @return string |
|
260
|
|
|
*/ |
|
261
|
|
|
public static function whyDisabled() |
|
262
|
|
|
{ |
|
263
|
|
|
if (!Director::isDev()) { |
|
264
|
|
|
return 'Not in dev mode'; |
|
265
|
|
|
} |
|
266
|
|
|
if (self::isDisabled()) { |
|
267
|
|
|
return 'Disabled by a constant or configuration'; |
|
268
|
|
|
} |
|
269
|
|
|
if (self::vendorNotInstalled()) { |
|
270
|
|
|
return 'DebugBar is not installed in vendors'; |
|
271
|
|
|
} |
|
272
|
|
|
if (self::notLocalIp()) { |
|
273
|
|
|
return 'Not a local ip'; |
|
274
|
|
|
} |
|
275
|
|
|
if (Director::is_cli()) { |
|
276
|
|
|
return 'In CLI mode'; |
|
277
|
|
|
} |
|
278
|
|
|
if (self::isDevUrl()) { |
|
279
|
|
|
return 'Dev tools'; |
|
280
|
|
|
} |
|
281
|
|
|
if (self::isAdminUrl() && !self::config()->enabled_in_admin) { |
|
282
|
|
|
return 'In admin'; |
|
283
|
|
|
} |
|
284
|
|
|
return "I don't know why"; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public static function vendorNotInstalled() |
|
288
|
|
|
{ |
|
289
|
|
|
return !class_exists('DebugBar\\StandardDebugBar'); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
public static function notLocalIp() |
|
|
|
|
|
|
293
|
|
|
{ |
|
294
|
|
|
if (!self::config()->check_local_ip) { |
|
295
|
|
|
return false; |
|
296
|
|
|
} |
|
297
|
|
|
if (isset($_SERVER['REMOTE_ADDR'])) { |
|
298
|
|
|
return !in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1', '1')); |
|
299
|
|
|
} |
|
300
|
|
|
return false; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public static function isDisabled() |
|
304
|
|
|
{ |
|
305
|
|
|
if (getenv('DEBUGBAR_DISABLE') || static::config()->disabled) { |
|
306
|
|
|
return true; |
|
307
|
|
|
} |
|
308
|
|
|
return false; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
public static function isDevUrl() |
|
312
|
|
|
{ |
|
313
|
|
|
return strpos(self::getRequestUrl(), '/dev/') === 0; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
public static function isAdminUrl() |
|
317
|
|
|
{ |
|
318
|
|
|
return strpos(self::getRequestUrl(), '/admin/') === 0; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public static function isAdminController() |
|
322
|
|
|
{ |
|
323
|
|
|
if (Controller::curr()) { |
|
324
|
|
|
return Controller::curr() instanceof LeftAndMain; |
|
325
|
|
|
} |
|
326
|
|
|
return self::isAdminUrl(); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Avoid triggering data collection for open handler |
|
331
|
|
|
* |
|
332
|
|
|
* @return boolean |
|
333
|
|
|
*/ |
|
334
|
|
|
public static function isDebugBarRequest() |
|
335
|
|
|
{ |
|
336
|
|
|
if ($url = self::getRequestUrl()) { |
|
337
|
|
|
return strpos($url, '/__debugbar') === 0; |
|
338
|
|
|
} |
|
339
|
|
|
return true; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Get request url |
|
344
|
|
|
* |
|
345
|
|
|
* @return string |
|
346
|
|
|
*/ |
|
347
|
|
|
public static function getRequestUrl() |
|
|
|
|
|
|
348
|
|
|
{ |
|
349
|
|
|
if (isset($_REQUEST['url'])) { |
|
350
|
|
|
return $_REQUEST['url']; |
|
351
|
|
|
} |
|
352
|
|
|
if (isset($_SERVER['REQUEST_URI'])) { |
|
353
|
|
|
return $_SERVER['REQUEST_URI']; |
|
354
|
|
|
} |
|
355
|
|
|
return ''; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Helper to make code cleaner |
|
360
|
|
|
* |
|
361
|
|
|
* @param callable $callback |
|
362
|
|
|
*/ |
|
363
|
|
|
public static function withDebugBar($callback) |
|
364
|
|
|
{ |
|
365
|
|
|
if (self::getDebugBar() && !self::isDebugBarRequest()) { |
|
366
|
|
|
$callback(self::getDebugBar()); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Set the current request. Is provided by the DebugBarMiddleware. |
|
372
|
|
|
* |
|
373
|
|
|
* @param HTTPRequest $request |
|
374
|
|
|
*/ |
|
375
|
|
|
public static function setRequest(HTTPRequest $request) |
|
376
|
|
|
{ |
|
377
|
|
|
self::$request = $request; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Get the current request |
|
382
|
|
|
* |
|
383
|
|
|
* @return HTTPRequest |
|
384
|
|
|
*/ |
|
385
|
|
|
public static function getRequest() |
|
386
|
|
|
{ |
|
387
|
|
|
if (self::$request) { |
|
388
|
|
|
return self::$request; |
|
389
|
|
|
} |
|
390
|
|
|
// Fall back to trying from the global state |
|
391
|
|
|
if (Controller::has_curr()) { |
|
392
|
|
|
return Controller::curr()->getRequest(); |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
|
|
|
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..