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