Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/var-dumper/Caster/ExceptionCaster.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\VarDumper\Caster; | |
13 | |
14 use Symfony\Component\Debug\Exception\SilencedErrorContext; | |
15 use Symfony\Component\VarDumper\Exception\ThrowingCasterException; | |
16 use Symfony\Component\VarDumper\Cloner\Stub; | |
17 | |
18 /** | |
19 * Casts common Exception classes to array representation. | |
20 * | |
21 * @author Nicolas Grekas <p@tchwork.com> | |
22 */ | |
23 class ExceptionCaster | |
24 { | |
25 public static $srcContext = 1; | |
26 public static $traceArgs = true; | |
27 public static $errorTypes = array( | |
28 E_DEPRECATED => 'E_DEPRECATED', | |
29 E_USER_DEPRECATED => 'E_USER_DEPRECATED', | |
30 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', | |
31 E_ERROR => 'E_ERROR', | |
32 E_WARNING => 'E_WARNING', | |
33 E_PARSE => 'E_PARSE', | |
34 E_NOTICE => 'E_NOTICE', | |
35 E_CORE_ERROR => 'E_CORE_ERROR', | |
36 E_CORE_WARNING => 'E_CORE_WARNING', | |
37 E_COMPILE_ERROR => 'E_COMPILE_ERROR', | |
38 E_COMPILE_WARNING => 'E_COMPILE_WARNING', | |
39 E_USER_ERROR => 'E_USER_ERROR', | |
40 E_USER_WARNING => 'E_USER_WARNING', | |
41 E_USER_NOTICE => 'E_USER_NOTICE', | |
42 E_STRICT => 'E_STRICT', | |
43 ); | |
44 | |
45 private static $framesCache = array(); | |
46 | |
47 public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0) | |
48 { | |
49 return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter); | |
50 } | |
51 | |
52 public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0) | |
53 { | |
54 return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter); | |
55 } | |
56 | |
57 public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested) | |
58 { | |
59 if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) { | |
60 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); | |
61 } | |
62 | |
63 return $a; | |
64 } | |
65 | |
66 public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested) | |
67 { | |
68 $trace = Caster::PREFIX_VIRTUAL.'trace'; | |
69 $prefix = Caster::PREFIX_PROTECTED; | |
70 $xPrefix = "\0Exception\0"; | |
71 | |
72 if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) { | |
73 $b = (array) $a[$xPrefix.'previous']; | |
74 self::traceUnshift($b[$xPrefix.'trace'], get_class($a[$xPrefix.'previous']), $b[$prefix.'file'], $b[$prefix.'line']); | |
75 $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -count($a[$trace]->value)); | |
76 } | |
77 | |
78 unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']); | |
79 | |
80 return $a; | |
81 } | |
82 | |
83 public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, $isNested) | |
84 { | |
85 $sPrefix = "\0".SilencedErrorContext::class."\0"; | |
86 | |
87 if (!isset($a[$s = $sPrefix.'severity'])) { | |
88 return $a; | |
89 } | |
90 | |
91 if (isset(self::$errorTypes[$a[$s]])) { | |
92 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); | |
93 } | |
94 | |
95 $trace = array(array( | |
96 'file' => $a[$sPrefix.'file'], | |
97 'line' => $a[$sPrefix.'line'], | |
98 )); | |
99 | |
100 if (isset($a[$sPrefix.'trace'])) { | |
101 $trace = array_merge($trace, $a[$sPrefix.'trace']); | |
102 } | |
103 | |
104 unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']); | |
105 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs); | |
106 | |
107 return $a; | |
108 } | |
109 | |
110 public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested) | |
111 { | |
112 if (!$isNested) { | |
113 return $a; | |
114 } | |
115 $stub->class = ''; | |
116 $stub->handle = 0; | |
117 $frames = $trace->value; | |
118 $prefix = Caster::PREFIX_VIRTUAL; | |
119 | |
120 $a = array(); | |
121 $j = count($frames); | |
122 if (0 > $i = $trace->sliceOffset) { | |
123 $i = max(0, $j + $i); | |
124 } | |
125 if (!isset($trace->value[$i])) { | |
126 return array(); | |
127 } | |
128 $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : ''; | |
129 $frames[] = array('function' => ''); | |
130 | |
131 for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) { | |
132 $f = $frames[$i]; | |
133 $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???'; | |
134 | |
135 $frame = new FrameStub( | |
136 array( | |
137 'object' => isset($f['object']) ? $f['object'] : null, | |
138 'class' => isset($f['class']) ? $f['class'] : null, | |
139 'type' => isset($f['type']) ? $f['type'] : null, | |
140 'function' => isset($f['function']) ? $f['function'] : null, | |
141 ) + $frames[$i - 1], | |
142 false, | |
143 true | |
144 ); | |
145 $f = self::castFrameStub($frame, array(), $frame, true); | |
146 if (isset($f[$prefix.'src'])) { | |
147 foreach ($f[$prefix.'src']->value as $label => $frame) { | |
148 $label = substr_replace($label, "title=Stack level $j.&", 2, 0); | |
149 } | |
150 $f = $frames[$i - 1]; | |
151 if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) { | |
152 $frame->value['arguments'] = new ArgsStub($f['args'], isset($f['function']) ? $f['function'] : null, isset($f['class']) ? $f['class'] : null); | |
153 } | |
154 } elseif ('???' !== $lastCall) { | |
155 $label = new ClassStub($lastCall); | |
156 if (isset($label->attr['ellipsis'])) { | |
157 $label->attr['ellipsis'] += 2; | |
158 $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level $j.", 2, 0).$label->value.'()'; | |
159 } else { | |
160 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()'; | |
161 } | |
162 } else { | |
163 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall; | |
164 } | |
165 $a[$label] = $frame; | |
166 | |
167 $lastCall = $call; | |
168 } | |
169 if (null !== $trace->sliceLength) { | |
170 $a = array_slice($a, 0, $trace->sliceLength, true); | |
171 } | |
172 | |
173 return $a; | |
174 } | |
175 | |
176 public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested) | |
177 { | |
178 if (!$isNested) { | |
179 return $a; | |
180 } | |
181 $f = $frame->value; | |
182 $prefix = Caster::PREFIX_VIRTUAL; | |
183 | |
184 if (isset($f['file'], $f['line'])) { | |
185 $cacheKey = $f; | |
186 unset($cacheKey['object'], $cacheKey['args']); | |
187 $cacheKey[] = self::$srcContext; | |
188 $cacheKey = implode('-', $cacheKey); | |
189 | |
190 if (isset(self::$framesCache[$cacheKey])) { | |
191 $a[$prefix.'src'] = self::$framesCache[$cacheKey]; | |
192 } else { | |
193 if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) { | |
194 $f['file'] = substr($f['file'], 0, -strlen($match[0])); | |
195 $f['line'] = (int) $match[1]; | |
196 } | |
197 $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null; | |
198 $src = $f['line']; | |
199 $srcKey = $f['file']; | |
200 $ellipsis = (new LinkStub($srcKey, 0))->attr; | |
201 $ellipsisTail = isset($ellipsis['ellipsis-tail']) ? $ellipsis['ellipsis-tail'] : 0; | |
202 $ellipsis = isset($ellipsis['ellipsis']) ? $ellipsis['ellipsis'] : 0; | |
203 | |
204 if (file_exists($f['file']) && 0 <= self::$srcContext) { | |
205 if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) { | |
206 $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class'])); | |
207 | |
208 $ellipsis = 0; | |
209 $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : ''); | |
210 $templateInfo = $template->getDebugInfo(); | |
211 if (isset($templateInfo[$f['line']])) { | |
212 if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) { | |
213 $templatePath = null; | |
214 } | |
215 if ($templateSrc) { | |
216 $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath); | |
217 $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']]; | |
218 } | |
219 } | |
220 } | |
221 if ($srcKey == $f['file']) { | |
222 $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']); | |
223 $srcKey .= ':'.$f['line']; | |
224 if ($ellipsis) { | |
225 $ellipsis += 1 + strlen($f['line']); | |
226 } | |
227 } | |
228 } | |
229 $srcAttr = $ellipsis ? 'ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : ''; | |
230 self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(array("\0~$srcAttr\0$srcKey" => $src)); | |
231 } | |
232 } | |
233 | |
234 unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']); | |
235 if ($frame->inTraceStub) { | |
236 unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']); | |
237 } | |
238 foreach ($a as $k => $v) { | |
239 if (!$v) { | |
240 unset($a[$k]); | |
241 } | |
242 } | |
243 if ($frame->keepArgs && !empty($f['args'])) { | |
244 $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']); | |
245 } | |
246 | |
247 return $a; | |
248 } | |
249 | |
250 private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter) | |
251 { | |
252 if (isset($a[$xPrefix.'trace'])) { | |
253 $trace = $a[$xPrefix.'trace']; | |
254 unset($a[$xPrefix.'trace']); // Ensures the trace is always last | |
255 } else { | |
256 $trace = array(); | |
257 } | |
258 | |
259 if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) { | |
260 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) { | |
261 self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']); | |
262 } | |
263 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs); | |
264 } | |
265 if (empty($a[$xPrefix.'previous'])) { | |
266 unset($a[$xPrefix.'previous']); | |
267 } | |
268 unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']); | |
269 | |
270 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) { | |
271 $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']); | |
272 } | |
273 | |
274 return $a; | |
275 } | |
276 | |
277 private static function traceUnshift(&$trace, $class, $file, $line) | |
278 { | |
279 if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) { | |
280 return; | |
281 } | |
282 array_unshift($trace, array( | |
283 'function' => $class ? 'new '.$class : null, | |
284 'file' => $file, | |
285 'line' => $line, | |
286 )); | |
287 } | |
288 | |
289 private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null) | |
290 { | |
291 $srcLines = explode("\n", $srcLines); | |
292 $src = array(); | |
293 | |
294 for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) { | |
295 $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n"; | |
296 } | |
297 | |
298 $srcLines = array(); | |
299 $ltrim = 0; | |
300 do { | |
301 $pad = null; | |
302 for ($i = $srcContext << 1; $i >= 0; --$i) { | |
303 if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) { | |
304 if (null === $pad) { | |
305 $pad = $c; | |
306 } | |
307 if ((' ' !== $c && "\t" !== $c) || $pad !== $c) { | |
308 break; | |
309 } | |
310 } | |
311 } | |
312 ++$ltrim; | |
313 } while (0 > $i && null !== $pad); | |
314 | |
315 --$ltrim; | |
316 | |
317 foreach ($src as $i => $c) { | |
318 if ($ltrim) { | |
319 $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t"); | |
320 } | |
321 $c = substr($c, 0, -1); | |
322 if ($i !== $srcContext) { | |
323 $c = new ConstStub('default', $c); | |
324 } else { | |
325 $c = new ConstStub($c, $title); | |
326 if (null !== $file) { | |
327 $c->attr['file'] = $file; | |
328 $c->attr['line'] = $line; | |
329 } | |
330 } | |
331 $c->attr['lang'] = $lang; | |
332 $srcLines[sprintf("\0~%d\0", $i + $line - $srcContext)] = $c; | |
333 } | |
334 | |
335 return new EnumStub($srcLines); | |
336 } | |
337 } |