ExceptionCaster.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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
  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 $a, Stub $stub, bool $isNested, int $filter = 0)
  44. {
  45. return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
  46. }
  47. public static function castException(\Exception $e, array $a, Stub $stub, bool $isNested, int $filter = 0)
  48. {
  49. return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter);
  50. }
  51. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, bool $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 $a, Stub $stub, bool $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_debug_type($a[$xPrefix.'previous']);
  66. self::traceUnshift($b[$xPrefix.'trace'], $class, $b[$prefix.'file'], $b[$prefix.'line']);
  67. $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -\count($a[$trace]->value));
  68. }
  69. unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
  70. return $a;
  71. }
  72. public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, bool $isNested)
  73. {
  74. $sPrefix = "\0".SilencedErrorContext::class."\0";
  75. if (!isset($a[$s = $sPrefix.'severity'])) {
  76. return $a;
  77. }
  78. if (isset(self::$errorTypes[$a[$s]])) {
  79. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  80. }
  81. $trace = [[
  82. 'file' => $a[$sPrefix.'file'],
  83. 'line' => $a[$sPrefix.'line'],
  84. ]];
  85. if (isset($a[$sPrefix.'trace'])) {
  86. $trace = array_merge($trace, $a[$sPrefix.'trace']);
  87. }
  88. unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']);
  89. $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
  90. return $a;
  91. }
  92. public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, bool $isNested)
  93. {
  94. if (!$isNested) {
  95. return $a;
  96. }
  97. $stub->class = '';
  98. $stub->handle = 0;
  99. $frames = $trace->value;
  100. $prefix = Caster::PREFIX_VIRTUAL;
  101. $a = [];
  102. $j = \count($frames);
  103. if (0 > $i = $trace->sliceOffset) {
  104. $i = max(0, $j + $i);
  105. }
  106. if (!isset($trace->value[$i])) {
  107. return [];
  108. }
  109. $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
  110. $frames[] = ['function' => ''];
  111. $collapse = false;
  112. for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
  113. $f = $frames[$i];
  114. $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???';
  115. $frame = new FrameStub(
  116. [
  117. 'object' => $f['object'] ?? null,
  118. 'class' => $f['class'] ?? null,
  119. 'type' => $f['type'] ?? null,
  120. 'function' => $f['function'] ?? null,
  121. ] + $frames[$i - 1],
  122. false,
  123. true
  124. );
  125. $f = self::castFrameStub($frame, [], $frame, true);
  126. if (isset($f[$prefix.'src'])) {
  127. foreach ($f[$prefix.'src']->value as $label => $frame) {
  128. if (str_starts_with($label, "\0~collapse=0")) {
  129. if ($collapse) {
  130. $label = substr_replace($label, '1', 11, 1);
  131. } else {
  132. $collapse = true;
  133. }
  134. }
  135. $label = substr_replace($label, "title=Stack level $j.&", 2, 0);
  136. }
  137. $f = $frames[$i - 1];
  138. if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) {
  139. $frame->value['arguments'] = new ArgsStub($f['args'], $f['function'] ?? null, $f['class'] ?? null);
  140. }
  141. } elseif ('???' !== $lastCall) {
  142. $label = new ClassStub($lastCall);
  143. if (isset($label->attr['ellipsis'])) {
  144. $label->attr['ellipsis'] += 2;
  145. $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level $j.", 2, 0).$label->value.'()';
  146. } else {
  147. $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()';
  148. }
  149. } else {
  150. $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall;
  151. }
  152. $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame;
  153. $lastCall = $call;
  154. }
  155. if (null !== $trace->sliceLength) {
  156. $a = \array_slice($a, 0, $trace->sliceLength, true);
  157. }
  158. return $a;
  159. }
  160. public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, bool $isNested)
  161. {
  162. if (!$isNested) {
  163. return $a;
  164. }
  165. $f = $frame->value;
  166. $prefix = Caster::PREFIX_VIRTUAL;
  167. if (isset($f['file'], $f['line'])) {
  168. $cacheKey = $f;
  169. unset($cacheKey['object'], $cacheKey['args']);
  170. $cacheKey[] = self::$srcContext;
  171. $cacheKey = implode('-', $cacheKey);
  172. if (isset(self::$framesCache[$cacheKey])) {
  173. $a[$prefix.'src'] = self::$framesCache[$cacheKey];
  174. } else {
  175. if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
  176. $f['file'] = substr($f['file'], 0, -\strlen($match[0]));
  177. $f['line'] = (int) $match[1];
  178. }
  179. $src = $f['line'];
  180. $srcKey = $f['file'];
  181. $ellipsis = new LinkStub($srcKey, 0);
  182. $srcAttr = 'collapse='.(int) $ellipsis->inVendor;
  183. $ellipsisTail = $ellipsis->attr['ellipsis-tail'] ?? 0;
  184. $ellipsis = $ellipsis->attr['ellipsis'] ?? 0;
  185. if (is_file($f['file']) && 0 <= self::$srcContext) {
  186. if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
  187. $template = null;
  188. if (isset($f['object'])) {
  189. $template = $f['object'];
  190. } elseif ((new \ReflectionClass($f['class']))->isInstantiable()) {
  191. $template = unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
  192. }
  193. if (null !== $template) {
  194. $ellipsis = 0;
  195. $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
  196. $templateInfo = $template->getDebugInfo();
  197. if (isset($templateInfo[$f['line']])) {
  198. if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) {
  199. $templatePath = null;
  200. }
  201. if ($templateSrc) {
  202. $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
  203. $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
  204. }
  205. }
  206. }
  207. }
  208. if ($srcKey == $f['file']) {
  209. $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, 'php', $f['file'], $f);
  210. $srcKey .= ':'.$f['line'];
  211. if ($ellipsis) {
  212. $ellipsis += 1 + \strlen($f['line']);
  213. }
  214. }
  215. $srcAttr .= sprintf('&separator= &file=%s&line=%d', rawurlencode($f['file']), $f['line']);
  216. } else {
  217. $srcAttr .= '&separator=:';
  218. }
  219. $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : '';
  220. self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(["\0~$srcAttr\0$srcKey" => $src]);
  221. }
  222. }
  223. unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
  224. if ($frame->inTraceStub) {
  225. unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
  226. }
  227. foreach ($a as $k => $v) {
  228. if (!$v) {
  229. unset($a[$k]);
  230. }
  231. }
  232. if ($frame->keepArgs && !empty($f['args'])) {
  233. $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']);
  234. }
  235. return $a;
  236. }
  237. private static function filterExceptionArray(string $xClass, array $a, string $xPrefix, int $filter): array
  238. {
  239. if (isset($a[$xPrefix.'trace'])) {
  240. $trace = $a[$xPrefix.'trace'];
  241. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  242. } else {
  243. $trace = [];
  244. }
  245. if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
  246. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  247. self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
  248. }
  249. $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
  250. }
  251. if (empty($a[$xPrefix.'previous'])) {
  252. unset($a[$xPrefix.'previous']);
  253. }
  254. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  255. if (isset($a[Caster::PREFIX_PROTECTED.'message']) && str_contains($a[Caster::PREFIX_PROTECTED.'message'], "@anonymous\0")) {
  256. $a[Caster::PREFIX_PROTECTED.'message'] = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
  257. return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
  258. }, $a[Caster::PREFIX_PROTECTED.'message']);
  259. }
  260. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  261. $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
  262. }
  263. return $a;
  264. }
  265. private static function traceUnshift(array &$trace, ?string $class, string $file, int $line): void
  266. {
  267. if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
  268. return;
  269. }
  270. array_unshift($trace, [
  271. 'function' => $class ? 'new '.$class : null,
  272. 'file' => $file,
  273. 'line' => $line,
  274. ]);
  275. }
  276. private static function extractSource(string $srcLines, int $line, int $srcContext, string $lang, ?string $file, array $frame): EnumStub
  277. {
  278. $srcLines = explode("\n", $srcLines);
  279. $src = [];
  280. for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
  281. $src[] = ($srcLines[$i] ?? '')."\n";
  282. }
  283. if ($frame['function'] ?? false) {
  284. $stub = new CutStub(new \stdClass());
  285. $stub->class = (isset($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function'];
  286. $stub->type = Stub::TYPE_OBJECT;
  287. $stub->attr['cut_hash'] = true;
  288. $stub->attr['file'] = $frame['file'];
  289. $stub->attr['line'] = $frame['line'];
  290. try {
  291. $caller = isset($frame['class']) ? new \ReflectionMethod($frame['class'], $frame['function']) : new \ReflectionFunction($frame['function']);
  292. $stub->class .= ReflectionCaster::getSignature(ReflectionCaster::castFunctionAbstract($caller, [], $stub, true, Caster::EXCLUDE_VERBOSE));
  293. if ($f = $caller->getFileName()) {
  294. $stub->attr['file'] = $f;
  295. $stub->attr['line'] = $caller->getStartLine();
  296. }
  297. } catch (\ReflectionException $e) {
  298. // ignore fake class/function
  299. }
  300. $srcLines = ["\0~separator=\0" => $stub];
  301. } else {
  302. $stub = null;
  303. $srcLines = [];
  304. }
  305. $ltrim = 0;
  306. do {
  307. $pad = null;
  308. for ($i = $srcContext << 1; $i >= 0; --$i) {
  309. if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
  310. if (null === $pad) {
  311. $pad = $c;
  312. }
  313. if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
  314. break;
  315. }
  316. }
  317. }
  318. ++$ltrim;
  319. } while (0 > $i && null !== $pad);
  320. --$ltrim;
  321. foreach ($src as $i => $c) {
  322. if ($ltrim) {
  323. $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
  324. }
  325. $c = substr($c, 0, -1);
  326. if ($i !== $srcContext) {
  327. $c = new ConstStub('default', $c);
  328. } else {
  329. $c = new ConstStub($c, $stub ? 'in '.$stub->class : '');
  330. if (null !== $file) {
  331. $c->attr['file'] = $file;
  332. $c->attr['line'] = $line;
  333. }
  334. }
  335. $c->attr['lang'] = $lang;
  336. $srcLines[sprintf("\0~separator=› &%d\0", $i + $line - $srcContext)] = $c;
  337. }
  338. return new EnumStub($srcLines);
  339. }
  340. }