ExceptionCaster.php 13 KB

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