菜谱项目

ExceptionCaster.php 13KB

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