ExceptionCaster.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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\Debug\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. class ExceptionCaster
  20. {
  21. public static $srcContext = 1;
  22. public static $traceArgs = true;
  23. public static $errorTypes = [
  24. E_DEPRECATED => 'E_DEPRECATED',
  25. E_USER_DEPRECATED => 'E_USER_DEPRECATED',
  26. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  27. E_ERROR => 'E_ERROR',
  28. E_WARNING => 'E_WARNING',
  29. E_PARSE => 'E_PARSE',
  30. E_NOTICE => 'E_NOTICE',
  31. E_CORE_ERROR => 'E_CORE_ERROR',
  32. E_CORE_WARNING => 'E_CORE_WARNING',
  33. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  34. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  35. E_USER_ERROR => 'E_USER_ERROR',
  36. E_USER_WARNING => 'E_USER_WARNING',
  37. E_USER_NOTICE => 'E_USER_NOTICE',
  38. E_STRICT => 'E_STRICT',
  39. ];
  40. private static $framesCache = [];
  41. public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
  42. {
  43. return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
  44. }
  45. public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0)
  46. {
  47. return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter);
  48. }
  49. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
  50. {
  51. if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) {
  52. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  53. }
  54. return $a;
  55. }
  56. public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
  57. {
  58. $trace = Caster::PREFIX_VIRTUAL.'trace';
  59. $prefix = Caster::PREFIX_PROTECTED;
  60. $xPrefix = "\0Exception\0";
  61. if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) {
  62. $b = (array) $a[$xPrefix.'previous'];
  63. $class = \get_class($a[$xPrefix.'previous']);
  64. $class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
  65. self::traceUnshift($b[$xPrefix.'trace'], $class, $b[$prefix.'file'], $b[$prefix.'line']);
  66. $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -\count($a[$trace]->value));
  67. }
  68. unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
  69. return $a;
  70. }
  71. public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, $isNested)
  72. {
  73. $sPrefix = "\0".SilencedErrorContext::class."\0";
  74. if (!isset($a[$s = $sPrefix.'severity'])) {
  75. return $a;
  76. }
  77. if (isset(self::$errorTypes[$a[$s]])) {
  78. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  79. }
  80. $trace = [[
  81. 'file' => $a[$sPrefix.'file'],
  82. 'line' => $a[$sPrefix.'line'],
  83. ]];
  84. if (isset($a[$sPrefix.'trace'])) {
  85. $trace = array_merge($trace, $a[$sPrefix.'trace']);
  86. }
  87. unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']);
  88. $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
  89. return $a;
  90. }
  91. public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
  92. {
  93. if (!$isNested) {
  94. return $a;
  95. }
  96. $stub->class = '';
  97. $stub->handle = 0;
  98. $frames = $trace->value;
  99. $prefix = Caster::PREFIX_VIRTUAL;
  100. $a = [];
  101. $j = \count($frames);
  102. if (0 > $i = $trace->sliceOffset) {
  103. $i = max(0, $j + $i);
  104. }
  105. if (!isset($trace->value[$i])) {
  106. return [];
  107. }
  108. $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
  109. $frames[] = ['function' => ''];
  110. $collapse = false;
  111. for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
  112. $f = $frames[$i];
  113. $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???';
  114. $frame = new FrameStub(
  115. [
  116. 'object' => isset($f['object']) ? $f['object'] : null,
  117. 'class' => isset($f['class']) ? $f['class'] : null,
  118. 'type' => isset($f['type']) ? $f['type'] : null,
  119. 'function' => isset($f['function']) ? $f['function'] : null,
  120. ] + $frames[$i - 1],
  121. false,
  122. true
  123. );
  124. $f = self::castFrameStub($frame, [], $frame, true);
  125. if (isset($f[$prefix.'src'])) {
  126. foreach ($f[$prefix.'src']->value as $label => $frame) {
  127. if (0 === strpos($label, "\0~collapse=0")) {
  128. if ($collapse) {
  129. $label = substr_replace($label, '1', 11, 1);
  130. } else {
  131. $collapse = true;
  132. }
  133. }
  134. $label = substr_replace($label, "title=Stack level $j.&", 2, 0);
  135. }
  136. $f = $frames[$i - 1];
  137. if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) {
  138. $frame->value['arguments'] = new ArgsStub($f['args'], isset($f['function']) ? $f['function'] : null, isset($f['class']) ? $f['class'] : null);
  139. }
  140. } elseif ('???' !== $lastCall) {
  141. $label = new ClassStub($lastCall);
  142. if (isset($label->attr['ellipsis'])) {
  143. $label->attr['ellipsis'] += 2;
  144. $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level $j.", 2, 0).$label->value.'()';
  145. } else {
  146. $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()';
  147. }
  148. } else {
  149. $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall;
  150. }
  151. $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame;
  152. $lastCall = $call;
  153. }
  154. if (null !== $trace->sliceLength) {
  155. $a = \array_slice($a, 0, $trace->sliceLength, true);
  156. }
  157. return $a;
  158. }
  159. public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested)
  160. {
  161. if (!$isNested) {
  162. return $a;
  163. }
  164. $f = $frame->value;
  165. $prefix = Caster::PREFIX_VIRTUAL;
  166. if (isset($f['file'], $f['line'])) {
  167. $cacheKey = $f;
  168. unset($cacheKey['object'], $cacheKey['args']);
  169. $cacheKey[] = self::$srcContext;
  170. $cacheKey = implode('-', $cacheKey);
  171. if (isset(self::$framesCache[$cacheKey])) {
  172. $a[$prefix.'src'] = self::$framesCache[$cacheKey];
  173. } else {
  174. if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
  175. $f['file'] = substr($f['file'], 0, -\strlen($match[0]));
  176. $f['line'] = (int) $match[1];
  177. }
  178. $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
  179. $src = $f['line'];
  180. $srcKey = $f['file'];
  181. $ellipsis = new LinkStub($srcKey, 0);
  182. $srcAttr = 'collapse='.(int) $ellipsis->inVendor;
  183. $ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0;
  184. $ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0;
  185. if (file_exists($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 = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
  188. $ellipsis = 0;
  189. $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
  190. $templateInfo = $template->getDebugInfo();
  191. if (isset($templateInfo[$f['line']])) {
  192. if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
  193. $templatePath = null;
  194. }
  195. if ($templateSrc) {
  196. $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
  197. $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
  198. }
  199. }
  200. }
  201. if ($srcKey == $f['file']) {
  202. $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
  203. $srcKey .= ':'.$f['line'];
  204. if ($ellipsis) {
  205. $ellipsis += 1 + \strlen($f['line']);
  206. }
  207. }
  208. $srcAttr .= '&separator= ';
  209. } else {
  210. $srcAttr .= '&separator=:';
  211. }
  212. $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : '';
  213. self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(["\0~$srcAttr\0$srcKey" => $src]);
  214. }
  215. }
  216. unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
  217. if ($frame->inTraceStub) {
  218. unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
  219. }
  220. foreach ($a as $k => $v) {
  221. if (!$v) {
  222. unset($a[$k]);
  223. }
  224. }
  225. if ($frame->keepArgs && !empty($f['args'])) {
  226. $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']);
  227. }
  228. return $a;
  229. }
  230. private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
  231. {
  232. if (isset($a[$xPrefix.'trace'])) {
  233. $trace = $a[$xPrefix.'trace'];
  234. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  235. } else {
  236. $trace = [];
  237. }
  238. if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
  239. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  240. self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
  241. }
  242. $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
  243. }
  244. if (empty($a[$xPrefix.'previous'])) {
  245. unset($a[$xPrefix.'previous']);
  246. }
  247. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  248. if (isset($a[Caster::PREFIX_PROTECTED.'message']) && false !== strpos($a[Caster::PREFIX_PROTECTED.'message'], "class@anonymous\0")) {
  249. $a[Caster::PREFIX_PROTECTED.'message'] = preg_replace_callback('/class@anonymous\x00.*?\.php0x?[0-9a-fA-F]++/', function ($m) {
  250. return \class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
  251. }, $a[Caster::PREFIX_PROTECTED.'message']);
  252. }
  253. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  254. $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
  255. }
  256. return $a;
  257. }
  258. private static function traceUnshift(&$trace, $class, $file, $line)
  259. {
  260. if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
  261. return;
  262. }
  263. array_unshift($trace, [
  264. 'function' => $class ? 'new '.$class : null,
  265. 'file' => $file,
  266. 'line' => $line,
  267. ]);
  268. }
  269. private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null)
  270. {
  271. $srcLines = explode("\n", $srcLines);
  272. $src = [];
  273. for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
  274. $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
  275. }
  276. $srcLines = [];
  277. $ltrim = 0;
  278. do {
  279. $pad = null;
  280. for ($i = $srcContext << 1; $i >= 0; --$i) {
  281. if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
  282. if (null === $pad) {
  283. $pad = $c;
  284. }
  285. if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
  286. break;
  287. }
  288. }
  289. }
  290. ++$ltrim;
  291. } while (0 > $i && null !== $pad);
  292. --$ltrim;
  293. foreach ($src as $i => $c) {
  294. if ($ltrim) {
  295. $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
  296. }
  297. $c = substr($c, 0, -1);
  298. if ($i !== $srcContext) {
  299. $c = new ConstStub('default', $c);
  300. } else {
  301. $c = new ConstStub($c, $title);
  302. if (null !== $file) {
  303. $c->attr['file'] = $file;
  304. $c->attr['line'] = $line;
  305. }
  306. }
  307. $c->attr['lang'] = $lang;
  308. $srcLines[sprintf("\0~separator=› &%d\0", $i + $line - $srcContext)] = $c;
  309. }
  310. return new EnumStub($srcLines);
  311. }
  312. }