Passed
Push — master ( 3a2d72...a747ff )
by Jens
02:50
created

GlobalFunctions::dump()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 29
nc 4
nop 1
dl 0
loc 35
rs 8.8571
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 ($debug_backtrace['args'] as $data) {
42
            echo '<code>';
43
            var_dump($data);
44
            echo '</code>';
45
            $i += 1;
46
        }
47
        echo '</pre>';
48
        echo <<<END
49
</body>
50
</html>
51
END;
52
        exit;
53
    }
54
55
    /**
56
     * Minify the html for the outputbuffer
57
     *
58
     * @param $buffer
59
     * @return mixed
60
     */
61
    public static function sanitizeOutput($buffer)
62
    {
63
        if (!isset($_GET['unsanitized'])) {
64
            $search = array(
65
                '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
66
                '/[^\S ]+\</s',     // strip whitespaces before tags, except space
67
                '/(\s)+/s',         // shorten multiple whitespace sequences
68
                '/<!--(.|\s)*?-->/' // Remove HTML comments
69
            );
70
71
            $replace = array(
72
                '>',
73
                '<',
74
                '\\1',
75
                ''
76
            );
77
78
            $buffer = preg_replace($search, $replace, $buffer);
79
80
            return $buffer;
81
        }
82
83
        return $buffer;
84
    }
85
86
    /**
87
     * Convert all values of an array to utf8
88
     *
89
     * @param $array
90
     * @return array
91
     */
92
    public static function utf8Convert($array)
93
    {
94
        array_walk_recursive($array, function (&$item) {
95
            if (!mb_detect_encoding($item, 'utf-8', true)) {
96
                $item = utf8_encode($item);
97
            }
98
        });
99
100
        return $array;
101
    }
102
103
    /**
104
     * @param $debug_backtrace
105
     */
106
    public static function cliDump($debug_backtrace)
107
    {
108
        echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
109
        foreach (func_get_args() as $data) {
110
            var_dump($data);
111
        }
112
    }
113
}