ExceptionCaster.php 14 KB

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