Passed
Push — develop ( 7eec78...e0880f )
by Jens
02:21
created

GlobalFunctions::cliDump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by: Jens
4
 * Date: 31-3-2018
5
 */
6
7
namespace CloudControl\Cms\util;
8
9
10
class GlobalFunctions
11
{
12
    /**
13
     * Dumps a var_dump of the passed arguments with <pre> tags surrounding it.
14
     * Dies afterwards
15
     *
16
     * @param mixed ...    The data to be displayed
17
     */
18
    public static function dump($debug_backtrace)
19
    {
20
        if (PHP_SAPI === 'cli') {
21
            self::cliDump($debug_backtrace);
22
        }
23
24
        ob_end_clean();
25
        ob_start();
26
        echo <<<END
27
<!DOCTYPE html>
28
<html>
29
<head>
30
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
31
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" />
32
<script>hljs.initHighlightingOnLoad();</script>
33
<style>code.php.hljs{margin: -0.4em 0;}code.active{background-color:#e0e0e0;}code:before{content:attr(data-line);}code.active:before{color:#c00;font-weight:bold;}</style>
34
</head>
35
<body>
36
END;
37
38
        echo '<div>Dump: ' . $debug_backtrace['file'] . ':<b>' . $debug_backtrace['line'] . "</b></div>";
39
        echo '<pre>';
40
        $i = 0;
41
        foreach (func_get_args() as $data) {
42
            if ($i = 0) {
43
                continue;
44
            }
45
            echo '<code>';
46
            var_dump($data);
47
            echo '</code>';
48
            $i += 1;
49
        }
50
        echo '</pre>';
51
        echo <<<END
52
</body>
53
</html>
54
END;
55
        exit;
56
    }
57
58
    /**
59
     * Minify the html for the outputbuffer
60
     *
61
     * @param $buffer
62
     * @return mixed
63
     */
64
    public static function sanitizeOutput($buffer)
65
    {
66
        if (!isset($_GET['unsanitized'])) {
67
            $search = array(
68
                '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
69
                '/[^\S ]+\</s',     // strip whitespaces before tags, except space
70
                '/(\s)+/s',         // shorten multiple whitespace sequences
71
                '/<!--(.|\s)*?-->/' // Remove HTML comments
72
            );
73
74
            $replace = array(
75
                '>',
76
                '<',
77
                '\\1',
78
                ''
79
            );
80
81
            $buffer = preg_replace($search, $replace, $buffer);
82
83
            return $buffer;
84
        }
85
86
        return $buffer;
87
    }
88
89
    /**
90
     * Convert all values of an array to utf8
91
     *
92
     * @param $array
93
     * @return array
94
     */
95
    public static function utf8Convert($array)
96
    {
97
        array_walk_recursive($array, function (&$item) {
98
            if (!mb_detect_encoding($item, 'utf-8', true)) {
99
                $item = utf8_encode($item);
100
            }
101
        });
102
103
        return $array;
104
    }
105
106
    /**
107
     * @param $debug_backtrace
108
     */
109
    public static function cliDump($debug_backtrace)
110
    {
111
        echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
112
        foreach (func_get_args() as $data) {
113
            var_dump($data);
114
        }
115
    }
116
}