vendor/symfony/var-dumper/Caster/ExceptionCaster.php line 114

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