1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
4
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
11
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
12
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
13
|
|
|
* @author Marcio Zebedeu - [email protected] |
14
|
|
|
* @version 1.0.2 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Created by PhpStorm. |
19
|
|
|
* User: artphotografie |
20
|
|
|
* Date: 10/01/17 |
21
|
|
|
* Time: 08:19 |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Ballybran\Helpers\vardump; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @link http://www.yiiframework.com/ |
29
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
30
|
|
|
* @license http://www.yiiframework.com/license/ |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* BaseVarDumper provides concrete implementation for [[VarDumper]]. |
36
|
|
|
* |
37
|
|
|
* Do not use BaseVarDumper. Use [[VarDumper]] instead. |
38
|
|
|
* |
39
|
|
|
* @author Qiang Xue <[email protected]> |
40
|
|
|
* @since 2.0 |
41
|
|
|
*/ |
42
|
|
|
class BaseVarDumper |
43
|
|
|
{ |
44
|
|
|
private static $_objects; |
45
|
|
|
private static $_output; |
46
|
|
|
private static $_depth; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Displays a variable. |
51
|
|
|
* This method achieves the similar functionality as var_dump and print_r |
52
|
|
|
* but is more robust when handling complex objects such as Yii controllers. |
53
|
|
|
* @param mixed $var variable to be dumped |
54
|
|
|
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10. |
55
|
|
|
* @param boolean $highlight whether the result should be syntax-highlighted |
56
|
|
|
*/ |
57
|
|
|
public static function dump($var, $depth = 10, bool $highlight = false) |
58
|
|
|
{ |
59
|
|
|
echo static::dumpAsString($var, $depth, $highlight); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Dumps a variable in terms of a string. |
64
|
|
|
* This method achieves the similar functionality as var_dump and print_r |
65
|
|
|
* but is more robust when handling complex objects such as Yii controllers. |
66
|
|
|
* @param mixed $var variable to be dumped |
67
|
|
|
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10. |
68
|
|
|
* @param boolean $highlight whether the result should be syntax-highlighted |
69
|
|
|
* @return string the string representation of the variable |
70
|
|
|
*/ |
71
|
|
|
private static function dumpAsString($var, int $depth = 10, bool $highlight = false) |
72
|
|
|
{ |
73
|
|
|
self::$_output = ''; |
74
|
|
|
self::$_objects = []; |
75
|
|
|
self::$_depth = $depth; |
76
|
|
|
self::dumpInternal($var, 0); |
77
|
|
|
if ($highlight) { |
78
|
|
|
$result = highlight_string("<?php\n" . self::$_output, true); |
79
|
|
|
self::$_output = preg_replace('/<\\?php<br \\/>/', '', $result, 1); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return self::$_output; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param mixed $var variable to be dumped |
87
|
|
|
* @param integer $level depth level |
88
|
|
|
*/ |
89
|
|
|
private static function dumpInternal($var, $level) |
90
|
|
|
{ |
91
|
|
|
switch (gettype($var)) { |
92
|
|
|
case 'boolean': |
93
|
|
|
self::$_output .= $var ? 'true' : 'false'; |
94
|
|
|
break; |
95
|
|
|
case 'integer': |
96
|
|
|
self::$_output .= "$var"; |
97
|
|
|
break; |
98
|
|
|
case 'double': |
99
|
|
|
self::$_output .= "$var"; |
100
|
|
|
break; |
101
|
|
|
case 'string': |
102
|
|
|
self::$_output .= "'" . addslashes($var) . "'"; |
103
|
|
|
break; |
104
|
|
|
case 'resource': |
105
|
|
|
self::$_output .= '{resource}'; |
106
|
|
|
break; |
107
|
|
|
case 'NULL': |
108
|
|
|
self::$_output .= "null"; |
109
|
|
|
break; |
110
|
|
|
case 'unknown type': |
111
|
|
|
self::$_output .= '{unknown}'; |
112
|
|
|
break; |
113
|
|
|
case 'array': |
114
|
|
|
if (self::$_depth <= $level) { |
115
|
|
|
self::$_output .= '[...]'; |
116
|
|
|
} elseif (empty($var)) { |
117
|
|
|
self::$_output .= '[]'; |
118
|
|
|
} else { |
119
|
|
|
$keys = array_keys($var); |
120
|
|
|
$spaces = str_repeat(' ', $level * 4); |
121
|
|
|
self::$_output .= '['; |
122
|
|
|
foreach ($keys as $key) { |
123
|
|
|
self::$_output .= "\n" . $spaces . ' '; |
124
|
|
|
self::dumpInternal($key, 0); |
125
|
|
|
self::$_output .= ' => '; |
126
|
|
|
self::dumpInternal($var[$key], $level + 1); |
127
|
|
|
} |
128
|
|
|
self::$_output .= "\n" . $spaces . ']'; |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
|
case 'object': |
132
|
|
|
if (false !== ($id = array_search($var, self::$_objects, true))) { |
133
|
|
|
self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)'; |
134
|
|
|
} elseif (self::$_depth <= $level) { |
135
|
|
|
self::$_output .= get_class($var) . '(...)'; |
136
|
|
|
} else { |
137
|
|
|
$id = array_push(self::$_objects, $var); |
138
|
|
|
$className = get_class($var); |
139
|
|
|
$spaces = str_repeat(' ', $level * 4); |
140
|
|
|
self::$_output .= "$className#$id\n" . $spaces . '('; |
141
|
|
|
foreach ((array)$var as $key => $value) { |
142
|
|
|
$keyDisplay = strtr(trim($key), "\0", ':'); |
143
|
|
|
self::$_output .= "\n" . $spaces . " [$keyDisplay] => "; |
144
|
|
|
self::dumpInternal($value, $level + 1); |
145
|
|
|
} |
146
|
|
|
self::$_output .= "\n" . $spaces . ')'; |
147
|
|
|
} |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Exports a variable as a string representation. |
154
|
|
|
* |
155
|
|
|
* The string is a valid PHP expression that can be evaluated by PHP parser |
156
|
|
|
* and the evaluation result will give back the variable value. |
157
|
|
|
* |
158
|
|
|
* This method is similar to `var_export()`. The main difference is that |
159
|
|
|
* it generates more compact string representation using short array syntax. |
160
|
|
|
* |
161
|
|
|
* It also handles objects by using the PHP functions serialize() and unserialize(). |
162
|
|
|
* |
163
|
|
|
* PHP 5.4 or above is required to parse the exported value. |
164
|
|
|
* |
165
|
|
|
* @param mixed $var the variable to be exported. |
166
|
|
|
* @return string a string representation of the variable |
167
|
|
|
*/ |
168
|
|
|
public static function export($var) |
169
|
|
|
{ |
170
|
|
|
self::$_output = ''; |
171
|
|
|
self::exportInternal($var, 0); |
172
|
|
|
return self::$_output; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param mixed $var variable to be exported |
177
|
|
|
* @param integer $level depth level |
178
|
|
|
*/ |
179
|
|
|
private static function exportInternal($var, $level) |
180
|
|
|
{ |
181
|
|
|
switch (gettype($var)) { |
182
|
|
|
case 'NULL': |
183
|
|
|
self::$_output .= 'null'; |
184
|
|
|
break; |
185
|
|
|
case 'array': |
186
|
|
|
if (empty($var)) { |
187
|
|
|
self::$_output .= '[]'; |
188
|
|
|
} else { |
189
|
|
|
$keys = array_keys($var); |
190
|
|
|
$outputKeys = ($keys !== range(0, sizeof($var) - 1)); |
191
|
|
|
$spaces = str_repeat(' ', $level * 4); |
192
|
|
|
self::$_output .= '['; |
193
|
|
|
foreach ($keys as $key) { |
194
|
|
|
self::$_output .= "\n" . $spaces . ' '; |
195
|
|
|
if ($outputKeys) { |
196
|
|
|
self::exportInternal($key, 0); |
197
|
|
|
self::$_output .= ' => '; |
198
|
|
|
} |
199
|
|
|
self::exportInternal($var[$key], $level + 1); |
200
|
|
|
self::$_output .= ','; |
201
|
|
|
} |
202
|
|
|
self::$_output .= "\n" . $spaces . ']'; |
203
|
|
|
} |
204
|
|
|
break; |
205
|
|
|
case 'object': |
206
|
|
|
try { |
207
|
|
|
$output = 'unserialize(' . var_export(serialize($var), true) . ')'; |
208
|
|
|
} catch (\Exception $e) { |
209
|
|
|
// serialize may fail, for example: if object contains a `\Closure` instance |
210
|
|
|
// so we use regular `var_export()` as fallback |
211
|
|
|
$output = var_export($var, true); |
212
|
|
|
} |
213
|
|
|
self::$_output .= $output; |
214
|
|
|
break; |
215
|
|
|
default: |
216
|
|
|
self::$_output .= var_export($var, true); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|