ExceptionCaster.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\VarDumper\Exception\ThrowingCasterException;
  12. use Symfony\Component\VarDumper\Cloner\Stub;
  13. /**
  14. * Casts common Exception classes to array representation.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. class ExceptionCaster
  19. {
  20. public static $srcContext = 1;
  21. public static $traceArgs = true;
  22. public static $errorTypes = array(
  23. E_DEPRECATED => 'E_DEPRECATED',
  24. E_USER_DEPRECATED => 'E_USER_DEPRECATED',
  25. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  26. E_ERROR => 'E_ERROR',
  27. E_WARNING => 'E_WARNING',
  28. E_PARSE => 'E_PARSE',
  29. E_NOTICE => 'E_NOTICE',
  30. E_CORE_ERROR => 'E_CORE_ERROR',
  31. E_CORE_WARNING => 'E_CORE_WARNING',
  32. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  33. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  34. E_USER_ERROR => 'E_USER_ERROR',
  35. E_USER_WARNING => 'E_USER_WARNING',
  36. E_USER_NOTICE => 'E_USER_NOTICE',
  37. E_STRICT => 'E_STRICT',
  38. );
  39. private static $framesCache = array();
  40. public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
  41. {
  42. return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
  43. }
  44. public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0)
  45. {
  46. return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter);
  47. }
  48. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
  49. {
  50. if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) {
  51. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  52. }
  53. return $a;
  54. }
  55. public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
  56. {
  57. $prefix = Caster::PREFIX_PROTECTED;
  58. $xPrefix = "\0Exception\0";
  59. if (isset($a[$xPrefix.'previous'], $a[$xPrefix.'trace']) && $a[$xPrefix.'previous'] instanceof \Exception) {
  60. $b = (array) $a[$xPrefix.'previous'];
  61. self::traceUnshift($b[$xPrefix.'trace'], get_class($a[$xPrefix.'previous']), $b[$prefix.'file'], $b[$prefix.'line']);
  62. $a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], false, 0, -1 - count($a[$xPrefix.'trace']->value));
  63. }
  64. unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
  65. return $a;
  66. }
  67. public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
  68. {
  69. if (!$isNested) {
  70. return $a;
  71. }
  72. $stub->class = '';
  73. $stub->handle = 0;
  74. $frames = $trace->value;
  75. $prefix = Caster::PREFIX_VIRTUAL;
  76. $a = array();
  77. $j = count($frames);
  78. if (0 > $i = $trace->sliceOffset) {
  79. $i = max(0, $j + $i);
  80. }
  81. if (!isset($trace->value[$i])) {
  82. return array();
  83. }
  84. $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
  85. $frames[] = array('function' => '');
  86. for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
  87. $f = $frames[$i];
  88. $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'].'()' : '???';
  89. $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall;
  90. $frame = new FrameStub(
  91. array(
  92. 'object' => isset($f['object']) ? $f['object'] : null,
  93. 'class' => isset($f['class']) ? $f['class'] : null,
  94. 'type' => isset($f['type']) ? $f['type'] : null,
  95. 'function' => isset($f['function']) ? $f['function'] : null,
  96. ) + $frames[$i - 1],
  97. false,
  98. true
  99. );
  100. $f = self::castFrameStub($frame, array(), $frame, true);
  101. if (isset($f[$prefix.'src'])) {
  102. foreach ($f[$prefix.'src']->value as $label => $frame) {
  103. $label = substr_replace($label, "title=Stack level $j.&", 2, 0);
  104. }
  105. $f = $frames[$i - 1];
  106. if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) {
  107. $frame->value['arguments'] = new ArgsStub($f['args'], isset($f['function']) ? $f['function'] : null, isset($f['class']) ? $f['class'] : null);
  108. }
  109. }
  110. $a[$label] = $frame;
  111. $lastCall = $call;
  112. }
  113. if (null !== $trace->sliceLength) {
  114. $a = array_slice($a, 0, $trace->sliceLength, true);
  115. }
  116. return $a;
  117. }
  118. public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested)
  119. {
  120. if (!$isNested) {
  121. return $a;
  122. }
  123. $f = $frame->value;
  124. $prefix = Caster::PREFIX_VIRTUAL;
  125. if (isset($f['file'], $f['line'])) {
  126. $cacheKey = $f;
  127. unset($cacheKey['object'], $cacheKey['args']);
  128. $cacheKey[] = self::$srcContext;
  129. $cacheKey = implode('-', $cacheKey);
  130. if (isset(self::$framesCache[$cacheKey])) {
  131. $a[$prefix.'src'] = self::$framesCache[$cacheKey];
  132. } else {
  133. if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
  134. $f['file'] = substr($f['file'], 0, -strlen($match[0]));
  135. $f['line'] = (int) $match[1];
  136. }
  137. $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
  138. $src = $f['line'];
  139. $srcKey = $f['file'];
  140. $ellipsis = explode(DIRECTORY_SEPARATOR, $srcKey);
  141. $ellipsis = 3 < count($ellipsis) ? 2 + strlen(implode(array_slice($ellipsis, -2))) : 0;
  142. if (file_exists($f['file']) && 0 <= self::$srcContext) {
  143. if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
  144. $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
  145. $ellipsis = 0;
  146. $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
  147. $templateInfo = $template->getDebugInfo();
  148. if (isset($templateInfo[$f['line']])) {
  149. if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
  150. $templatePath = null;
  151. }
  152. if ($templateSrc) {
  153. $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
  154. $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
  155. }
  156. }
  157. }
  158. if ($srcKey == $f['file']) {
  159. $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
  160. $srcKey .= ':'.$f['line'];
  161. if ($ellipsis) {
  162. $ellipsis += 1 + strlen($f['line']);
  163. }
  164. }
  165. }
  166. $srcAttr = $ellipsis ? 'ellipsis='.$ellipsis : '';
  167. self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(array("\0~$srcAttr\0$srcKey" => $src));
  168. }
  169. }
  170. unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
  171. if ($frame->inTraceStub) {
  172. unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
  173. }
  174. foreach ($a as $k => $v) {
  175. if (!$v) {
  176. unset($a[$k]);
  177. }
  178. }
  179. if ($frame->keepArgs && !empty($f['args'])) {
  180. $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']);
  181. }
  182. return $a;
  183. }
  184. private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
  185. {
  186. if (isset($a[$xPrefix.'trace'])) {
  187. $trace = $a[$xPrefix.'trace'];
  188. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  189. } else {
  190. $trace = array();
  191. }
  192. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  193. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  194. self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
  195. }
  196. $a[$xPrefix.'trace'] = new TraceStub($trace, self::$traceArgs);
  197. }
  198. if (empty($a[$xPrefix.'previous'])) {
  199. unset($a[$xPrefix.'previous']);
  200. }
  201. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  202. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  203. $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
  204. }
  205. return $a;
  206. }
  207. private static function traceUnshift(&$trace, $class, $file, $line)
  208. {
  209. if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
  210. return;
  211. }
  212. array_unshift($trace, array(
  213. 'function' => $class ? 'new '.$class : null,
  214. 'file' => $file,
  215. 'line' => $line,
  216. ));
  217. }
  218. private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null)
  219. {
  220. $srcLines = explode("\n", $srcLines);
  221. $src = array();
  222. for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
  223. $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
  224. }
  225. $srcLines = array();
  226. $ltrim = 0;
  227. do {
  228. $pad = null;
  229. for ($i = $srcContext << 1; $i >= 0; --$i) {
  230. if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
  231. if (null === $pad) {
  232. $pad = $c;
  233. }
  234. if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
  235. break;
  236. }
  237. }
  238. }
  239. ++$ltrim;
  240. } while (0 > $i && null !== $pad);
  241. --$ltrim;
  242. foreach ($src as $i => $c) {
  243. if ($ltrim) {
  244. $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
  245. }
  246. $c = substr($c, 0, -1);
  247. if ($i !== $srcContext) {
  248. $c = new ConstStub('default', $c);
  249. } else {
  250. $c = new ConstStub($c, $title);
  251. if (null !== $file) {
  252. $c->attr['file'] = $file;
  253. $c->attr['line'] = $line;
  254. }
  255. }
  256. $c->attr['lang'] = $lang;
  257. $srcLines[sprintf("\0~%d\0", $i + $line - $srcContext)] = $c;
  258. }
  259. return new EnumStub($srcLines);
  260. }
  261. }