|
| 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\VarDumper\Exception\ThrowingCasterException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Casts common Exception classes to array representation. |
| 18 | + * |
| 19 | + * @author Nicolas Grekas <p@tchwork.com> |
| 20 | + */ |
| 21 | +class ExceptionCaster |
| 22 | +{ |
| 23 | + public static $traceArgs = true; |
| 24 | + public static $errorTypes = array( |
| 25 | + E_DEPRECATED => 'E_DEPRECATED', |
| 26 | + E_USER_DEPRECATED => 'E_USER_DEPRECATED', |
| 27 | + E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', |
| 28 | + E_ERROR => 'E_ERROR', |
| 29 | + E_WARNING => 'E_WARNING', |
| 30 | + E_PARSE => 'E_PARSE', |
| 31 | + E_NOTICE => 'E_NOTICE', |
| 32 | + E_CORE_ERROR => 'E_CORE_ERROR', |
| 33 | + E_CORE_WARNING => 'E_CORE_WARNING', |
| 34 | + E_COMPILE_ERROR => 'E_COMPILE_ERROR', |
| 35 | + E_COMPILE_WARNING => 'E_COMPILE_WARNING', |
| 36 | + E_USER_ERROR => 'E_USER_ERROR', |
| 37 | + E_USER_WARNING => 'E_USER_WARNING', |
| 38 | + E_USER_NOTICE => 'E_USER_NOTICE', |
| 39 | + E_STRICT => 'E_STRICT', |
| 40 | + ); |
| 41 | + |
| 42 | + public static function castException(\Exception $e, array $a) |
| 43 | + { |
| 44 | + $trace = $a["\0Exception\0trace"]; |
| 45 | + unset($a["\0Exception\0trace"]); // Ensures the trace is always last |
| 46 | + |
| 47 | + static::filterTrace($trace, static::$traceArgs); |
| 48 | + |
| 49 | + if (null !== $trace) { |
| 50 | + $a["\0Exception\0trace"] = $trace; |
| 51 | + } |
| 52 | + if (empty($a["\0Exception\0previous"])) { |
| 53 | + unset($a["\0Exception\0previous"]); |
| 54 | + } |
| 55 | + unset($a["\0Exception\0string"], $a["\0+\0xdebug_message"], $a["\0+\0__destructorException"]); |
| 56 | + |
| 57 | + return $a; |
| 58 | + } |
| 59 | + |
| 60 | + public static function castErrorException(\ErrorException $e, array $a) |
| 61 | + { |
| 62 | + if (isset($a[$s = "\0*\0severity"], self::$errorTypes[$a[$s]])) { |
| 63 | + $a[$s] = self::$errorTypes[$a[$s]]; |
| 64 | + } |
| 65 | + |
| 66 | + return $a; |
| 67 | + } |
| 68 | + |
| 69 | + public static function castThrowingCasterException(ThrowingCasterException $e, array $a) |
| 70 | + { |
| 71 | + $b = (array) $a["\0Exception\0previous"]; |
| 72 | + |
| 73 | + array_splice($b["\0Exception\0trace"], count($a["\0Exception\0trace"])); |
| 74 | + |
| 75 | + $t = static::$traceArgs; |
| 76 | + static::$traceArgs = false; |
| 77 | + $b = static::castException($a["\0Exception\0previous"], $b); |
| 78 | + static::$traceArgs = $t; |
| 79 | + |
| 80 | + if (empty($a["\0*\0message"])) { |
| 81 | + $a["\0*\0message"] = "Unexpected exception thrown from a caster: ".get_class($a["\0Exception\0previous"]); |
| 82 | + } |
| 83 | + |
| 84 | + if (isset($b["\0*\0message"])) { |
| 85 | + $a["\0~\0message"] = $b["\0*\0message"]; |
| 86 | + } |
| 87 | + if (isset($b["\0*\0file"])) { |
| 88 | + $a["\0~\0file"] = $b["\0*\0file"]; |
| 89 | + } |
| 90 | + if (isset($b["\0*\0line"])) { |
| 91 | + $a["\0~\0line"] = $b["\0*\0line"]; |
| 92 | + } |
| 93 | + if (isset($b["\0Exception\0trace"])) { |
| 94 | + $a["\0~\0trace"] = $b["\0Exception\0trace"]; |
| 95 | + } |
| 96 | + |
| 97 | + unset($a["\0Exception\0trace"], $a["\0Exception\0previous"], $a["\0*\0code"], $a["\0*\0file"], $a["\0*\0line"]); |
| 98 | + |
| 99 | + return $a; |
| 100 | + } |
| 101 | + |
| 102 | + public static function filterTrace(&$trace, $dumpArgs, $offset = 0) |
| 103 | + { |
| 104 | + if (0 > $offset || empty($trace[$offset])) { |
| 105 | + return $trace = null; |
| 106 | + } |
| 107 | + |
| 108 | + $t = $trace[$offset]; |
| 109 | + |
| 110 | + if (empty($t['class']) && isset($t['function'])) { |
| 111 | + if ('user_error' === $t['function'] || 'trigger_error' === $t['function']) { |
| 112 | + ++$offset; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if ($offset) { |
| 117 | + array_splice($trace, 0, $offset); |
| 118 | + } |
| 119 | + |
| 120 | + foreach ($trace as &$t) { |
| 121 | + $t = array( |
| 122 | + 'call' => (isset($t['class']) ? $t['class'].$t['type'] : '').$t['function'].'()', |
| 123 | + 'file' => isset($t['line']) ? "{$t['file']}:{$t['line']}" : '', |
| 124 | + 'args' => &$t['args'], |
| 125 | + ); |
| 126 | + |
| 127 | + if (!isset($t['args']) || !$dumpArgs) { |
| 128 | + unset($t['args']); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments