|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TVarDumper class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Qiang Xue <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Prado\Util; |
|
11
|
|
|
|
|
12
|
|
|
use Prado\Exceptions\TInvalidDataValueException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* TVarDumper class. |
|
16
|
|
|
* |
|
17
|
|
|
* TVarDumper is intended to replace the buggy PHP function var_dump and print_r. |
|
18
|
|
|
* It can correctly identify the recursively referenced objects in a complex |
|
19
|
|
|
* object structure. It also has a recursive depth control to avoid indefinite |
|
20
|
|
|
* recursive display of some peculiar variables. |
|
21
|
|
|
* |
|
22
|
|
|
* TVarDumper can be used as follows, |
|
23
|
|
|
* <code> |
|
24
|
|
|
* echo TVarDumper::dump($var); |
|
25
|
|
|
* </code> |
|
26
|
|
|
* |
|
27
|
|
|
* @author Qiang Xue <[email protected]> |
|
28
|
|
|
* @since 3.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
class TVarDumper |
|
31
|
|
|
{ |
|
32
|
|
|
private static $_objects; |
|
33
|
|
|
private static $_output; |
|
34
|
|
|
private static $_depth; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Converts a variable into a string representation. |
|
38
|
|
|
* This method achieves the similar functionality as var_dump and print_r |
|
39
|
|
|
* but is more robust when handling complex objects such as PRADO controls. |
|
40
|
|
|
* @param mixed $var variable to be dumped |
|
41
|
|
|
* @param int $depth maximum depth that the dumper should go into the variable. Defaults to 10. |
|
42
|
|
|
* @param bool $highlight wether to highlight th resulting string |
|
43
|
|
|
* @return string the string representation of the variable |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function dump($var, $depth = 10, $highlight = false) |
|
46
|
|
|
{ |
|
47
|
|
|
self::$_output = ''; |
|
48
|
|
|
self::$_objects = []; |
|
49
|
|
|
self::$_depth = $depth; |
|
50
|
|
|
self::dumpInternal($var, 0); |
|
51
|
|
|
if ($highlight) { |
|
52
|
|
|
$result = highlight_string("<?php\n" . self::$_output, true); |
|
53
|
|
|
return preg_replace('/<\\?php<br \\/>/', '', $result, 1); |
|
54
|
|
|
} else { |
|
55
|
|
|
return self::$_output; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private static function dumpInternal($var, $level) |
|
60
|
|
|
{ |
|
61
|
|
|
switch (gettype($var)) { |
|
62
|
|
|
case 'boolean': |
|
63
|
|
|
self::$_output .= $var ? 'true' : 'false'; |
|
64
|
|
|
break; |
|
65
|
|
|
case 'integer': |
|
66
|
|
|
self::$_output .= "$var"; |
|
67
|
|
|
break; |
|
68
|
|
|
case 'double': |
|
69
|
|
|
self::$_output .= "$var"; |
|
70
|
|
|
break; |
|
71
|
|
|
case 'string': |
|
72
|
|
|
self::$_output .= "'" . addslashes($var) . "'"; |
|
73
|
|
|
break; |
|
74
|
|
|
case 'resource': |
|
75
|
|
|
self::$_output .= '{resource}'; |
|
76
|
|
|
break; |
|
77
|
|
|
case 'NULL': |
|
78
|
|
|
self::$_output .= "null"; |
|
79
|
|
|
break; |
|
80
|
|
|
case 'unknown type': |
|
81
|
|
|
self::$_output .= '{unknown}'; |
|
82
|
|
|
break; |
|
83
|
|
|
case 'array': |
|
84
|
|
|
if (self::$_depth <= $level) { |
|
85
|
|
|
self::$_output .= 'array(...)'; |
|
86
|
|
|
} elseif (empty($var)) { |
|
87
|
|
|
self::$_output .= 'array()'; |
|
88
|
|
|
} else { |
|
89
|
|
|
$keys = array_keys($var); |
|
90
|
|
|
$spaces = str_repeat(' ', $level * 4); |
|
91
|
|
|
self::$_output .= "array\n" . $spaces . '('; |
|
92
|
|
|
foreach ($keys as $key) { |
|
93
|
|
|
self::$_output .= "\n" . $spaces . ' '; |
|
94
|
|
|
self::dumpInternal($key, 0); |
|
95
|
|
|
self::$_output .= ' => '; |
|
96
|
|
|
self::dumpInternal($var[$key], $level + 1); |
|
97
|
|
|
} |
|
98
|
|
|
self::$_output .= "\n" . $spaces . ')'; |
|
99
|
|
|
} |
|
100
|
|
|
break; |
|
101
|
|
|
case 'object': |
|
102
|
|
|
if (($id = array_search($var, self::$_objects, true)) !== false) { |
|
103
|
|
|
self::$_output .= $var::class . '#' . ($id + 1) . '(...)'; |
|
104
|
|
|
} elseif (self::$_depth <= $level) { |
|
105
|
|
|
self::$_output .= $var::class . '(...)'; |
|
106
|
|
|
} else { |
|
107
|
|
|
$id = array_push(self::$_objects, $var); |
|
108
|
|
|
$className = $var::class; |
|
109
|
|
|
$spaces = str_repeat(' ', $level * 4); |
|
110
|
|
|
self::$_output .= "$className#$id\n" . $spaces . '('; |
|
111
|
|
|
if ('__PHP_Incomplete_Class' !== get_class($var) && method_exists($var, '__debugInfo')) { |
|
112
|
|
|
$members = $var->__debugInfo(); |
|
113
|
|
|
if (!is_array($dumpValues)) { |
|
|
|
|
|
|
114
|
|
|
throw new TInvalidDataValueException('vardumper_not_array'); |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
$members = (array) $var; |
|
118
|
|
|
} |
|
119
|
|
|
foreach ($members as $key => $value) { |
|
120
|
|
|
$keyDisplay = strtr(trim($key), ["\0" => ':']); |
|
121
|
|
|
self::$_output .= "\n" . $spaces . " [$keyDisplay] => "; |
|
122
|
|
|
self::$_output .= self::dumpInternal($value, $level + 1); |
|
123
|
|
|
} |
|
124
|
|
|
self::$_output .= "\n" . $spaces . ')'; |
|
125
|
|
|
} |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|