-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump_debug.php
152 lines (122 loc) · 6.18 KB
/
dump_debug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* A function to var_dump given args (in a pretty way) and stop all processes at the end.
*
* @param mixed $expression
* @param mixed $_ - optional [default = null]
*/
function ddd($expression, $_ = null) {
$extraExpressions = array_slice(func_get_args(), 1);
if (empty($extraExpressions)) {
dump_debug($expression);
} else {
array_map(function($arg) {
dump_debug($arg);
}, func_get_args());
}
die;
}
function dump_debug($input, $collapse = false) {
$recursive = function($data, $level=0) use (&$recursive, $collapse) {
global $argv;
$isTerminal = isset($argv);
if (!$isTerminal && $level == 0 && !defined("DUMP_DEBUG_SCRIPT")) {
define("DUMP_DEBUG_SCRIPT", true);
echo '<meta charset="UTF-8">';
echo '<script language="Javascript">function toggleDisplay(id) {';
echo 'var state = document.getElementById("container"+id).style.display;';
echo 'document.getElementById("container"+id).style.display = state == "inline" ? "none" : "inline";';
echo 'document.getElementById("plus"+id).style.display = state == "inline" ? "inline" : "none";';
echo '}</script>'."\n";
}
$type = !is_string($data) && is_callable($data) ? "Callable" : ucfirst(gettype($data));
$type_data = null;
$type_color = null;
$type_length = null;
switch ($type) {
case "String":
$type_color = "green";
$type_length = strlen($data);
$type_data = "\"" . htmlentities($data) . "\""; break;
case "Double":
case "Float":
$type = "Float";
$type_color = "#0099c5";
$type_length = strlen($data);
$type_data = htmlentities($data); break;
case "Integer":
$type_color = "red";
$type_length = strlen($data);
$type_data = htmlentities($data); break;
case "Boolean":
$type = "Bool";
$type_color = "#92008d";
$type_length = null;
$type_data = $data ? "TRUE" : "FALSE"; break;
case "NULL":
$type_length = null; break;
case "Array":
$type_length = count($data);
}
if (in_array($type, array("Object", "Array"))) {
$notEmpty = false;
foreach($data as $key => $value) {
if (!$notEmpty) {
$notEmpty = true;
if ($isTerminal) {
echo $type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " . ($type === "Object" ? "{" : "[");
} else {
$id = substr(md5(rand().":".$key.":".$level), 0, 8);
echo "<a href=\"javascript:toggleDisplay('". $id ."');\" style=\"text-decoration:none\">";
echo "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "")
. ($type === "Object" ? " {</span>" : " [</span>");
echo "</a>";
echo "<span id=\"plus". $id ."\" style=\"display: " . ($collapse ? "inline" : "none") . ";\">";
echo "<a href=\"javascript:toggleDisplay('". $id ."');\" style=\"text-decoration:none\">";
echo ($type === "Object" ? "<span style='color:#666666'>...}</span>" : "<span style='color:#666666'>...]</span>");
echo "</a></span>";
echo "<div id=\"container". $id ."\" style=\"display: " . ($collapse ? "" : "inline") . ";\">";
}
for ($i=0; $i <= $level; $i++) {
echo $isTerminal ? " " : "<span style='color:black'></span> ";
}
echo $isTerminal ? "\n" : "<br />";
}
for ($i=0; $i <= $level; $i++) {
echo $isTerminal ? " " : "<span style='color:black'></span> ";
}
echo $isTerminal ? "[" . $key . "] => " : "<span style='color:black'>[" . $key . "] => </span>";
call_user_func($recursive, $value, $level+1);
}
if ($notEmpty) {
if ($level === 0 | $level > 1) {
$whitespace = $isTerminal ? " " : " ";
$indent = str_repeat($whitespace, $level);
echo $isTerminal ? ($type === "Object" ? $indent . "}" : $indent . "]") :
($type === "Object" ? "<span style='color:#666666'>" . $indent . "}</span>" : "<span style='color:#666666'>" . $indent . "]</span>");
} else {
for ($i=0; $i < $level; $i++) {
echo $isTerminal ? ($type === "Object" ? " }" : " ]") :
($type === "Object" ? "<span style='color:#666666'> }</span>" : "<span style='color:#666666'> ]</span>");
}
}
if (!$isTerminal) {
echo "</div>";
}
} else {
echo $isTerminal ?
$type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " :
"<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span> ";
}
} else {
echo $isTerminal ?
$type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " :
"<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span> ";
if ($type_data != null) {
echo $isTerminal ? $type_data : "<span style='color:" . $type_color . "'>" . $type_data . "</span>";
}
}
echo $isTerminal ? "\n" : "<br />";
};
call_user_func($recursive, $input);
}