Passed
Pull Request — master (#18)
by
unknown
02:00
created

functions-dev.php (2 issues)

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
13
namespace Koded\Stdlib;
14
15
/**
16
 * Dump the variable with extra info.
17
 *
18
 * @param mixed $var
19
 * @param string|null $label [optional]
20
 * @param bool $traceback [optional]
21
 * @param bool $die [optional]
22
 */
23
function dump($var, $label = null, bool $traceback = false, bool $die = true)
24
{
25
    $MESSAGE_CLI = "\n>>> %s[info] %s\n[type] %s\n[call] \x1b[1m%s\x1b[0m\n\x1b[2m%s\x1b[0m";
26
    $MESSAGE_HTM = '<span style="clear:both; color:black; font-size:11px;">[<b>%s</b>] %s (%s) %s</span>';
27
28
    list($position, $backtrace) = (function(array $backtrace) {
29
        if (isset($backtrace[1]) && '__invoke' === $backtrace[1]['function']) {
30
            $p = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . ':' . $backtrace[0]['line'];
31
        } else if ('dump' === $backtrace[0]['function'] && count($backtrace) > 1) {
32
            $p = $backtrace[1]['class'] ?? $backtrace[0]['file'] ?? __FILE__;
33
            $p .= $backtrace[1]['type'] ?? ' *';
34
            $p .= $backtrace[1]['function'] . ':' . $backtrace[0]['line'] ?? $backtrace[1]['line'];
35
        } else {
36
            $p = $backtrace[0]['class'] ?? $backtrace[0]['file'];
37
            $p .= $backtrace[0]['type'] ?? ':' . $backtrace[0]['line'];
38
            $p .= ' ' . $backtrace[0]['function'] . '()';
39
        }
40
41
        // backtrace
42
        $b = array_map(function($l) {
43
            $MESSAGE_BACKTRACE = "  File \"\x1b[36;2m%s\x1b[0m\", line \x1b[36;2m%d\x1b[0m\n    from %s\x1b[1m%s()\x1b[0m";
44
            return sprintf($MESSAGE_BACKTRACE, $l['file'] ?? '?', $l['line'] ?? -1, (($l['class'] ?? '') ? $l['class'] . '::' : ''), $l['function']);
45
        }, array_slice(array_reverse($backtrace), 0, -1));
46
47
        return [$p, join(PHP_EOL, $b)];
48
49
    })(debug_backtrace());
50
51
    $backtrace = ($traceback ? 'Traceback (most recent call last):' . PHP_EOL . $backtrace . PHP_EOL . str_repeat('-', 80) : date(DATE_COOKIE)) . PHP_EOL;
52
53
    if ('cli' === php_sapi_name()) {
54
        $output = sprintf($MESSAGE_CLI, $backtrace, var_export($label, true), gettype($var), $position, print_r($var, true));
55
    } elseif (strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') == 'XMLHTTPREQUEST') {
56
        $output = json_encode($var, JSON_UNESCAPED_SLASHES);
57
    } else {
58
        $format = function($var) {
59
            ob_start();
60
            var_dump($var);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var) looks like debug code. Are you sure you do not want to remove it?
Loading history...
61
            $dump = ob_get_contents();
62
            $dump = preg_replace('/<small>\/.*<\/small>/', '\\1', $dump);
63
            ob_end_clean();
64
65
            return $dump;
66
        };
67
68
        $output = sprintf($MESSAGE_HTM, $position, var_export($label, true), gettype($var), $format($var));
69
    }
70
71
    $die and die($output);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
    print $output;
73
}
74