comparison vendor/symfony/var-dumper/Caster/ExceptionCaster.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 $collapse = false;
131
132 for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
133 $f = $frames[$i];
134 $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???';
135
136 $frame = new FrameStub(
137 array(
138 'object' => isset($f['object']) ? $f['object'] : null,
139 'class' => isset($f['class']) ? $f['class'] : null,
140 'type' => isset($f['type']) ? $f['type'] : null,
141 'function' => isset($f['function']) ? $f['function'] : null,
142 ) + $frames[$i - 1],
143 false,
144 true
145 );
146 $f = self::castFrameStub($frame, array(), $frame, true);
147 if (isset($f[$prefix.'src'])) {
148 foreach ($f[$prefix.'src']->value as $label => $frame) {
149 if (0 === strpos($label, "\0~collapse=0")) {
150 if ($collapse) {
151 $label = substr_replace($label, '1', 11, 1);
152 } else {
153 $collapse = true;
154 }
155 }
156 $label = substr_replace($label, "title=Stack level $j.&", 2, 0);
157 }
158 $f = $frames[$i - 1];
159 if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) {
160 $frame->value['arguments'] = new ArgsStub($f['args'], isset($f['function']) ? $f['function'] : null, isset($f['class']) ? $f['class'] : null);
161 }
162 } elseif ('???' !== $lastCall) {
163 $label = new ClassStub($lastCall);
164 if (isset($label->attr['ellipsis'])) {
165 $label->attr['ellipsis'] += 2;
166 $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level $j.", 2, 0).$label->value.'()';
167 } else {
168 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()';
169 }
170 } else {
171 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall;
172 }
173 $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame;
174
175 $lastCall = $call;
176 }
177 if (null !== $trace->sliceLength) {
178 $a = array_slice($a, 0, $trace->sliceLength, true);
179 }
180
181 return $a;
182 }
183
184 public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested)
185 {
186 if (!$isNested) {
187 return $a;
188 }
189 $f = $frame->value;
190 $prefix = Caster::PREFIX_VIRTUAL;
191
192 if (isset($f['file'], $f['line'])) {
193 $cacheKey = $f;
194 unset($cacheKey['object'], $cacheKey['args']);
195 $cacheKey[] = self::$srcContext;
196 $cacheKey = implode('-', $cacheKey);
197
198 if (isset(self::$framesCache[$cacheKey])) {
199 $a[$prefix.'src'] = self::$framesCache[$cacheKey];
200 } else {
201 if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
202 $f['file'] = substr($f['file'], 0, -strlen($match[0]));
203 $f['line'] = (int) $match[1];
204 }
205 $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
206 $src = $f['line'];
207 $srcKey = $f['file'];
208 $ellipsis = new LinkStub($srcKey, 0);
209 $srcAttr = 'collapse='.(int) $ellipsis->inVendor;
210 $ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0;
211 $ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0;
212
213 if (file_exists($f['file']) && 0 <= self::$srcContext) {
214 if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
215 $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
216
217 $ellipsis = 0;
218 $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
219 $templateInfo = $template->getDebugInfo();
220 if (isset($templateInfo[$f['line']])) {
221 if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
222 $templatePath = null;
223 }
224 if ($templateSrc) {
225 $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
226 $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
227 }
228 }
229 }
230 if ($srcKey == $f['file']) {
231 $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
232 $srcKey .= ':'.$f['line'];
233 if ($ellipsis) {
234 $ellipsis += 1 + strlen($f['line']);
235 }
236 }
237 $srcAttr .= '&separator= ';
238 } else {
239 $srcAttr .= '&separator=:';
240 }
241 $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : '';
242 self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(array("\0~$srcAttr\0$srcKey" => $src));
243 }
244 }
245
246 unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
247 if ($frame->inTraceStub) {
248 unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
249 }
250 foreach ($a as $k => $v) {
251 if (!$v) {
252 unset($a[$k]);
253 }
254 }
255 if ($frame->keepArgs && !empty($f['args'])) {
256 $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']);
257 }
258
259 return $a;
260 }
261
262 private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
263 {
264 if (isset($a[$xPrefix.'trace'])) {
265 $trace = $a[$xPrefix.'trace'];
266 unset($a[$xPrefix.'trace']); // Ensures the trace is always last
267 } else {
268 $trace = array();
269 }
270
271 if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
272 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
273 self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
274 }
275 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
276 }
277 if (empty($a[$xPrefix.'previous'])) {
278 unset($a[$xPrefix.'previous']);
279 }
280 unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
281
282 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
283 $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
284 }
285
286 return $a;
287 }
288
289 private static function traceUnshift(&$trace, $class, $file, $line)
290 {
291 if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
292 return;
293 }
294 array_unshift($trace, array(
295 'function' => $class ? 'new '.$class : null,
296 'file' => $file,
297 'line' => $line,
298 ));
299 }
300
301 private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null)
302 {
303 $srcLines = explode("\n", $srcLines);
304 $src = array();
305
306 for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
307 $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
308 }
309
310 $srcLines = array();
311 $ltrim = 0;
312 do {
313 $pad = null;
314 for ($i = $srcContext << 1; $i >= 0; --$i) {
315 if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
316 if (null === $pad) {
317 $pad = $c;
318 }
319 if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
320 break;
321 }
322 }
323 }
324 ++$ltrim;
325 } while (0 > $i && null !== $pad);
326
327 --$ltrim;
328
329 foreach ($src as $i => $c) {
330 if ($ltrim) {
331 $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
332 }
333 $c = substr($c, 0, -1);
334 if ($i !== $srcContext) {
335 $c = new ConstStub('default', $c);
336 } else {
337 $c = new ConstStub($c, $title);
338 if (null !== $file) {
339 $c->attr['file'] = $file;
340 $c->attr['line'] = $line;
341 }
342 }
343 $c->attr['lang'] = $lang;
344 $srcLines[sprintf("\0~separator=› &%d\0", $i + $line - $srcContext)] = $c;
345 }
346
347 return new EnumStub($srcLines);
348 }
349 }