kodedphp /
stdlib
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Koded package. |
||
| 5 | * |
||
| 6 | * (c) Mihail Binev <[email protected]> |
||
| 7 | * |
||
| 8 | * Please view the LICENSE distributed with this source code |
||
| 9 | * for the full copyright and license information. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Koded\Stdlib; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Dump the variable with extra info. |
||
| 16 | * |
||
| 17 | * @param mixed $var |
||
| 18 | * @param string|null $label [optional] |
||
| 19 | * @param bool $traceback [optional] |
||
| 20 | * @param bool $die [optional] |
||
| 21 | */ |
||
| 22 | function dump($var, $label = null, bool $traceback = false, bool $die = true) |
||
| 23 | { |
||
| 24 | $MESSAGE_CLI = "\n>>> %s[type] %s\n[info] %s\n[line] \x1b[1m%s\x1b[0m\n\x1b[2m%s\x1b[0m"; |
||
| 25 | $MESSAGE_HTM = '<span style="clear:both; color:black; font-size:11px;">[<b>%s</b>] %s (%s) %s</span>'; |
||
| 26 | |||
| 27 | list($position, $backtrace) = (function(array $backtrace) { |
||
| 28 | if (isset($backtrace[1]) && '__invoke' === $backtrace[1]['function']) { |
||
| 29 | $p = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . ':' . $backtrace[0]['line']; |
||
| 30 | } else if ('dump' === $backtrace[0]['function'] && \count($backtrace) > 1) { |
||
| 31 | $p = $backtrace[1]['class'] ?? $backtrace[0]['file'] ?? __FILE__; |
||
| 32 | $p .= $backtrace[1]['type'] ?? ' *'; |
||
| 33 | $p .= $backtrace[1]['function'] . ':' . $backtrace[0]['line'] ?? $backtrace[1]['line']; |
||
| 34 | } else { |
||
| 35 | $p = $backtrace[0]['class'] ?? $backtrace[0]['file']; |
||
| 36 | $p .= $backtrace[0]['type'] ?? ':' . $backtrace[0]['line']; |
||
| 37 | $p .= ' ' . $backtrace[0]['function'] . '()'; |
||
| 38 | } |
||
| 39 | |||
| 40 | // backtrace |
||
| 41 | $b = \array_map(function($l) { |
||
| 42 | $MESSAGE_BACKTRACE = " File \"\x1b[36;2m%s\x1b[0m\", line \x1b[36;2m%d\x1b[0m\n from %s\x1b[1m%s()\x1b[0m"; |
||
| 43 | return sprintf($MESSAGE_BACKTRACE, $l['file'] ?? '?', $l['line'] ?? -1, (($l['class'] ?? '') ? $l['class'] . '::' : ''), $l['function']); |
||
| 44 | }, \array_slice(\array_reverse($backtrace), 0, -1)); |
||
| 45 | |||
| 46 | return [$p, \join(PHP_EOL, $b)]; |
||
| 47 | |||
| 48 | })(\debug_backtrace()); |
||
| 49 | |||
| 50 | $backtrace = ($traceback ? 'Traceback (most recent call last):' . PHP_EOL . $backtrace . PHP_EOL . \str_repeat('-', 80) : \date(DATE_COOKIE)) . PHP_EOL; |
||
| 51 | |||
| 52 | if ('cli' === \php_sapi_name()) { |
||
| 53 | $output = \sprintf($MESSAGE_CLI, $backtrace, \gettype($var), \var_export($label, true), $position, \print_r($var, true)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | } elseif (\strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') == 'XMLHTTPREQUEST') { |
||
| 55 | $output = \json_encode($var, JSON_UNESCAPED_SLASHES); |
||
| 56 | } else { |
||
| 57 | $format = function($var) { |
||
| 58 | \ob_start(); |
||
| 59 | \var_dump($var); |
||
|
0 ignored issues
–
show
|
|||
| 60 | $dump = \ob_get_contents(); |
||
| 61 | $dump = \preg_replace('/<small>\/.*<\/small>/', '\\1', $dump); |
||
| 62 | \ob_end_clean(); |
||
| 63 | |||
| 64 | return $dump; |
||
| 65 | }; |
||
| 66 | |||
| 67 | $output = \sprintf($MESSAGE_HTM, $position, \gettype($var), \var_export($label, true), $format($var)); |
||
| 68 | } |
||
| 69 | |||
| 70 | $die and die($output); |
||
|
0 ignored issues
–
show
|
|||
| 71 | print $output; |
||
| 72 | } |
||
| 73 |