1 | <?php |
||
16 | class TextDumper extends AbstractDumper { |
||
17 | |||
18 | protected static $depth = 0; |
||
19 | |||
20 | protected static $stack = []; |
||
21 | |||
22 | public static function dumpNull() { |
||
25 | |||
26 | public static function dumpBoolean( $var ) { |
||
29 | |||
30 | public static function dumpInteger( $var ) { |
||
33 | |||
34 | public static function dumpFloat( $var ) { |
||
37 | |||
38 | public static function dumpString( $str ) { |
||
39 | $enc = mb_detect_encoding($str, ['UTF-8', 'WINDOWS-1252', 'ISO-8859-1', 'ASCII'], true); |
||
40 | $enc = ($enc == 'ASCII') ? '' : "; $enc"; |
||
41 | return sprintf('string(%d%s) "%s"', strlen($str), $enc, $str); |
||
42 | } |
||
43 | |||
44 | public static function dumpArray( array $arr ) { |
||
45 | |||
46 | static::$depth++; |
||
47 | |||
48 | $item = sprintf("array(%d) {\n", count($arr)); |
||
49 | foreach( $arr as $k => $v ) { |
||
50 | $item .= sprintf("%s[%s] => %s\n", str_repeat("\t", static::$depth), $k, static::dump($v, false)); |
||
51 | } |
||
52 | $item .= str_repeat("\t", static::$depth - 1). "}"; |
||
53 | |||
54 | static::$depth--; |
||
55 | |||
56 | return $item; |
||
57 | |||
58 | } |
||
59 | |||
60 | public static function dumpObject( $obj ) { |
||
61 | |||
62 | if( $item = static::recursionCheck($obj) ) { |
||
63 | return $item; |
||
64 | } |
||
65 | elseif( $obj instanceof Dumpable ) { |
||
66 | $item = $obj->dump(get_called_class(), static::$depth + 1); |
||
67 | if( $item ) |
||
68 | return $item; |
||
69 | } |
||
70 | elseif( $obj instanceof \Exception ) |
||
71 | return static::dumpException($obj); |
||
72 | |||
73 | static::$stack[] = $obj; |
||
74 | |||
75 | static::$depth++; |
||
76 | |||
77 | $item = get_class($obj). " {\n"; |
||
78 | |||
79 | $item .= static::dumpObjectProperties($obj); |
||
80 | |||
81 | $item .= str_repeat("\t", static::$depth - 1). "}"; |
||
82 | |||
83 | static::$depth--; |
||
84 | |||
85 | array_pop(static::$stack); |
||
86 | |||
87 | return $item; |
||
88 | |||
89 | } |
||
90 | |||
91 | public static function dumpException( \Exception $e ) { |
||
92 | |||
93 | $item = get_class($e); |
||
94 | |||
95 | $meta = [ |
||
96 | 'message' => $e->getMessage(), |
||
97 | 'code' => $e->getCode(), |
||
98 | 'file' => $e->getFile(), |
||
99 | 'line' => $e->getLine(), |
||
100 | 'trace' => static::dumpTrace($e->getTrace()), |
||
101 | 'previous' => $e->getPrevious(), |
||
102 | ]; |
||
103 | |||
104 | if( $e instanceof \ErrorException ) { |
||
105 | $lookup = [ |
||
106 | E_ERROR => 'ERROR', |
||
107 | E_WARNING => 'WARNING', |
||
108 | E_PARSE => 'PARSE', |
||
109 | E_NOTICE => 'NOTICE', |
||
110 | E_CORE_ERROR => 'CORE_ERROR', |
||
111 | E_CORE_WARNING => 'CORE_WARNING', |
||
112 | E_COMPILE_ERROR => 'COMPILE_ERROR', |
||
113 | E_COMPILE_WARNING => 'COMPILE_WARNING', |
||
114 | E_USER_ERROR => 'USER_ERROR', |
||
115 | E_USER_WARNING => 'USER_WARNING', |
||
116 | E_USER_NOTICE => 'USER_NOTICE', |
||
117 | E_STRICT => 'STRICT', |
||
118 | E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR', |
||
119 | E_DEPRECATED => 'DEPRECATED', |
||
120 | E_USER_DEPRECATED => 'USER_DEPRECATED', |
||
121 | ]; |
||
122 | $meta = array_merge([ |
||
123 | 'severity' => $lookup[$e->getSeverity()], |
||
124 | ], $meta); |
||
125 | } |
||
126 | |||
127 | $item .= static::dumpMeta($meta); |
||
128 | |||
129 | return $item; |
||
130 | |||
131 | } |
||
132 | |||
133 | public static function dumpResource( $resource ) { |
||
134 | |||
135 | $type = get_resource_type($resource); |
||
136 | |||
137 | $item = (string) $resource; |
||
138 | $item = sprintf("resource(%s; %s)", substr($item, strpos($item, '#')), $type); |
||
139 | |||
140 | // try and get some additional info about the resource |
||
141 | switch( $type ) { |
||
142 | case 'stream': |
||
143 | $item .= static::dumpMeta( |
||
144 | stream_get_meta_data($resource) |
||
145 | ); |
||
146 | break; |
||
147 | |||
148 | case 'curl': |
||
149 | $item .= static::dumpMeta( |
||
150 | curl_getinfo($resource) |
||
151 | ); |
||
152 | break; |
||
153 | |||
154 | } |
||
155 | |||
156 | return $item; |
||
157 | |||
158 | } |
||
159 | |||
160 | protected static function dumpMeta( $meta ) { |
||
161 | |||
162 | static::$depth++; |
||
163 | |||
164 | $width = max(array_map('strlen', array_keys($meta))) + 1; |
||
165 | |||
166 | $item = " {\n"; |
||
167 | foreach( $meta as $k => $v ) { |
||
168 | $item .= sprintf("%s%s: %s\n", str_repeat("\t", static::$depth), str_pad(ucwords(str_replace('_', ' ', $k)), $width) , static::dump($v, false)); |
||
169 | } |
||
170 | $item .= str_repeat("\t", static::$depth - 1). "}"; |
||
171 | |||
172 | static::$depth--; |
||
173 | |||
174 | return $item; |
||
175 | |||
176 | } |
||
177 | |||
178 | protected static function dumpTrace( array $trace ) { |
||
179 | |||
180 | $lines = []; |
||
181 | |||
182 | foreach( $trace as $i => $frame ) { |
||
183 | |||
184 | $line = ''; |
||
185 | |||
186 | if( isset($frame['class']) ) |
||
187 | $line .= $frame['class']. $frame['type']; |
||
188 | |||
189 | $line .= $frame['function']. '()'; |
||
190 | |||
191 | if( isset($frame['file']) ) { |
||
192 | $line .= ' ['. $frame['file']; |
||
193 | if( isset($frame['line']) ) |
||
194 | $line .= ':'. $frame['line']; |
||
195 | $line .= ']'; |
||
196 | } |
||
197 | |||
198 | $lines[] = $line; |
||
199 | |||
200 | } |
||
201 | |||
202 | return $lines; |
||
203 | |||
204 | } |
||
205 | |||
206 | protected static function dumpObjectProperties( $obj ) { |
||
207 | |||
208 | // we use reflection to access all the object's properties (public, protected and private) |
||
209 | $r = new \ReflectionObject($obj); |
||
210 | |||
211 | $item = ''; |
||
212 | |||
213 | foreach( static::getClassProperties($r) as $p ) { |
||
214 | $p->setAccessible(true); |
||
215 | $item .= sprintf("%s%s: %s\n", str_repeat("\t", static::$depth), $p->name, static::dump($p->getValue($obj), false)); |
||
216 | } |
||
217 | |||
218 | return $item; |
||
219 | |||
220 | } |
||
221 | |||
222 | protected static function getClassProperties( \ReflectionClass $class ) { |
||
239 | |||
240 | protected static function recursionCheck( $obj ) { |
||
241 | |||
242 | if( end(static::$stack) === $obj ) |
||
243 | return '**SELF**'; |
||
244 | |||
245 | elseif( in_array($obj, static::$stack) ) |
||
246 | return '**RECURSION**'; |
||
247 | |||
251 | |||
252 | } |
||
253 | |||
254 | // EOF |