@@ -400,7 +400,7 @@ |
||
400 | 400 | } |
401 | 401 | |
402 | 402 | /** |
403 | - * @param int|float $number |
|
403 | + * @param integer $number |
|
404 | 404 | * @param int $precision |
405 | 405 | * @param null|string $forceUnit |
406 | 406 | * |
@@ -7,34 +7,34 @@ discard block |
||
7 | 7 | * @package Xicrow\PhpDebug |
8 | 8 | */ |
9 | 9 | class Timer { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - public static $collection = []; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var boolean|string |
|
17 | - */ |
|
18 | - public static $currentItem = false; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - public static $runningItems = []; |
|
24 | - |
|
25 | - /** |
|
26 | - * Force the unit to display elapsed times in (MS|S|M|H|D|W) |
|
27 | - * |
|
28 | - * @var null|string |
|
29 | - */ |
|
30 | - public static $forceDisplayUnit = null; |
|
31 | - |
|
32 | - /** |
|
33 | - * Color threshold for output (5 => 'red', all items with values of 5 or higher will be red) |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - public static $colorThreshold = []; |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + public static $collection = []; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var boolean|string |
|
17 | + */ |
|
18 | + public static $currentItem = false; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + public static $runningItems = []; |
|
24 | + |
|
25 | + /** |
|
26 | + * Force the unit to display elapsed times in (MS|S|M|H|D|W) |
|
27 | + * |
|
28 | + * @var null|string |
|
29 | + */ |
|
30 | + public static $forceDisplayUnit = null; |
|
31 | + |
|
32 | + /** |
|
33 | + * Color threshold for output (5 => 'red', all items with values of 5 or higher will be red) |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + public static $colorThreshold = []; |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * @var bool |
@@ -77,396 +77,396 @@ discard block |
||
77 | 77 | self::$runningItems = []; |
78 | 78 | } |
79 | 79 | |
80 | - /** |
|
81 | - * @param string|null $key |
|
82 | - * @param array $data |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public static function add($key = null, $data = []) { |
|
87 | - // If no key is given |
|
88 | - if (is_null($key)) { |
|
89 | - // Set key to file and line |
|
90 | - $key = Debugger::getCalledFrom(2); |
|
91 | - } |
|
92 | - |
|
93 | - // If key is allready in use |
|
94 | - if (isset(self::$collection[$key])) { |
|
95 | - // Get original item |
|
96 | - $item = self::$collection[$key]; |
|
97 | - |
|
98 | - // Set new item count |
|
99 | - $itemCount = (isset($item['count']) ? ($item['count'] + 1) : 2); |
|
100 | - |
|
101 | - // Set correct key for the original item |
|
102 | - if (strpos($item['key'], '#') === false) { |
|
103 | - self::$collection[$key] = array_merge($item, [ |
|
104 | - 'key' => $key . ' #1', |
|
105 | - 'count' => $itemCount, |
|
106 | - ]); |
|
107 | - } else { |
|
108 | - self::$collection[$key] = array_merge($item, [ |
|
109 | - 'count' => $itemCount, |
|
110 | - ]); |
|
111 | - } |
|
112 | - |
|
113 | - // Set new key |
|
114 | - $key = $key . ' #' . $itemCount; |
|
115 | - } |
|
116 | - |
|
117 | - // Make sure various options are set |
|
118 | - if (!isset($data['key'])) { |
|
119 | - $data['key'] = $key; |
|
120 | - } |
|
121 | - if (!isset($data['parent'])) { |
|
122 | - $data['parent'] = self::$currentItem; |
|
123 | - } |
|
124 | - if (!isset($data['level'])) { |
|
125 | - $data['level'] = 0; |
|
126 | - if (isset($data['parent']) && isset(self::$collection[$data['parent']])) { |
|
127 | - $data['level'] = (self::$collection[$data['parent']]['level'] + 1); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - // Add item to collection |
|
132 | - self::$collection[$key] = $data; |
|
133 | - |
|
134 | - return $key; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string|null $key |
|
139 | - * |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public static function start($key = null) { |
|
143 | - // Add new item |
|
144 | - $key = self::add($key, [ |
|
145 | - 'start' => microtime(true), |
|
146 | - ]); |
|
147 | - |
|
148 | - // Set current item |
|
149 | - self::$currentItem = $key; |
|
150 | - |
|
151 | - // Add to running items |
|
152 | - self::$runningItems[$key] = true; |
|
153 | - |
|
154 | - return $key; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param string|null $key |
|
159 | - * |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public static function stop($key = null) { |
|
163 | - // If no key is given |
|
164 | - if (is_null($key)) { |
|
165 | - // Get key of the last started item |
|
166 | - end(self::$runningItems); |
|
167 | - $key = key(self::$runningItems); |
|
168 | - } |
|
169 | - |
|
170 | - // Check for key duplicates, and find the last one not stopped |
|
171 | - if (isset(self::$collection[$key]) && isset(self::$collection[$key . ' #2'])) { |
|
172 | - $lastNotStopped = false; |
|
173 | - $currentKey = $key; |
|
174 | - $currentIndex = 1; |
|
175 | - while (isset(self::$collection[$currentKey])) { |
|
176 | - if (!isset(self::$collection[$currentKey]['stop'])) { |
|
177 | - $lastNotStopped = $currentKey; |
|
178 | - } |
|
179 | - |
|
180 | - $currentIndex++; |
|
181 | - $currentKey = $key . ' #' . $currentIndex; |
|
182 | - } |
|
183 | - |
|
184 | - if ($lastNotStopped) { |
|
185 | - $key = $lastNotStopped; |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - // If item exists in collection |
|
190 | - if (isset(self::$collection[$key])) { |
|
191 | - // Update the item |
|
192 | - self::$collection[$key]['stop'] = microtime(true); |
|
193 | - |
|
194 | - self::$currentItem = self::$collection[$key]['parent']; |
|
195 | - } |
|
196 | - |
|
197 | - if (isset(self::$runningItems[$key])) { |
|
198 | - unset(self::$runningItems[$key]); |
|
199 | - } |
|
200 | - |
|
201 | - return $key; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * @param string|null $key |
|
206 | - * @param int|float|null $start |
|
207 | - * @param int|float|null $stop |
|
208 | - * |
|
209 | - * @return string |
|
210 | - */ |
|
211 | - public static function custom($key = null, $start = null, $stop = null) { |
|
212 | - // Add new item |
|
213 | - self::add($key, [ |
|
214 | - 'start' => $start, |
|
215 | - 'stop' => $stop, |
|
216 | - ]); |
|
217 | - |
|
218 | - // If no stop value is given |
|
219 | - if (is_null($stop)) { |
|
220 | - // Set current item |
|
221 | - self::$currentItem = $key; |
|
222 | - |
|
223 | - // Add to running items |
|
224 | - self::$runningItems[$key] = true; |
|
225 | - } |
|
226 | - |
|
227 | - return $key; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * @param string|null $key |
|
232 | - * @param string|array|\Closure $callback |
|
233 | - * |
|
234 | - * @return mixed |
|
235 | - */ |
|
236 | - public static function callback($key = null, $callback) { |
|
237 | - // Get parameters for callback |
|
238 | - $callbackParams = func_get_args(); |
|
239 | - unset($callbackParams[0], $callbackParams[1]); |
|
240 | - $callbackParams = array_values($callbackParams); |
|
241 | - |
|
242 | - // Get key if no key is given |
|
243 | - if (is_null($key)) { |
|
244 | - if (is_string($callback)) { |
|
245 | - $key = $callback; |
|
246 | - } elseif (is_array($callback)) { |
|
247 | - $keyArr = []; |
|
248 | - foreach ($callback as $k => $v) { |
|
249 | - if (is_string($v)) { |
|
250 | - $keyArr[] = $v; |
|
251 | - } elseif (is_object($v)) { |
|
252 | - $keyArr[] = get_class($v); |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - $key = implode('', $keyArr); |
|
257 | - if (count($keyArr) > 1) { |
|
258 | - $method = array_pop($keyArr); |
|
259 | - $key = implode('/', $keyArr); |
|
260 | - $key .= '::' . $method; |
|
261 | - } |
|
262 | - |
|
263 | - unset($keyArr, $method); |
|
264 | - } elseif (is_object($callback) && $callback instanceof \Closure) { |
|
265 | - $key = 'closure'; |
|
266 | - } |
|
267 | - $key = 'callback: ' . $key; |
|
268 | - } |
|
269 | - |
|
270 | - // Set default return value |
|
271 | - $returnValue = true; |
|
272 | - |
|
273 | - // Set error handler, to convert errors to exceptions |
|
274 | - set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) { |
|
275 | - throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); |
|
276 | - }); |
|
277 | - |
|
278 | - try { |
|
279 | - // Start output buffer to capture any output |
|
280 | - ob_start(); |
|
281 | - |
|
282 | - // Start profiler |
|
283 | - self::start($key); |
|
284 | - |
|
285 | - // Execute callback, and get result |
|
286 | - $callbackResult = call_user_func_array($callback, $callbackParams); |
|
287 | - |
|
288 | - // Stop profiler |
|
289 | - self::stop($key); |
|
290 | - |
|
291 | - // Get and clean output buffer |
|
292 | - $callbackOutput = ob_get_clean(); |
|
293 | - } catch (\ErrorException $callbackException) { |
|
294 | - // Stop and clean output buffer |
|
295 | - ob_end_clean(); |
|
296 | - |
|
297 | - // Show error message |
|
298 | - self::output('Invalid callback sent to Timer::callback: ' . str_replace('callback: ', '', $key)); |
|
299 | - |
|
300 | - // Clear the item from the collection |
|
301 | - unset(self::$collection[$key]); |
|
302 | - |
|
303 | - // Clear callback result and output |
|
304 | - unset($callbackResult, $callbackOutput); |
|
305 | - |
|
306 | - // Set return value to false |
|
307 | - $returnValue = false; |
|
308 | - } |
|
309 | - |
|
310 | - // Restore error handler |
|
311 | - restore_error_handler(); |
|
312 | - |
|
313 | - // Return result, output or true |
|
314 | - return (isset($callbackResult) ? $callbackResult : (!empty($callbackOutput) ? $callbackOutput : $returnValue)); |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * @param string|null $key |
|
319 | - * @param array $options |
|
320 | - * |
|
321 | - * @codeCoverageIgnore |
|
322 | - */ |
|
323 | - public static function show($key = null, $options = []) { |
|
324 | - $output = self::getStats($key, $options); |
|
325 | - |
|
326 | - if (!empty($output)) { |
|
327 | - self::output($output); |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * @param array $options |
|
333 | - * |
|
334 | - * @codeCoverageIgnore |
|
335 | - */ |
|
336 | - public static function showAll($options = []) { |
|
337 | - // Stop started items |
|
338 | - if (count(self::$runningItems)) { |
|
339 | - foreach (self::$runningItems as $key => $value) { |
|
340 | - self::stop($key); |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - // Output items |
|
345 | - $output = ''; |
|
346 | - $itemCount = 1; |
|
347 | - foreach (self::$collection as $key => $item) { |
|
348 | - $stats = self::getStats($key, $options); |
|
349 | - |
|
350 | - if (php_sapi_name() == 'cli') { |
|
351 | - $output .= (!empty($output) ? "\n" : '') . $stats; |
|
352 | - } else { |
|
353 | - $output .= '<div class="xicrow-php-debug-timer">'; |
|
354 | - $output .= $stats; |
|
355 | - $output .= '</div>'; |
|
356 | - } |
|
357 | - |
|
358 | - $itemCount++; |
|
359 | - |
|
360 | - unset($stats); |
|
361 | - } |
|
362 | - unset($itemCount); |
|
363 | - |
|
364 | - self::output($output); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * @param string|null $key |
|
369 | - * @param array $options |
|
370 | - * |
|
371 | - * @return string |
|
372 | - */ |
|
373 | - public static function getStats($key, $options = []) { |
|
374 | - // Merge options with default options |
|
375 | - $options = array_merge([ |
|
376 | - // Show nested (boolean) |
|
377 | - 'nested' => true, |
|
378 | - // Prefix for nested items (string) |
|
379 | - 'nested_prefix' => '|-- ', |
|
380 | - // Max key length (int) |
|
381 | - 'max_key_length' => 100, |
|
382 | - ], $options); |
|
383 | - |
|
384 | - // If item does not exist |
|
385 | - if (!isset(self::$collection[$key])) { |
|
386 | - return 'Unknow item in with key: ' . $key; |
|
387 | - } |
|
388 | - |
|
389 | - // Get item |
|
390 | - $item = self::$collection[$key]; |
|
391 | - |
|
392 | - // Get item result |
|
393 | - $itemResult = 'N/A'; |
|
394 | - $itemResultFormatted = 'N/A'; |
|
395 | - if (isset($item['start']) && isset($item['stop'])) { |
|
396 | - $itemResult = (($item['stop'] - $item['start']) * 1000); |
|
397 | - $itemResultFormatted = self::formatMiliseconds($itemResult, 4, self::$forceDisplayUnit); |
|
398 | - } |
|
399 | - |
|
400 | - // Variable for output |
|
401 | - $output = ''; |
|
402 | - |
|
403 | - // Prep key for output |
|
404 | - $outputName = ''; |
|
405 | - $outputName .= ($options['nested'] ? str_repeat($options['nested_prefix'], $item['level']) : ''); |
|
406 | - $outputName .= $item['key']; |
|
407 | - if (mb_strlen($outputName) > $options['max_key_length']) { |
|
408 | - $outputName = '~' . mb_substr($item['key'], -($options['max_key_length'] - 1)); |
|
409 | - } |
|
410 | - |
|
411 | - // Add item stats |
|
412 | - $output .= str_pad($outputName, ($options['max_key_length'] + (strlen($outputName) - mb_strlen($outputName))), ' '); |
|
413 | - $output .= ' | '; |
|
414 | - $output .= str_pad($itemResultFormatted, 20, ' ', ($itemResult == 'N/A' ? STR_PAD_RIGHT : STR_PAD_LEFT)); |
|
415 | - |
|
416 | - if (php_sapi_name() != 'cli' && is_array(self::$colorThreshold) && count(self::$colorThreshold)) { |
|
417 | - krsort(self::$colorThreshold); |
|
418 | - foreach (self::$colorThreshold as $value => $color) { |
|
419 | - if (is_numeric($itemResult) && $itemResult >= $value) { |
|
420 | - $output = '<span style="color: ' . $color . ';">' . $output . '</span>'; |
|
421 | - } |
|
422 | - } |
|
423 | - } |
|
424 | - |
|
425 | - return $output; |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * @param int|float $number |
|
430 | - * @param int $precision |
|
431 | - * @param null|string $forceUnit |
|
432 | - * |
|
433 | - * @return string |
|
434 | - */ |
|
435 | - public static function formatMiliseconds($number = 0, $precision = 2, $forceUnit = null) { |
|
436 | - $units = [ |
|
437 | - 'MS' => 1, |
|
438 | - 'S' => 1000, |
|
439 | - 'M' => 60, |
|
440 | - 'H' => 60, |
|
441 | - 'D' => 24, |
|
442 | - 'W' => 7, |
|
443 | - ]; |
|
444 | - |
|
445 | - if (is_null($forceUnit)) { |
|
446 | - $forceUnit = self::$forceDisplayUnit; |
|
447 | - } |
|
448 | - |
|
449 | - $value = $number; |
|
450 | - if (!empty($forceUnit) && array_key_exists($forceUnit, $units)) { |
|
451 | - $unit = $forceUnit; |
|
452 | - foreach ($units as $k => $v) { |
|
453 | - $value = ($value / $v); |
|
454 | - if ($k == $unit) { |
|
455 | - break; |
|
456 | - } |
|
457 | - } |
|
458 | - } else { |
|
459 | - $unit = ''; |
|
460 | - foreach ($units as $k => $v) { |
|
461 | - if (empty($unit) || ($value / $v) > 1) { |
|
462 | - $value = ($value / $v); |
|
463 | - $unit = $k; |
|
464 | - } else { |
|
465 | - break; |
|
466 | - } |
|
467 | - } |
|
468 | - } |
|
469 | - |
|
470 | - return sprintf('%0.' . $precision . 'f', $value) . ' ' . str_pad($unit, 2, ' ', STR_PAD_RIGHT); |
|
471 | - } |
|
80 | + /** |
|
81 | + * @param string|null $key |
|
82 | + * @param array $data |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public static function add($key = null, $data = []) { |
|
87 | + // If no key is given |
|
88 | + if (is_null($key)) { |
|
89 | + // Set key to file and line |
|
90 | + $key = Debugger::getCalledFrom(2); |
|
91 | + } |
|
92 | + |
|
93 | + // If key is allready in use |
|
94 | + if (isset(self::$collection[$key])) { |
|
95 | + // Get original item |
|
96 | + $item = self::$collection[$key]; |
|
97 | + |
|
98 | + // Set new item count |
|
99 | + $itemCount = (isset($item['count']) ? ($item['count'] + 1) : 2); |
|
100 | + |
|
101 | + // Set correct key for the original item |
|
102 | + if (strpos($item['key'], '#') === false) { |
|
103 | + self::$collection[$key] = array_merge($item, [ |
|
104 | + 'key' => $key . ' #1', |
|
105 | + 'count' => $itemCount, |
|
106 | + ]); |
|
107 | + } else { |
|
108 | + self::$collection[$key] = array_merge($item, [ |
|
109 | + 'count' => $itemCount, |
|
110 | + ]); |
|
111 | + } |
|
112 | + |
|
113 | + // Set new key |
|
114 | + $key = $key . ' #' . $itemCount; |
|
115 | + } |
|
116 | + |
|
117 | + // Make sure various options are set |
|
118 | + if (!isset($data['key'])) { |
|
119 | + $data['key'] = $key; |
|
120 | + } |
|
121 | + if (!isset($data['parent'])) { |
|
122 | + $data['parent'] = self::$currentItem; |
|
123 | + } |
|
124 | + if (!isset($data['level'])) { |
|
125 | + $data['level'] = 0; |
|
126 | + if (isset($data['parent']) && isset(self::$collection[$data['parent']])) { |
|
127 | + $data['level'] = (self::$collection[$data['parent']]['level'] + 1); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + // Add item to collection |
|
132 | + self::$collection[$key] = $data; |
|
133 | + |
|
134 | + return $key; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string|null $key |
|
139 | + * |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public static function start($key = null) { |
|
143 | + // Add new item |
|
144 | + $key = self::add($key, [ |
|
145 | + 'start' => microtime(true), |
|
146 | + ]); |
|
147 | + |
|
148 | + // Set current item |
|
149 | + self::$currentItem = $key; |
|
150 | + |
|
151 | + // Add to running items |
|
152 | + self::$runningItems[$key] = true; |
|
153 | + |
|
154 | + return $key; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param string|null $key |
|
159 | + * |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public static function stop($key = null) { |
|
163 | + // If no key is given |
|
164 | + if (is_null($key)) { |
|
165 | + // Get key of the last started item |
|
166 | + end(self::$runningItems); |
|
167 | + $key = key(self::$runningItems); |
|
168 | + } |
|
169 | + |
|
170 | + // Check for key duplicates, and find the last one not stopped |
|
171 | + if (isset(self::$collection[$key]) && isset(self::$collection[$key . ' #2'])) { |
|
172 | + $lastNotStopped = false; |
|
173 | + $currentKey = $key; |
|
174 | + $currentIndex = 1; |
|
175 | + while (isset(self::$collection[$currentKey])) { |
|
176 | + if (!isset(self::$collection[$currentKey]['stop'])) { |
|
177 | + $lastNotStopped = $currentKey; |
|
178 | + } |
|
179 | + |
|
180 | + $currentIndex++; |
|
181 | + $currentKey = $key . ' #' . $currentIndex; |
|
182 | + } |
|
183 | + |
|
184 | + if ($lastNotStopped) { |
|
185 | + $key = $lastNotStopped; |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + // If item exists in collection |
|
190 | + if (isset(self::$collection[$key])) { |
|
191 | + // Update the item |
|
192 | + self::$collection[$key]['stop'] = microtime(true); |
|
193 | + |
|
194 | + self::$currentItem = self::$collection[$key]['parent']; |
|
195 | + } |
|
196 | + |
|
197 | + if (isset(self::$runningItems[$key])) { |
|
198 | + unset(self::$runningItems[$key]); |
|
199 | + } |
|
200 | + |
|
201 | + return $key; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * @param string|null $key |
|
206 | + * @param int|float|null $start |
|
207 | + * @param int|float|null $stop |
|
208 | + * |
|
209 | + * @return string |
|
210 | + */ |
|
211 | + public static function custom($key = null, $start = null, $stop = null) { |
|
212 | + // Add new item |
|
213 | + self::add($key, [ |
|
214 | + 'start' => $start, |
|
215 | + 'stop' => $stop, |
|
216 | + ]); |
|
217 | + |
|
218 | + // If no stop value is given |
|
219 | + if (is_null($stop)) { |
|
220 | + // Set current item |
|
221 | + self::$currentItem = $key; |
|
222 | + |
|
223 | + // Add to running items |
|
224 | + self::$runningItems[$key] = true; |
|
225 | + } |
|
226 | + |
|
227 | + return $key; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * @param string|null $key |
|
232 | + * @param string|array|\Closure $callback |
|
233 | + * |
|
234 | + * @return mixed |
|
235 | + */ |
|
236 | + public static function callback($key = null, $callback) { |
|
237 | + // Get parameters for callback |
|
238 | + $callbackParams = func_get_args(); |
|
239 | + unset($callbackParams[0], $callbackParams[1]); |
|
240 | + $callbackParams = array_values($callbackParams); |
|
241 | + |
|
242 | + // Get key if no key is given |
|
243 | + if (is_null($key)) { |
|
244 | + if (is_string($callback)) { |
|
245 | + $key = $callback; |
|
246 | + } elseif (is_array($callback)) { |
|
247 | + $keyArr = []; |
|
248 | + foreach ($callback as $k => $v) { |
|
249 | + if (is_string($v)) { |
|
250 | + $keyArr[] = $v; |
|
251 | + } elseif (is_object($v)) { |
|
252 | + $keyArr[] = get_class($v); |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + $key = implode('', $keyArr); |
|
257 | + if (count($keyArr) > 1) { |
|
258 | + $method = array_pop($keyArr); |
|
259 | + $key = implode('/', $keyArr); |
|
260 | + $key .= '::' . $method; |
|
261 | + } |
|
262 | + |
|
263 | + unset($keyArr, $method); |
|
264 | + } elseif (is_object($callback) && $callback instanceof \Closure) { |
|
265 | + $key = 'closure'; |
|
266 | + } |
|
267 | + $key = 'callback: ' . $key; |
|
268 | + } |
|
269 | + |
|
270 | + // Set default return value |
|
271 | + $returnValue = true; |
|
272 | + |
|
273 | + // Set error handler, to convert errors to exceptions |
|
274 | + set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) { |
|
275 | + throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); |
|
276 | + }); |
|
277 | + |
|
278 | + try { |
|
279 | + // Start output buffer to capture any output |
|
280 | + ob_start(); |
|
281 | + |
|
282 | + // Start profiler |
|
283 | + self::start($key); |
|
284 | + |
|
285 | + // Execute callback, and get result |
|
286 | + $callbackResult = call_user_func_array($callback, $callbackParams); |
|
287 | + |
|
288 | + // Stop profiler |
|
289 | + self::stop($key); |
|
290 | + |
|
291 | + // Get and clean output buffer |
|
292 | + $callbackOutput = ob_get_clean(); |
|
293 | + } catch (\ErrorException $callbackException) { |
|
294 | + // Stop and clean output buffer |
|
295 | + ob_end_clean(); |
|
296 | + |
|
297 | + // Show error message |
|
298 | + self::output('Invalid callback sent to Timer::callback: ' . str_replace('callback: ', '', $key)); |
|
299 | + |
|
300 | + // Clear the item from the collection |
|
301 | + unset(self::$collection[$key]); |
|
302 | + |
|
303 | + // Clear callback result and output |
|
304 | + unset($callbackResult, $callbackOutput); |
|
305 | + |
|
306 | + // Set return value to false |
|
307 | + $returnValue = false; |
|
308 | + } |
|
309 | + |
|
310 | + // Restore error handler |
|
311 | + restore_error_handler(); |
|
312 | + |
|
313 | + // Return result, output or true |
|
314 | + return (isset($callbackResult) ? $callbackResult : (!empty($callbackOutput) ? $callbackOutput : $returnValue)); |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * @param string|null $key |
|
319 | + * @param array $options |
|
320 | + * |
|
321 | + * @codeCoverageIgnore |
|
322 | + */ |
|
323 | + public static function show($key = null, $options = []) { |
|
324 | + $output = self::getStats($key, $options); |
|
325 | + |
|
326 | + if (!empty($output)) { |
|
327 | + self::output($output); |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * @param array $options |
|
333 | + * |
|
334 | + * @codeCoverageIgnore |
|
335 | + */ |
|
336 | + public static function showAll($options = []) { |
|
337 | + // Stop started items |
|
338 | + if (count(self::$runningItems)) { |
|
339 | + foreach (self::$runningItems as $key => $value) { |
|
340 | + self::stop($key); |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + // Output items |
|
345 | + $output = ''; |
|
346 | + $itemCount = 1; |
|
347 | + foreach (self::$collection as $key => $item) { |
|
348 | + $stats = self::getStats($key, $options); |
|
349 | + |
|
350 | + if (php_sapi_name() == 'cli') { |
|
351 | + $output .= (!empty($output) ? "\n" : '') . $stats; |
|
352 | + } else { |
|
353 | + $output .= '<div class="xicrow-php-debug-timer">'; |
|
354 | + $output .= $stats; |
|
355 | + $output .= '</div>'; |
|
356 | + } |
|
357 | + |
|
358 | + $itemCount++; |
|
359 | + |
|
360 | + unset($stats); |
|
361 | + } |
|
362 | + unset($itemCount); |
|
363 | + |
|
364 | + self::output($output); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * @param string|null $key |
|
369 | + * @param array $options |
|
370 | + * |
|
371 | + * @return string |
|
372 | + */ |
|
373 | + public static function getStats($key, $options = []) { |
|
374 | + // Merge options with default options |
|
375 | + $options = array_merge([ |
|
376 | + // Show nested (boolean) |
|
377 | + 'nested' => true, |
|
378 | + // Prefix for nested items (string) |
|
379 | + 'nested_prefix' => '|-- ', |
|
380 | + // Max key length (int) |
|
381 | + 'max_key_length' => 100, |
|
382 | + ], $options); |
|
383 | + |
|
384 | + // If item does not exist |
|
385 | + if (!isset(self::$collection[$key])) { |
|
386 | + return 'Unknow item in with key: ' . $key; |
|
387 | + } |
|
388 | + |
|
389 | + // Get item |
|
390 | + $item = self::$collection[$key]; |
|
391 | + |
|
392 | + // Get item result |
|
393 | + $itemResult = 'N/A'; |
|
394 | + $itemResultFormatted = 'N/A'; |
|
395 | + if (isset($item['start']) && isset($item['stop'])) { |
|
396 | + $itemResult = (($item['stop'] - $item['start']) * 1000); |
|
397 | + $itemResultFormatted = self::formatMiliseconds($itemResult, 4, self::$forceDisplayUnit); |
|
398 | + } |
|
399 | + |
|
400 | + // Variable for output |
|
401 | + $output = ''; |
|
402 | + |
|
403 | + // Prep key for output |
|
404 | + $outputName = ''; |
|
405 | + $outputName .= ($options['nested'] ? str_repeat($options['nested_prefix'], $item['level']) : ''); |
|
406 | + $outputName .= $item['key']; |
|
407 | + if (mb_strlen($outputName) > $options['max_key_length']) { |
|
408 | + $outputName = '~' . mb_substr($item['key'], -($options['max_key_length'] - 1)); |
|
409 | + } |
|
410 | + |
|
411 | + // Add item stats |
|
412 | + $output .= str_pad($outputName, ($options['max_key_length'] + (strlen($outputName) - mb_strlen($outputName))), ' '); |
|
413 | + $output .= ' | '; |
|
414 | + $output .= str_pad($itemResultFormatted, 20, ' ', ($itemResult == 'N/A' ? STR_PAD_RIGHT : STR_PAD_LEFT)); |
|
415 | + |
|
416 | + if (php_sapi_name() != 'cli' && is_array(self::$colorThreshold) && count(self::$colorThreshold)) { |
|
417 | + krsort(self::$colorThreshold); |
|
418 | + foreach (self::$colorThreshold as $value => $color) { |
|
419 | + if (is_numeric($itemResult) && $itemResult >= $value) { |
|
420 | + $output = '<span style="color: ' . $color . ';">' . $output . '</span>'; |
|
421 | + } |
|
422 | + } |
|
423 | + } |
|
424 | + |
|
425 | + return $output; |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * @param int|float $number |
|
430 | + * @param int $precision |
|
431 | + * @param null|string $forceUnit |
|
432 | + * |
|
433 | + * @return string |
|
434 | + */ |
|
435 | + public static function formatMiliseconds($number = 0, $precision = 2, $forceUnit = null) { |
|
436 | + $units = [ |
|
437 | + 'MS' => 1, |
|
438 | + 'S' => 1000, |
|
439 | + 'M' => 60, |
|
440 | + 'H' => 60, |
|
441 | + 'D' => 24, |
|
442 | + 'W' => 7, |
|
443 | + ]; |
|
444 | + |
|
445 | + if (is_null($forceUnit)) { |
|
446 | + $forceUnit = self::$forceDisplayUnit; |
|
447 | + } |
|
448 | + |
|
449 | + $value = $number; |
|
450 | + if (!empty($forceUnit) && array_key_exists($forceUnit, $units)) { |
|
451 | + $unit = $forceUnit; |
|
452 | + foreach ($units as $k => $v) { |
|
453 | + $value = ($value / $v); |
|
454 | + if ($k == $unit) { |
|
455 | + break; |
|
456 | + } |
|
457 | + } |
|
458 | + } else { |
|
459 | + $unit = ''; |
|
460 | + foreach ($units as $k => $v) { |
|
461 | + if (empty($unit) || ($value / $v) > 1) { |
|
462 | + $value = ($value / $v); |
|
463 | + $unit = $k; |
|
464 | + } else { |
|
465 | + break; |
|
466 | + } |
|
467 | + } |
|
468 | + } |
|
469 | + |
|
470 | + return sprintf('%0.' . $precision . 'f', $value) . ' ' . str_pad($unit, 2, ' ', STR_PAD_RIGHT); |
|
471 | + } |
|
472 | 472 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | // Set correct key for the original item |
102 | 102 | if (strpos($item['key'], '#') === false) { |
103 | 103 | self::$collection[$key] = array_merge($item, [ |
104 | - 'key' => $key . ' #1', |
|
104 | + 'key' => $key.' #1', |
|
105 | 105 | 'count' => $itemCount, |
106 | 106 | ]); |
107 | 107 | } else { |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | } |
112 | 112 | |
113 | 113 | // Set new key |
114 | - $key = $key . ' #' . $itemCount; |
|
114 | + $key = $key.' #'.$itemCount; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | // Make sure various options are set |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | } |
169 | 169 | |
170 | 170 | // Check for key duplicates, and find the last one not stopped |
171 | - if (isset(self::$collection[$key]) && isset(self::$collection[$key . ' #2'])) { |
|
171 | + if (isset(self::$collection[$key]) && isset(self::$collection[$key.' #2'])) { |
|
172 | 172 | $lastNotStopped = false; |
173 | 173 | $currentKey = $key; |
174 | 174 | $currentIndex = 1; |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | } |
179 | 179 | |
180 | 180 | $currentIndex++; |
181 | - $currentKey = $key . ' #' . $currentIndex; |
|
181 | + $currentKey = $key.' #'.$currentIndex; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | if ($lastNotStopped) { |
@@ -257,21 +257,21 @@ discard block |
||
257 | 257 | if (count($keyArr) > 1) { |
258 | 258 | $method = array_pop($keyArr); |
259 | 259 | $key = implode('/', $keyArr); |
260 | - $key .= '::' . $method; |
|
260 | + $key .= '::'.$method; |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | unset($keyArr, $method); |
264 | 264 | } elseif (is_object($callback) && $callback instanceof \Closure) { |
265 | 265 | $key = 'closure'; |
266 | 266 | } |
267 | - $key = 'callback: ' . $key; |
|
267 | + $key = 'callback: '.$key; |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | // Set default return value |
271 | 271 | $returnValue = true; |
272 | 272 | |
273 | 273 | // Set error handler, to convert errors to exceptions |
274 | - set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) { |
|
274 | + set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) { |
|
275 | 275 | throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); |
276 | 276 | }); |
277 | 277 | |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | ob_end_clean(); |
296 | 296 | |
297 | 297 | // Show error message |
298 | - self::output('Invalid callback sent to Timer::callback: ' . str_replace('callback: ', '', $key)); |
|
298 | + self::output('Invalid callback sent to Timer::callback: '.str_replace('callback: ', '', $key)); |
|
299 | 299 | |
300 | 300 | // Clear the item from the collection |
301 | 301 | unset(self::$collection[$key]); |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | $stats = self::getStats($key, $options); |
349 | 349 | |
350 | 350 | if (php_sapi_name() == 'cli') { |
351 | - $output .= (!empty($output) ? "\n" : '') . $stats; |
|
351 | + $output .= (!empty($output) ? "\n" : '').$stats; |
|
352 | 352 | } else { |
353 | 353 | $output .= '<div class="xicrow-php-debug-timer">'; |
354 | 354 | $output .= $stats; |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | |
384 | 384 | // If item does not exist |
385 | 385 | if (!isset(self::$collection[$key])) { |
386 | - return 'Unknow item in with key: ' . $key; |
|
386 | + return 'Unknow item in with key: '.$key; |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | // Get item |
@@ -405,7 +405,7 @@ discard block |
||
405 | 405 | $outputName .= ($options['nested'] ? str_repeat($options['nested_prefix'], $item['level']) : ''); |
406 | 406 | $outputName .= $item['key']; |
407 | 407 | if (mb_strlen($outputName) > $options['max_key_length']) { |
408 | - $outputName = '~' . mb_substr($item['key'], -($options['max_key_length'] - 1)); |
|
408 | + $outputName = '~'.mb_substr($item['key'], -($options['max_key_length'] - 1)); |
|
409 | 409 | } |
410 | 410 | |
411 | 411 | // Add item stats |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | krsort(self::$colorThreshold); |
418 | 418 | foreach (self::$colorThreshold as $value => $color) { |
419 | 419 | if (is_numeric($itemResult) && $itemResult >= $value) { |
420 | - $output = '<span style="color: ' . $color . ';">' . $output . '</span>'; |
|
420 | + $output = '<span style="color: '.$color.';">'.$output.'</span>'; |
|
421 | 421 | } |
422 | 422 | } |
423 | 423 | } |
@@ -467,6 +467,6 @@ discard block |
||
467 | 467 | } |
468 | 468 | } |
469 | 469 | |
470 | - return sprintf('%0.' . $precision . 'f', $value) . ' ' . str_pad($unit, 2, ' ', STR_PAD_RIGHT); |
|
470 | + return sprintf('%0.'.$precision.'f', $value).' '.str_pad($unit, 2, ' ', STR_PAD_RIGHT); |
|
471 | 471 | } |
472 | 472 | } |
@@ -7,458 +7,458 @@ |
||
7 | 7 | * @package Xicrow\PhpDebug |
8 | 8 | */ |
9 | 9 | class Debugger { |
10 | - /** |
|
11 | - * @var string|null |
|
12 | - */ |
|
13 | - public static $documentRoot = null; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var bool |
|
17 | - */ |
|
18 | - public static $showCalledFrom = true; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var bool |
|
22 | - */ |
|
23 | - public static $output = true; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string $data |
|
27 | - * @param array $options |
|
28 | - * |
|
29 | - * @codeCoverageIgnore |
|
30 | - */ |
|
31 | - public static function output($data, array $options = []) { |
|
32 | - $options = array_merge([ |
|
33 | - 'trace_offset' => 0, |
|
34 | - ], $options); |
|
35 | - |
|
36 | - if (!self::$output || !is_string($data)) { |
|
37 | - return; |
|
38 | - } |
|
39 | - |
|
40 | - if (php_sapi_name() == 'cli') { |
|
41 | - echo str_pad(' DEBUG ', 100, '-', STR_PAD_BOTH); |
|
42 | - echo "\n"; |
|
43 | - if (self::$showCalledFrom) { |
|
44 | - echo self::getCalledFrom($options['trace_offset'] + 2); |
|
45 | - echo "\n"; |
|
46 | - } |
|
47 | - echo $data; |
|
48 | - echo "\n"; |
|
49 | - } else { |
|
50 | - if (self::$showCalledFrom) { |
|
51 | - echo '<pre style="margin-bottom: 0; padding: 5px; font-family: Menlo, Monaco, Consolas, monospace; font-weight: normal; font-size: 12px; background-color: #18171B; color: #AAAAAA;">'; |
|
10 | + /** |
|
11 | + * @var string|null |
|
12 | + */ |
|
13 | + public static $documentRoot = null; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var bool |
|
17 | + */ |
|
18 | + public static $showCalledFrom = true; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var bool |
|
22 | + */ |
|
23 | + public static $output = true; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string $data |
|
27 | + * @param array $options |
|
28 | + * |
|
29 | + * @codeCoverageIgnore |
|
30 | + */ |
|
31 | + public static function output($data, array $options = []) { |
|
32 | + $options = array_merge([ |
|
33 | + 'trace_offset' => 0, |
|
34 | + ], $options); |
|
35 | + |
|
36 | + if (!self::$output || !is_string($data)) { |
|
37 | + return; |
|
38 | + } |
|
39 | + |
|
40 | + if (php_sapi_name() == 'cli') { |
|
41 | + echo str_pad(' DEBUG ', 100, '-', STR_PAD_BOTH); |
|
42 | + echo "\n"; |
|
43 | + if (self::$showCalledFrom) { |
|
52 | 44 | echo self::getCalledFrom($options['trace_offset'] + 2); |
53 | - echo '</pre>'; |
|
54 | - } |
|
45 | + echo "\n"; |
|
46 | + } |
|
47 | + echo $data; |
|
48 | + echo "\n"; |
|
49 | + } else { |
|
50 | + if (self::$showCalledFrom) { |
|
51 | + echo '<pre style="margin-bottom: 0; padding: 5px; font-family: Menlo, Monaco, Consolas, monospace; font-weight: normal; font-size: 12px; background-color: #18171B; color: #AAAAAA;">'; |
|
52 | + echo self::getCalledFrom($options['trace_offset'] + 2); |
|
53 | + echo '</pre>'; |
|
54 | + } |
|
55 | 55 | echo '<pre style="margin-top: 0; padding: 5px; font-family: Menlo, Monaco, Consolas, monospace; font-weight: bold; font-size: 12px; background-color: #18171B; color: #FF8400;">'; |
56 | - echo $data; |
|
57 | - echo '</pre>'; |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param mixed $data |
|
63 | - * @param array $options |
|
64 | - * |
|
65 | - * @codeCoverageIgnore |
|
66 | - */ |
|
67 | - public static function debug($data, array $options = []) { |
|
68 | - $options = array_merge([ |
|
69 | - 'trace_offset' => 0, |
|
70 | - ], $options); |
|
56 | + echo $data; |
|
57 | + echo '</pre>'; |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param mixed $data |
|
63 | + * @param array $options |
|
64 | + * |
|
65 | + * @codeCoverageIgnore |
|
66 | + */ |
|
67 | + public static function debug($data, array $options = []) { |
|
68 | + $options = array_merge([ |
|
69 | + 'trace_offset' => 0, |
|
70 | + ], $options); |
|
71 | 71 | |
72 | 72 | self::output(self::getDebugInformation($data), $options); |
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param array $options |
|
77 | - * |
|
78 | - * @codeCoverageIgnore |
|
79 | - */ |
|
80 | - public static function showTrace(array $options = []) { |
|
81 | - $options = array_merge([ |
|
82 | - 'trace_offset' => 0, |
|
83 | - 'reverse' => false, |
|
84 | - ], $options); |
|
85 | - |
|
86 | - $backtrace = ($options['reverse'] ? array_reverse(debug_backtrace()) : debug_backtrace()); |
|
87 | - |
|
88 | - $output = ''; |
|
89 | - $traceIndex = ($options['reverse'] ? 1 : count($backtrace)); |
|
90 | - foreach ($backtrace as $trace) { |
|
91 | - $output .= $traceIndex . ': '; |
|
92 | - $output .= self::getCalledFromTrace($trace); |
|
93 | - $output .= "\n"; |
|
94 | - |
|
95 | - $traceIndex += ($options['reverse'] ? 1 : -1); |
|
96 | - } |
|
97 | - |
|
98 | - self::output($output); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param string $class |
|
103 | - * @param bool $output |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public static function reflectClass($class, $output = true) { |
|
108 | - $data = ''; |
|
109 | - |
|
110 | - $reflectionClass = new \ReflectionClass($class); |
|
111 | - |
|
112 | - $comment = $reflectionClass->getDocComment(); |
|
113 | - if (!empty($comment)) { |
|
114 | - $data .= $comment; |
|
115 | - $data .= "\n"; |
|
116 | - } |
|
117 | - |
|
118 | - $data .= 'class ' . $reflectionClass->name . '{'; |
|
119 | - $firstElement = true; |
|
120 | - foreach ($reflectionClass->getProperties() as $reflectionProperty) { |
|
121 | - if (!$firstElement) { |
|
122 | - $data .= "\n"; |
|
123 | - } |
|
124 | - $firstElement = false; |
|
125 | - |
|
126 | - $data .= self::reflectClassProperty($class, $reflectionProperty->name, false); |
|
127 | - } |
|
128 | - |
|
129 | - foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
|
130 | - if (!$firstElement) { |
|
131 | - $data .= "\n"; |
|
132 | - } |
|
133 | - $firstElement = false; |
|
134 | - |
|
135 | - $data .= self::reflectClassMethod($class, $reflectionMethod->name, false); |
|
136 | - } |
|
137 | - $data .= "\n"; |
|
138 | - $data .= '}'; |
|
139 | - |
|
140 | - if ($output) { |
|
141 | - self::output($data); |
|
142 | - } |
|
143 | - |
|
144 | - return $data; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $class |
|
149 | - * @param string $property |
|
150 | - * |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public static function reflectClassProperty($class, $property, $output = true) { |
|
154 | - $data = ''; |
|
155 | - |
|
156 | - $reflectionClass = new \ReflectionClass($class); |
|
157 | - $reflectionProperty = new \ReflectionProperty($class, $property); |
|
158 | - |
|
159 | - $defaultPropertyValues = $reflectionClass->getDefaultProperties(); |
|
160 | - |
|
161 | - $comment = $reflectionProperty->getDocComment(); |
|
162 | - if (!empty($comment)) { |
|
163 | - $data .= "\n"; |
|
164 | - $data .= "\t"; |
|
165 | - $data .= $comment; |
|
166 | - } |
|
167 | - |
|
168 | - $data .= "\n"; |
|
169 | - $data .= "\t"; |
|
170 | - $data .= ($reflectionProperty->isPublic() ? 'public ' : ''); |
|
171 | - $data .= ($reflectionProperty->isPrivate() ? 'private ' : ''); |
|
172 | - $data .= ($reflectionProperty->isProtected() ? 'protected ' : ''); |
|
173 | - $data .= ($reflectionProperty->isStatic() ? 'static ' : ''); |
|
174 | - $data .= '$' . $reflectionProperty->name; |
|
175 | - if (isset($defaultPropertyValues[$property])) { |
|
176 | - $data .= ' = ' . self::getDebugInformation($defaultPropertyValues[$property]); |
|
177 | - } |
|
178 | - $data .= ';'; |
|
179 | - |
|
180 | - if ($output) { |
|
181 | - self::output($data); |
|
182 | - } |
|
183 | - |
|
184 | - return $data; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @param string $class |
|
189 | - * @param string $method |
|
190 | - * |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - public static function reflectClassMethod($class, $method, $output = true) { |
|
194 | - $data = ''; |
|
195 | - |
|
196 | - $reflectionMethod = new \ReflectionMethod($class, $method); |
|
197 | - |
|
198 | - $comment = $reflectionMethod->getDocComment(); |
|
199 | - if (!empty($comment)) { |
|
200 | - $data .= "\n"; |
|
201 | - $data .= "\t"; |
|
202 | - $data .= $comment; |
|
203 | - } |
|
204 | - |
|
205 | - $data .= "\n"; |
|
206 | - $data .= "\t"; |
|
207 | - $data .= ($reflectionMethod->isPublic() ? 'public ' : ''); |
|
208 | - $data .= ($reflectionMethod->isPrivate() ? 'private ' : ''); |
|
209 | - $data .= ($reflectionMethod->isProtected() ? 'protected ' : ''); |
|
210 | - $data .= ($reflectionMethod->isStatic() ? 'static ' : ''); |
|
211 | - $data .= 'function ' . $reflectionMethod->name . '('; |
|
212 | - if ($reflectionMethod->getNumberOfParameters()) { |
|
213 | - foreach ($reflectionMethod->getParameters() as $reflectionMethodParameterIndex => $reflectionMethodParameter) { |
|
214 | - $data .= ($reflectionMethodParameterIndex > 0 ? ', ' : ''); |
|
215 | - $data .= '$' . $reflectionMethodParameter->name; |
|
216 | - if ($reflectionMethodParameter->isDefaultValueAvailable()) { |
|
217 | - $defaultValue = self::getDebugInformation($reflectionMethodParameter->getDefaultValue()); |
|
218 | - $defaultValue = str_replace(["\n", "\t"], '', $defaultValue); |
|
219 | - $data .= ' = ' . $defaultValue; |
|
220 | - } |
|
221 | - } |
|
222 | - } |
|
223 | - $data .= ') {}'; |
|
224 | - |
|
225 | - if ($output) { |
|
226 | - self::output($data); |
|
227 | - } |
|
228 | - |
|
229 | - return $data; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * @param int $index |
|
234 | - * |
|
235 | - * @return string |
|
236 | - */ |
|
237 | - public static function getCalledFrom($index = 0) { |
|
238 | - $backtrace = debug_backtrace(); |
|
239 | - |
|
240 | - if (!isset($backtrace[$index])) { |
|
241 | - return 'Unknown trace with index: ' . $index; |
|
242 | - } |
|
243 | - |
|
244 | - return self::getCalledFromTrace($backtrace[$index]); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @param array $trace |
|
249 | - * |
|
250 | - * @return string |
|
251 | - */ |
|
252 | - public static function getCalledFromTrace($trace) { |
|
253 | - // Get file and line number |
|
254 | - $calledFromFile = ''; |
|
255 | - if (isset($trace['file'])) { |
|
256 | - $calledFromFile .= $trace['file'] . ' line ' . $trace['line']; |
|
257 | - $calledFromFile = str_replace('\\', '/', $calledFromFile); |
|
258 | - $calledFromFile = (!empty(self::$documentRoot) ? substr($calledFromFile, strlen(self::$documentRoot)) : $calledFromFile); |
|
259 | - $calledFromFile = trim($calledFromFile, '/'); |
|
260 | - } |
|
261 | - |
|
262 | - // Get function call |
|
263 | - $calledFromFunction = ''; |
|
264 | - if (isset($trace['function'])) { |
|
265 | - $calledFromFunction .= (isset($trace['class']) ? $trace['class'] : ''); |
|
266 | - $calledFromFunction .= (isset($trace['type']) ? $trace['type'] : ''); |
|
267 | - $calledFromFunction .= $trace['function'] . '()'; |
|
268 | - } |
|
269 | - |
|
270 | - // Return called from |
|
271 | - if ($calledFromFile) { |
|
272 | - return $calledFromFile; |
|
273 | - } elseif ($calledFromFunction) { |
|
274 | - return $calledFromFunction; |
|
275 | - } else { |
|
276 | - return 'Unable to get called from trace'; |
|
277 | - } |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @param mixed $data |
|
282 | - * |
|
283 | - * @return string |
|
284 | - */ |
|
285 | - public static function getDebugInformation($data, array $options = []) { |
|
286 | - $options = array_merge([ |
|
287 | - 'depth' => 25, |
|
288 | - 'indent' => 0, |
|
289 | - ], $options); |
|
290 | - |
|
291 | - $dataType = gettype($data); |
|
292 | - |
|
293 | - $methodName = 'getDebugInformation' . ucfirst(strtolower($dataType)); |
|
294 | - |
|
295 | - $result = 'No method found supporting data type: ' . $dataType; |
|
296 | - if ($dataType == 'string') { |
|
297 | - if (php_sapi_name() == 'cli') { |
|
298 | - $result = '"' . (string) $data . '"'; |
|
299 | - } else { |
|
300 | - $result = htmlentities($data); |
|
301 | - if ($data !== '' && $result === '') { |
|
302 | - $result = htmlentities(utf8_encode($data)); |
|
303 | - } |
|
304 | - |
|
305 | - $result = '<span style="color: #1299DA;">"</span>' . (string) $result . '<span style="color: #1299DA;">"</span>'; |
|
306 | - } |
|
307 | - } elseif (method_exists('\Xicrow\PhpDebug\Debugger', $methodName)) { |
|
308 | - $result = (string) self::$methodName($data, [ |
|
309 | - 'depth' => ($options['depth'] - 1), |
|
310 | - 'indent' => ($options['indent'] + 1), |
|
311 | - ]); |
|
312 | - } |
|
313 | - |
|
314 | - return $result; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * @return string |
|
319 | - */ |
|
320 | - private static function getDebugInformationNull() { |
|
321 | - return 'NULL'; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * @param boolean $data |
|
326 | - * |
|
327 | - * @return string |
|
328 | - */ |
|
329 | - private static function getDebugInformationBoolean($data) { |
|
330 | - return ($data ? 'TRUE' : 'FALSE'); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * @param integer $data |
|
335 | - * |
|
336 | - * @return string |
|
337 | - */ |
|
338 | - private static function getDebugInformationInteger($data) { |
|
339 | - return (string) $data; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * @param double $data |
|
344 | - * |
|
345 | - * @return string |
|
346 | - */ |
|
347 | - private static function getDebugInformationDouble($data) { |
|
348 | - return (string) $data; |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * @param array $data |
|
353 | - * |
|
354 | - * @return string |
|
355 | - */ |
|
356 | - private static function getDebugInformationArray($data, array $options = []) { |
|
357 | - $options = array_merge([ |
|
358 | - 'depth' => 25, |
|
359 | - 'indent' => 0, |
|
360 | - ], $options); |
|
361 | - |
|
362 | - $debugInfo = "["; |
|
363 | - |
|
364 | - $break = $end = null; |
|
365 | - if (!empty($data)) { |
|
366 | - $break = "\n" . str_repeat("\t", $options['indent']); |
|
367 | - $end = "\n" . str_repeat("\t", $options['indent'] - 1); |
|
368 | - } |
|
369 | - |
|
370 | - $datas = []; |
|
371 | - if ($options['depth'] >= 0) { |
|
372 | - foreach ($data as $key => $val) { |
|
373 | - // Sniff for globals as !== explodes in < 5.4 |
|
374 | - if ($key === 'GLOBALS' && is_array($val) && isset($val['GLOBALS'])) { |
|
375 | - $val = '[recursion]'; |
|
376 | - } elseif ($val !== $data) { |
|
377 | - $val = static::getDebugInformation($val, $options); |
|
378 | - } |
|
379 | - $datas[] = $break . static::getDebugInformation($key) . ' => ' . $val; |
|
380 | - } |
|
381 | - } else { |
|
382 | - $datas[] = $break . '[maximum depth reached]'; |
|
383 | - } |
|
384 | - |
|
385 | - return $debugInfo . implode(',', $datas) . $end . ']'; |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * @param object $data |
|
390 | - * |
|
391 | - * @return string |
|
392 | - */ |
|
393 | - private static function getDebugInformationObject($data, array $options = []) { |
|
394 | - $options = array_merge([ |
|
395 | - 'depth' => 25, |
|
396 | - 'indent' => 0, |
|
397 | - ], $options); |
|
398 | - |
|
399 | - $debugInfo = ''; |
|
400 | - $debugInfo .= 'object(' . get_class($data) . ') {'; |
|
401 | - |
|
402 | - $break = "\n" . str_repeat("\t", $options['indent']); |
|
403 | - $end = "\n" . str_repeat("\t", $options['indent'] - 1); |
|
404 | - |
|
405 | - if ($options['depth'] > 0 && method_exists($data, '__debugInfo')) { |
|
406 | - try { |
|
407 | - $debugArray = static::getDebugInformationArray($data->__debugInfo(), array_merge($options, [ |
|
408 | - 'depth' => ($options['depth'] - 1), |
|
409 | - ])); |
|
410 | - $debugInfo .= substr($debugArray, 1, -1); |
|
411 | - |
|
412 | - return $debugInfo . $end . '}'; |
|
413 | - } catch (\Exception $e) { |
|
414 | - $message = $e->getMessage(); |
|
415 | - |
|
416 | - return $debugInfo . "\n(unable to export object: $message)\n }"; |
|
417 | - } |
|
418 | - } |
|
419 | - |
|
420 | - if ($options['depth'] > 0) { |
|
421 | - $props = []; |
|
422 | - $objectVars = get_object_vars($data); |
|
423 | - foreach ($objectVars as $key => $value) { |
|
424 | - $value = static::getDebugInformation($value, array_merge($options, [ |
|
425 | - 'depth' => ($options['depth'] - 1), |
|
426 | - ])); |
|
427 | - $props[] = "$key => " . $value; |
|
428 | - } |
|
429 | - |
|
430 | - $ref = new \ReflectionObject($data); |
|
431 | - $filters = [ |
|
432 | - \ReflectionProperty::IS_PROTECTED => 'protected', |
|
433 | - \ReflectionProperty::IS_PRIVATE => 'private', |
|
434 | - ]; |
|
435 | - foreach ($filters as $filter => $visibility) { |
|
436 | - $reflectionProperties = $ref->getProperties($filter); |
|
437 | - foreach ($reflectionProperties as $reflectionProperty) { |
|
438 | - $reflectionProperty->setAccessible(true); |
|
439 | - $property = $reflectionProperty->getValue($data); |
|
440 | - |
|
441 | - $value = static::getDebugInformation($property, array_merge($options, [ |
|
442 | - 'depth' => ($options['depth'] - 1), |
|
443 | - ])); |
|
444 | - $key = $reflectionProperty->name; |
|
445 | - $props[] = sprintf('[%s] %s => %s', $visibility, $key, $value); |
|
446 | - } |
|
447 | - } |
|
448 | - |
|
449 | - $debugInfo .= $break . implode($break, $props) . $end; |
|
450 | - } |
|
451 | - $debugInfo .= '}'; |
|
452 | - |
|
453 | - return $debugInfo; |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * @param resource $data |
|
458 | - * |
|
459 | - * @return string |
|
460 | - */ |
|
461 | - private static function getDebugInformationResource($data) { |
|
462 | - return (string) $data . ' (' . get_resource_type($data) . ')'; |
|
463 | - } |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param array $options |
|
77 | + * |
|
78 | + * @codeCoverageIgnore |
|
79 | + */ |
|
80 | + public static function showTrace(array $options = []) { |
|
81 | + $options = array_merge([ |
|
82 | + 'trace_offset' => 0, |
|
83 | + 'reverse' => false, |
|
84 | + ], $options); |
|
85 | + |
|
86 | + $backtrace = ($options['reverse'] ? array_reverse(debug_backtrace()) : debug_backtrace()); |
|
87 | + |
|
88 | + $output = ''; |
|
89 | + $traceIndex = ($options['reverse'] ? 1 : count($backtrace)); |
|
90 | + foreach ($backtrace as $trace) { |
|
91 | + $output .= $traceIndex . ': '; |
|
92 | + $output .= self::getCalledFromTrace($trace); |
|
93 | + $output .= "\n"; |
|
94 | + |
|
95 | + $traceIndex += ($options['reverse'] ? 1 : -1); |
|
96 | + } |
|
97 | + |
|
98 | + self::output($output); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param string $class |
|
103 | + * @param bool $output |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public static function reflectClass($class, $output = true) { |
|
108 | + $data = ''; |
|
109 | + |
|
110 | + $reflectionClass = new \ReflectionClass($class); |
|
111 | + |
|
112 | + $comment = $reflectionClass->getDocComment(); |
|
113 | + if (!empty($comment)) { |
|
114 | + $data .= $comment; |
|
115 | + $data .= "\n"; |
|
116 | + } |
|
117 | + |
|
118 | + $data .= 'class ' . $reflectionClass->name . '{'; |
|
119 | + $firstElement = true; |
|
120 | + foreach ($reflectionClass->getProperties() as $reflectionProperty) { |
|
121 | + if (!$firstElement) { |
|
122 | + $data .= "\n"; |
|
123 | + } |
|
124 | + $firstElement = false; |
|
125 | + |
|
126 | + $data .= self::reflectClassProperty($class, $reflectionProperty->name, false); |
|
127 | + } |
|
128 | + |
|
129 | + foreach ($reflectionClass->getMethods() as $reflectionMethod) { |
|
130 | + if (!$firstElement) { |
|
131 | + $data .= "\n"; |
|
132 | + } |
|
133 | + $firstElement = false; |
|
134 | + |
|
135 | + $data .= self::reflectClassMethod($class, $reflectionMethod->name, false); |
|
136 | + } |
|
137 | + $data .= "\n"; |
|
138 | + $data .= '}'; |
|
139 | + |
|
140 | + if ($output) { |
|
141 | + self::output($data); |
|
142 | + } |
|
143 | + |
|
144 | + return $data; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $class |
|
149 | + * @param string $property |
|
150 | + * |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public static function reflectClassProperty($class, $property, $output = true) { |
|
154 | + $data = ''; |
|
155 | + |
|
156 | + $reflectionClass = new \ReflectionClass($class); |
|
157 | + $reflectionProperty = new \ReflectionProperty($class, $property); |
|
158 | + |
|
159 | + $defaultPropertyValues = $reflectionClass->getDefaultProperties(); |
|
160 | + |
|
161 | + $comment = $reflectionProperty->getDocComment(); |
|
162 | + if (!empty($comment)) { |
|
163 | + $data .= "\n"; |
|
164 | + $data .= "\t"; |
|
165 | + $data .= $comment; |
|
166 | + } |
|
167 | + |
|
168 | + $data .= "\n"; |
|
169 | + $data .= "\t"; |
|
170 | + $data .= ($reflectionProperty->isPublic() ? 'public ' : ''); |
|
171 | + $data .= ($reflectionProperty->isPrivate() ? 'private ' : ''); |
|
172 | + $data .= ($reflectionProperty->isProtected() ? 'protected ' : ''); |
|
173 | + $data .= ($reflectionProperty->isStatic() ? 'static ' : ''); |
|
174 | + $data .= '$' . $reflectionProperty->name; |
|
175 | + if (isset($defaultPropertyValues[$property])) { |
|
176 | + $data .= ' = ' . self::getDebugInformation($defaultPropertyValues[$property]); |
|
177 | + } |
|
178 | + $data .= ';'; |
|
179 | + |
|
180 | + if ($output) { |
|
181 | + self::output($data); |
|
182 | + } |
|
183 | + |
|
184 | + return $data; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @param string $class |
|
189 | + * @param string $method |
|
190 | + * |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + public static function reflectClassMethod($class, $method, $output = true) { |
|
194 | + $data = ''; |
|
195 | + |
|
196 | + $reflectionMethod = new \ReflectionMethod($class, $method); |
|
197 | + |
|
198 | + $comment = $reflectionMethod->getDocComment(); |
|
199 | + if (!empty($comment)) { |
|
200 | + $data .= "\n"; |
|
201 | + $data .= "\t"; |
|
202 | + $data .= $comment; |
|
203 | + } |
|
204 | + |
|
205 | + $data .= "\n"; |
|
206 | + $data .= "\t"; |
|
207 | + $data .= ($reflectionMethod->isPublic() ? 'public ' : ''); |
|
208 | + $data .= ($reflectionMethod->isPrivate() ? 'private ' : ''); |
|
209 | + $data .= ($reflectionMethod->isProtected() ? 'protected ' : ''); |
|
210 | + $data .= ($reflectionMethod->isStatic() ? 'static ' : ''); |
|
211 | + $data .= 'function ' . $reflectionMethod->name . '('; |
|
212 | + if ($reflectionMethod->getNumberOfParameters()) { |
|
213 | + foreach ($reflectionMethod->getParameters() as $reflectionMethodParameterIndex => $reflectionMethodParameter) { |
|
214 | + $data .= ($reflectionMethodParameterIndex > 0 ? ', ' : ''); |
|
215 | + $data .= '$' . $reflectionMethodParameter->name; |
|
216 | + if ($reflectionMethodParameter->isDefaultValueAvailable()) { |
|
217 | + $defaultValue = self::getDebugInformation($reflectionMethodParameter->getDefaultValue()); |
|
218 | + $defaultValue = str_replace(["\n", "\t"], '', $defaultValue); |
|
219 | + $data .= ' = ' . $defaultValue; |
|
220 | + } |
|
221 | + } |
|
222 | + } |
|
223 | + $data .= ') {}'; |
|
224 | + |
|
225 | + if ($output) { |
|
226 | + self::output($data); |
|
227 | + } |
|
228 | + |
|
229 | + return $data; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * @param int $index |
|
234 | + * |
|
235 | + * @return string |
|
236 | + */ |
|
237 | + public static function getCalledFrom($index = 0) { |
|
238 | + $backtrace = debug_backtrace(); |
|
239 | + |
|
240 | + if (!isset($backtrace[$index])) { |
|
241 | + return 'Unknown trace with index: ' . $index; |
|
242 | + } |
|
243 | + |
|
244 | + return self::getCalledFromTrace($backtrace[$index]); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @param array $trace |
|
249 | + * |
|
250 | + * @return string |
|
251 | + */ |
|
252 | + public static function getCalledFromTrace($trace) { |
|
253 | + // Get file and line number |
|
254 | + $calledFromFile = ''; |
|
255 | + if (isset($trace['file'])) { |
|
256 | + $calledFromFile .= $trace['file'] . ' line ' . $trace['line']; |
|
257 | + $calledFromFile = str_replace('\\', '/', $calledFromFile); |
|
258 | + $calledFromFile = (!empty(self::$documentRoot) ? substr($calledFromFile, strlen(self::$documentRoot)) : $calledFromFile); |
|
259 | + $calledFromFile = trim($calledFromFile, '/'); |
|
260 | + } |
|
261 | + |
|
262 | + // Get function call |
|
263 | + $calledFromFunction = ''; |
|
264 | + if (isset($trace['function'])) { |
|
265 | + $calledFromFunction .= (isset($trace['class']) ? $trace['class'] : ''); |
|
266 | + $calledFromFunction .= (isset($trace['type']) ? $trace['type'] : ''); |
|
267 | + $calledFromFunction .= $trace['function'] . '()'; |
|
268 | + } |
|
269 | + |
|
270 | + // Return called from |
|
271 | + if ($calledFromFile) { |
|
272 | + return $calledFromFile; |
|
273 | + } elseif ($calledFromFunction) { |
|
274 | + return $calledFromFunction; |
|
275 | + } else { |
|
276 | + return 'Unable to get called from trace'; |
|
277 | + } |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @param mixed $data |
|
282 | + * |
|
283 | + * @return string |
|
284 | + */ |
|
285 | + public static function getDebugInformation($data, array $options = []) { |
|
286 | + $options = array_merge([ |
|
287 | + 'depth' => 25, |
|
288 | + 'indent' => 0, |
|
289 | + ], $options); |
|
290 | + |
|
291 | + $dataType = gettype($data); |
|
292 | + |
|
293 | + $methodName = 'getDebugInformation' . ucfirst(strtolower($dataType)); |
|
294 | + |
|
295 | + $result = 'No method found supporting data type: ' . $dataType; |
|
296 | + if ($dataType == 'string') { |
|
297 | + if (php_sapi_name() == 'cli') { |
|
298 | + $result = '"' . (string) $data . '"'; |
|
299 | + } else { |
|
300 | + $result = htmlentities($data); |
|
301 | + if ($data !== '' && $result === '') { |
|
302 | + $result = htmlentities(utf8_encode($data)); |
|
303 | + } |
|
304 | + |
|
305 | + $result = '<span style="color: #1299DA;">"</span>' . (string) $result . '<span style="color: #1299DA;">"</span>'; |
|
306 | + } |
|
307 | + } elseif (method_exists('\Xicrow\PhpDebug\Debugger', $methodName)) { |
|
308 | + $result = (string) self::$methodName($data, [ |
|
309 | + 'depth' => ($options['depth'] - 1), |
|
310 | + 'indent' => ($options['indent'] + 1), |
|
311 | + ]); |
|
312 | + } |
|
313 | + |
|
314 | + return $result; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * @return string |
|
319 | + */ |
|
320 | + private static function getDebugInformationNull() { |
|
321 | + return 'NULL'; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * @param boolean $data |
|
326 | + * |
|
327 | + * @return string |
|
328 | + */ |
|
329 | + private static function getDebugInformationBoolean($data) { |
|
330 | + return ($data ? 'TRUE' : 'FALSE'); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * @param integer $data |
|
335 | + * |
|
336 | + * @return string |
|
337 | + */ |
|
338 | + private static function getDebugInformationInteger($data) { |
|
339 | + return (string) $data; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * @param double $data |
|
344 | + * |
|
345 | + * @return string |
|
346 | + */ |
|
347 | + private static function getDebugInformationDouble($data) { |
|
348 | + return (string) $data; |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * @param array $data |
|
353 | + * |
|
354 | + * @return string |
|
355 | + */ |
|
356 | + private static function getDebugInformationArray($data, array $options = []) { |
|
357 | + $options = array_merge([ |
|
358 | + 'depth' => 25, |
|
359 | + 'indent' => 0, |
|
360 | + ], $options); |
|
361 | + |
|
362 | + $debugInfo = "["; |
|
363 | + |
|
364 | + $break = $end = null; |
|
365 | + if (!empty($data)) { |
|
366 | + $break = "\n" . str_repeat("\t", $options['indent']); |
|
367 | + $end = "\n" . str_repeat("\t", $options['indent'] - 1); |
|
368 | + } |
|
369 | + |
|
370 | + $datas = []; |
|
371 | + if ($options['depth'] >= 0) { |
|
372 | + foreach ($data as $key => $val) { |
|
373 | + // Sniff for globals as !== explodes in < 5.4 |
|
374 | + if ($key === 'GLOBALS' && is_array($val) && isset($val['GLOBALS'])) { |
|
375 | + $val = '[recursion]'; |
|
376 | + } elseif ($val !== $data) { |
|
377 | + $val = static::getDebugInformation($val, $options); |
|
378 | + } |
|
379 | + $datas[] = $break . static::getDebugInformation($key) . ' => ' . $val; |
|
380 | + } |
|
381 | + } else { |
|
382 | + $datas[] = $break . '[maximum depth reached]'; |
|
383 | + } |
|
384 | + |
|
385 | + return $debugInfo . implode(',', $datas) . $end . ']'; |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * @param object $data |
|
390 | + * |
|
391 | + * @return string |
|
392 | + */ |
|
393 | + private static function getDebugInformationObject($data, array $options = []) { |
|
394 | + $options = array_merge([ |
|
395 | + 'depth' => 25, |
|
396 | + 'indent' => 0, |
|
397 | + ], $options); |
|
398 | + |
|
399 | + $debugInfo = ''; |
|
400 | + $debugInfo .= 'object(' . get_class($data) . ') {'; |
|
401 | + |
|
402 | + $break = "\n" . str_repeat("\t", $options['indent']); |
|
403 | + $end = "\n" . str_repeat("\t", $options['indent'] - 1); |
|
404 | + |
|
405 | + if ($options['depth'] > 0 && method_exists($data, '__debugInfo')) { |
|
406 | + try { |
|
407 | + $debugArray = static::getDebugInformationArray($data->__debugInfo(), array_merge($options, [ |
|
408 | + 'depth' => ($options['depth'] - 1), |
|
409 | + ])); |
|
410 | + $debugInfo .= substr($debugArray, 1, -1); |
|
411 | + |
|
412 | + return $debugInfo . $end . '}'; |
|
413 | + } catch (\Exception $e) { |
|
414 | + $message = $e->getMessage(); |
|
415 | + |
|
416 | + return $debugInfo . "\n(unable to export object: $message)\n }"; |
|
417 | + } |
|
418 | + } |
|
419 | + |
|
420 | + if ($options['depth'] > 0) { |
|
421 | + $props = []; |
|
422 | + $objectVars = get_object_vars($data); |
|
423 | + foreach ($objectVars as $key => $value) { |
|
424 | + $value = static::getDebugInformation($value, array_merge($options, [ |
|
425 | + 'depth' => ($options['depth'] - 1), |
|
426 | + ])); |
|
427 | + $props[] = "$key => " . $value; |
|
428 | + } |
|
429 | + |
|
430 | + $ref = new \ReflectionObject($data); |
|
431 | + $filters = [ |
|
432 | + \ReflectionProperty::IS_PROTECTED => 'protected', |
|
433 | + \ReflectionProperty::IS_PRIVATE => 'private', |
|
434 | + ]; |
|
435 | + foreach ($filters as $filter => $visibility) { |
|
436 | + $reflectionProperties = $ref->getProperties($filter); |
|
437 | + foreach ($reflectionProperties as $reflectionProperty) { |
|
438 | + $reflectionProperty->setAccessible(true); |
|
439 | + $property = $reflectionProperty->getValue($data); |
|
440 | + |
|
441 | + $value = static::getDebugInformation($property, array_merge($options, [ |
|
442 | + 'depth' => ($options['depth'] - 1), |
|
443 | + ])); |
|
444 | + $key = $reflectionProperty->name; |
|
445 | + $props[] = sprintf('[%s] %s => %s', $visibility, $key, $value); |
|
446 | + } |
|
447 | + } |
|
448 | + |
|
449 | + $debugInfo .= $break . implode($break, $props) . $end; |
|
450 | + } |
|
451 | + $debugInfo .= '}'; |
|
452 | + |
|
453 | + return $debugInfo; |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * @param resource $data |
|
458 | + * |
|
459 | + * @return string |
|
460 | + */ |
|
461 | + private static function getDebugInformationResource($data) { |
|
462 | + return (string) $data . ' (' . get_resource_type($data) . ')'; |
|
463 | + } |
|
464 | 464 | } |
@@ -85,10 +85,10 @@ discard block |
||
85 | 85 | |
86 | 86 | $backtrace = ($options['reverse'] ? array_reverse(debug_backtrace()) : debug_backtrace()); |
87 | 87 | |
88 | - $output = ''; |
|
88 | + $output = ''; |
|
89 | 89 | $traceIndex = ($options['reverse'] ? 1 : count($backtrace)); |
90 | 90 | foreach ($backtrace as $trace) { |
91 | - $output .= $traceIndex . ': '; |
|
91 | + $output .= $traceIndex.': '; |
|
92 | 92 | $output .= self::getCalledFromTrace($trace); |
93 | 93 | $output .= "\n"; |
94 | 94 | |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $data .= "\n"; |
116 | 116 | } |
117 | 117 | |
118 | - $data .= 'class ' . $reflectionClass->name . '{'; |
|
118 | + $data .= 'class '.$reflectionClass->name.'{'; |
|
119 | 119 | $firstElement = true; |
120 | 120 | foreach ($reflectionClass->getProperties() as $reflectionProperty) { |
121 | 121 | if (!$firstElement) { |
@@ -171,9 +171,9 @@ discard block |
||
171 | 171 | $data .= ($reflectionProperty->isPrivate() ? 'private ' : ''); |
172 | 172 | $data .= ($reflectionProperty->isProtected() ? 'protected ' : ''); |
173 | 173 | $data .= ($reflectionProperty->isStatic() ? 'static ' : ''); |
174 | - $data .= '$' . $reflectionProperty->name; |
|
174 | + $data .= '$'.$reflectionProperty->name; |
|
175 | 175 | if (isset($defaultPropertyValues[$property])) { |
176 | - $data .= ' = ' . self::getDebugInformation($defaultPropertyValues[$property]); |
|
176 | + $data .= ' = '.self::getDebugInformation($defaultPropertyValues[$property]); |
|
177 | 177 | } |
178 | 178 | $data .= ';'; |
179 | 179 | |
@@ -208,15 +208,15 @@ discard block |
||
208 | 208 | $data .= ($reflectionMethod->isPrivate() ? 'private ' : ''); |
209 | 209 | $data .= ($reflectionMethod->isProtected() ? 'protected ' : ''); |
210 | 210 | $data .= ($reflectionMethod->isStatic() ? 'static ' : ''); |
211 | - $data .= 'function ' . $reflectionMethod->name . '('; |
|
211 | + $data .= 'function '.$reflectionMethod->name.'('; |
|
212 | 212 | if ($reflectionMethod->getNumberOfParameters()) { |
213 | 213 | foreach ($reflectionMethod->getParameters() as $reflectionMethodParameterIndex => $reflectionMethodParameter) { |
214 | 214 | $data .= ($reflectionMethodParameterIndex > 0 ? ', ' : ''); |
215 | - $data .= '$' . $reflectionMethodParameter->name; |
|
215 | + $data .= '$'.$reflectionMethodParameter->name; |
|
216 | 216 | if ($reflectionMethodParameter->isDefaultValueAvailable()) { |
217 | 217 | $defaultValue = self::getDebugInformation($reflectionMethodParameter->getDefaultValue()); |
218 | 218 | $defaultValue = str_replace(["\n", "\t"], '', $defaultValue); |
219 | - $data .= ' = ' . $defaultValue; |
|
219 | + $data .= ' = '.$defaultValue; |
|
220 | 220 | } |
221 | 221 | } |
222 | 222 | } |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | $backtrace = debug_backtrace(); |
239 | 239 | |
240 | 240 | if (!isset($backtrace[$index])) { |
241 | - return 'Unknown trace with index: ' . $index; |
|
241 | + return 'Unknown trace with index: '.$index; |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | return self::getCalledFromTrace($backtrace[$index]); |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | // Get file and line number |
254 | 254 | $calledFromFile = ''; |
255 | 255 | if (isset($trace['file'])) { |
256 | - $calledFromFile .= $trace['file'] . ' line ' . $trace['line']; |
|
256 | + $calledFromFile .= $trace['file'].' line '.$trace['line']; |
|
257 | 257 | $calledFromFile = str_replace('\\', '/', $calledFromFile); |
258 | 258 | $calledFromFile = (!empty(self::$documentRoot) ? substr($calledFromFile, strlen(self::$documentRoot)) : $calledFromFile); |
259 | 259 | $calledFromFile = trim($calledFromFile, '/'); |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | if (isset($trace['function'])) { |
265 | 265 | $calledFromFunction .= (isset($trace['class']) ? $trace['class'] : ''); |
266 | 266 | $calledFromFunction .= (isset($trace['type']) ? $trace['type'] : ''); |
267 | - $calledFromFunction .= $trace['function'] . '()'; |
|
267 | + $calledFromFunction .= $trace['function'].'()'; |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | // Return called from |
@@ -290,19 +290,19 @@ discard block |
||
290 | 290 | |
291 | 291 | $dataType = gettype($data); |
292 | 292 | |
293 | - $methodName = 'getDebugInformation' . ucfirst(strtolower($dataType)); |
|
293 | + $methodName = 'getDebugInformation'.ucfirst(strtolower($dataType)); |
|
294 | 294 | |
295 | - $result = 'No method found supporting data type: ' . $dataType; |
|
295 | + $result = 'No method found supporting data type: '.$dataType; |
|
296 | 296 | if ($dataType == 'string') { |
297 | 297 | if (php_sapi_name() == 'cli') { |
298 | - $result = '"' . (string) $data . '"'; |
|
298 | + $result = '"'.(string) $data.'"'; |
|
299 | 299 | } else { |
300 | 300 | $result = htmlentities($data); |
301 | 301 | if ($data !== '' && $result === '') { |
302 | 302 | $result = htmlentities(utf8_encode($data)); |
303 | 303 | } |
304 | 304 | |
305 | - $result = '<span style="color: #1299DA;">"</span>' . (string) $result . '<span style="color: #1299DA;">"</span>'; |
|
305 | + $result = '<span style="color: #1299DA;">"</span>'.(string) $result.'<span style="color: #1299DA;">"</span>'; |
|
306 | 306 | } |
307 | 307 | } elseif (method_exists('\Xicrow\PhpDebug\Debugger', $methodName)) { |
308 | 308 | $result = (string) self::$methodName($data, [ |
@@ -363,8 +363,8 @@ discard block |
||
363 | 363 | |
364 | 364 | $break = $end = null; |
365 | 365 | if (!empty($data)) { |
366 | - $break = "\n" . str_repeat("\t", $options['indent']); |
|
367 | - $end = "\n" . str_repeat("\t", $options['indent'] - 1); |
|
366 | + $break = "\n".str_repeat("\t", $options['indent']); |
|
367 | + $end = "\n".str_repeat("\t", $options['indent'] - 1); |
|
368 | 368 | } |
369 | 369 | |
370 | 370 | $datas = []; |
@@ -376,13 +376,13 @@ discard block |
||
376 | 376 | } elseif ($val !== $data) { |
377 | 377 | $val = static::getDebugInformation($val, $options); |
378 | 378 | } |
379 | - $datas[] = $break . static::getDebugInformation($key) . ' => ' . $val; |
|
379 | + $datas[] = $break.static::getDebugInformation($key).' => '.$val; |
|
380 | 380 | } |
381 | 381 | } else { |
382 | - $datas[] = $break . '[maximum depth reached]'; |
|
382 | + $datas[] = $break.'[maximum depth reached]'; |
|
383 | 383 | } |
384 | 384 | |
385 | - return $debugInfo . implode(',', $datas) . $end . ']'; |
|
385 | + return $debugInfo.implode(',', $datas).$end.']'; |
|
386 | 386 | } |
387 | 387 | |
388 | 388 | /** |
@@ -397,23 +397,23 @@ discard block |
||
397 | 397 | ], $options); |
398 | 398 | |
399 | 399 | $debugInfo = ''; |
400 | - $debugInfo .= 'object(' . get_class($data) . ') {'; |
|
400 | + $debugInfo .= 'object('.get_class($data).') {'; |
|
401 | 401 | |
402 | - $break = "\n" . str_repeat("\t", $options['indent']); |
|
403 | - $end = "\n" . str_repeat("\t", $options['indent'] - 1); |
|
402 | + $break = "\n".str_repeat("\t", $options['indent']); |
|
403 | + $end = "\n".str_repeat("\t", $options['indent'] - 1); |
|
404 | 404 | |
405 | 405 | if ($options['depth'] > 0 && method_exists($data, '__debugInfo')) { |
406 | 406 | try { |
407 | 407 | $debugArray = static::getDebugInformationArray($data->__debugInfo(), array_merge($options, [ |
408 | 408 | 'depth' => ($options['depth'] - 1), |
409 | 409 | ])); |
410 | - $debugInfo .= substr($debugArray, 1, -1); |
|
410 | + $debugInfo .= substr($debugArray, 1, -1); |
|
411 | 411 | |
412 | - return $debugInfo . $end . '}'; |
|
412 | + return $debugInfo.$end.'}'; |
|
413 | 413 | } catch (\Exception $e) { |
414 | 414 | $message = $e->getMessage(); |
415 | 415 | |
416 | - return $debugInfo . "\n(unable to export object: $message)\n }"; |
|
416 | + return $debugInfo."\n(unable to export object: $message)\n }"; |
|
417 | 417 | } |
418 | 418 | } |
419 | 419 | |
@@ -424,7 +424,7 @@ discard block |
||
424 | 424 | $value = static::getDebugInformation($value, array_merge($options, [ |
425 | 425 | 'depth' => ($options['depth'] - 1), |
426 | 426 | ])); |
427 | - $props[] = "$key => " . $value; |
|
427 | + $props[] = "$key => ".$value; |
|
428 | 428 | } |
429 | 429 | |
430 | 430 | $ref = new \ReflectionObject($data); |
@@ -446,7 +446,7 @@ discard block |
||
446 | 446 | } |
447 | 447 | } |
448 | 448 | |
449 | - $debugInfo .= $break . implode($break, $props) . $end; |
|
449 | + $debugInfo .= $break.implode($break, $props).$end; |
|
450 | 450 | } |
451 | 451 | $debugInfo .= '}'; |
452 | 452 | |
@@ -459,6 +459,6 @@ discard block |
||
459 | 459 | * @return string |
460 | 460 | */ |
461 | 461 | private static function getDebugInformationResource($data) { |
462 | - return (string) $data . ' (' . get_resource_type($data) . ')'; |
|
462 | + return (string) $data.' ('.get_resource_type($data).')'; |
|
463 | 463 | } |
464 | 464 | } |