4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Caster;
14 use Symfony\Component\Debug\Exception\SilencedErrorContext;
15 use Symfony\Component\VarDumper\Cloner\Stub;
16 use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
19 * Casts common Exception classes to array representation.
21 * @author Nicolas Grekas <p@tchwork.com>
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',
32 E_WARNING => 'E_WARNING',
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',
45 private static $framesCache = array();
47 public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
49 return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
52 public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0)
54 return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter);
57 public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
59 if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) {
60 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
66 public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
68 $trace = Caster::PREFIX_VIRTUAL.'trace';
69 $prefix = Caster::PREFIX_PROTECTED;
70 $xPrefix = "\0Exception\0";
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));
78 unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
83 public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, $isNested)
85 $sPrefix = "\0".SilencedErrorContext::class."\0";
87 if (!isset($a[$s = $sPrefix.'severity'])) {
91 if (isset(self::$errorTypes[$a[$s]])) {
92 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
96 'file' => $a[$sPrefix.'file'],
97 'line' => $a[$sPrefix.'line'],
100 if (isset($a[$sPrefix.'trace'])) {
101 $trace = array_merge($trace, $a[$sPrefix.'trace']);
104 unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']);
105 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
110 public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
117 $frames = $trace->value;
118 $prefix = Caster::PREFIX_VIRTUAL;
121 $j = \count($frames);
122 if (0 > $i = $trace->sliceOffset) {
123 $i = max(0, $j + $i);
125 if (!isset($trace->value[$i])) {
128 $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
129 $frames[] = array('function' => '');
132 for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
134 $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???';
136 $frame = new FrameStub(
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,
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")) {
151 $label = substr_replace($label, '1', 11, 1);
156 $label = substr_replace($label, "title=Stack level $j.&", 2, 0);
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);
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.'()';
168 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()';
171 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall;
173 $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame;
177 if (null !== $trace->sliceLength) {
178 $a = \array_slice($a, 0, $trace->sliceLength, true);
184 public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested)
190 $prefix = Caster::PREFIX_VIRTUAL;
192 if (isset($f['file'], $f['line'])) {
194 unset($cacheKey['object'], $cacheKey['args']);
195 $cacheKey[] = self::$srcContext;
196 $cacheKey = implode('-', $cacheKey);
198 if (isset(self::$framesCache[$cacheKey])) {
199 $a[$prefix.'src'] = self::$framesCache[$cacheKey];
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];
205 $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
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;
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']));
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;
225 $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
226 $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
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'];
234 $ellipsis += 1 + \strlen($f['line']);
237 $srcAttr .= '&separator= ';
239 $srcAttr .= '&separator=:';
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));
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']);
250 foreach ($a as $k => $v) {
255 if ($frame->keepArgs && !empty($f['args'])) {
256 $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']);
262 private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
264 if (isset($a[$xPrefix.'trace'])) {
265 $trace = $a[$xPrefix.'trace'];
266 unset($a[$xPrefix.'trace']); // Ensures the trace is always last
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']);
275 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
277 if (empty($a[$xPrefix.'previous'])) {
278 unset($a[$xPrefix.'previous']);
280 unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
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']);
289 private static function traceUnshift(&$trace, $class, $file, $line)
291 if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
294 array_unshift($trace, array(
295 'function' => $class ? 'new '.$class : null,
301 private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null)
303 $srcLines = explode("\n", $srcLines);
306 for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
307 $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
314 for ($i = $srcContext << 1; $i >= 0; --$i) {
315 if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
319 if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
325 } while (0 > $i && null !== $pad);
329 foreach ($src as $i => $c) {
331 $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
333 $c = substr($c, 0, -1);
334 if ($i !== $srcContext) {
335 $c = new ConstStub('default', $c);
337 $c = new ConstStub($c, $title);
338 if (null !== $file) {
339 $c->attr['file'] = $file;
340 $c->attr['line'] = $line;
343 $c->attr['lang'] = $lang;
344 $srcLines[sprintf("\0~separator=› &%d\0", $i + $line - $srcContext)] = $c;
347 return new EnumStub($srcLines);