ExceptionCaster.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
  40. {
  41. return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
  42. }
  43. public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0)
  44. {
  45. return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter);
  46. }
  47. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
  48. {
  49. if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) {
  50. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  51. }
  52. return $a;
  53. }
  54. public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
  55. {
  56. $prefix = Caster::PREFIX_PROTECTED;
  57. $xPrefix = "\0Exception\0";
  58. if (isset($a[$xPrefix.'previous'], $a[$xPrefix.'trace']) && $a[$xPrefix.'previous'] instanceof \Exception) {
  59. $b = (array) $a[$xPrefix.'previous'];
  60. array_unshift($b[$xPrefix.'trace'], array(
  61. 'function' => 'new '.get_class($a[$xPrefix.'previous']),
  62. 'file' => $b[$prefix.'file'],
  63. 'line' => $b[$prefix.'line'],
  64. ));
  65. $a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], false, 0, -1 - count($a[$xPrefix.'trace']->value));
  66. }
  67. unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
  68. return $a;
  69. }
  70. public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
  71. {
  72. if (!$isNested) {
  73. return $a;
  74. }
  75. $stub->class = '';
  76. $stub->handle = 0;
  77. $frames = $trace->value;
  78. $a = array();
  79. $j = count($frames);
  80. if (0 > $i = $trace->sliceOffset) {
  81. $i = max(0, $j + $i);
  82. }
  83. if (!isset($trace->value[$i])) {
  84. return array();
  85. }
  86. $lastCall = isset($frames[$i]['function']) ? ' ==> '.(isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
  87. for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
  88. $call = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[$i]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '???';
  89. $a[Caster::PREFIX_VIRTUAL.$j.'. '.$call.$lastCall] = new FrameStub(
  90. array(
  91. 'object' => isset($frames[$i]['object']) ? $frames[$i]['object'] : null,
  92. 'class' => isset($frames[$i]['class']) ? $frames[$i]['class'] : null,
  93. 'type' => isset($frames[$i]['type']) ? $frames[$i]['type'] : null,
  94. 'function' => isset($frames[$i]['function']) ? $frames[$i]['function'] : null,
  95. ) + $frames[$i - 1],
  96. $trace->keepArgs,
  97. true
  98. );
  99. $lastCall = ' ==> '.$call;
  100. }
  101. $a[Caster::PREFIX_VIRTUAL.$j.'. {main}'.$lastCall] = new FrameStub(
  102. array(
  103. 'object' => null,
  104. 'class' => null,
  105. 'type' => null,
  106. 'function' => '{main}',
  107. ) + $frames[$i - 1],
  108. $trace->keepArgs,
  109. true
  110. );
  111. if (null !== $trace->sliceLength) {
  112. $a = array_slice($a, 0, $trace->sliceLength, true);
  113. }
  114. return $a;
  115. }
  116. public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested)
  117. {
  118. if (!$isNested) {
  119. return $a;
  120. }
  121. $f = $frame->value;
  122. $prefix = Caster::PREFIX_VIRTUAL;
  123. if (isset($f['file'], $f['line'])) {
  124. if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
  125. $f['file'] = substr($f['file'], 0, -strlen($match[0]));
  126. $f['line'] = (int) $match[1];
  127. }
  128. if (file_exists($f['file']) && 0 <= self::$srcContext) {
  129. $src[$f['file'].':'.$f['line']] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);
  130. if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
  131. $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
  132. $templateName = $template->getTemplateName();
  133. $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
  134. $templateInfo = $template->getDebugInfo();
  135. if (isset($templateInfo[$f['line']])) {
  136. if (method_exists($template, 'getSourceContext')) {
  137. $templateName = $template->getSourceContext()->getPath() ?: $templateName;
  138. }
  139. if ($templateSrc) {
  140. $templateSrc = explode("\n", $templateSrc);
  141. $src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
  142. } else {
  143. $src[$templateName] = $templateInfo[$f['line']];
  144. }
  145. }
  146. }
  147. } else {
  148. $src[$f['file']] = $f['line'];
  149. }
  150. $a[$prefix.'src'] = new EnumStub($src);
  151. }
  152. unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
  153. if ($frame->inTraceStub) {
  154. unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
  155. }
  156. foreach ($a as $k => $v) {
  157. if (!$v) {
  158. unset($a[$k]);
  159. }
  160. }
  161. if ($frame->keepArgs && isset($f['args'])) {
  162. $a[$prefix.'args'] = $f['args'];
  163. }
  164. return $a;
  165. }
  166. /**
  167. * @deprecated since 2.8, to be removed in 3.0. Use the castTraceStub method instead.
  168. */
  169. public static function filterTrace(&$trace, $dumpArgs, $offset = 0)
  170. {
  171. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0. Use the castTraceStub method instead.', E_USER_DEPRECATED);
  172. if (0 > $offset || empty($trace[$offset])) {
  173. return $trace = null;
  174. }
  175. $t = $trace[$offset];
  176. if (empty($t['class']) && isset($t['function'])) {
  177. if ('user_error' === $t['function'] || 'trigger_error' === $t['function']) {
  178. ++$offset;
  179. }
  180. }
  181. if ($offset) {
  182. array_splice($trace, 0, $offset);
  183. }
  184. foreach ($trace as &$t) {
  185. $t = array(
  186. 'call' => (isset($t['class']) ? $t['class'].$t['type'] : '').$t['function'].'()',
  187. 'file' => isset($t['line']) ? "{$t['file']}:{$t['line']}" : '',
  188. 'args' => &$t['args'],
  189. );
  190. if (!isset($t['args']) || !$dumpArgs) {
  191. unset($t['args']);
  192. }
  193. }
  194. }
  195. private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
  196. {
  197. if (isset($a[$xPrefix.'trace'])) {
  198. $trace = $a[$xPrefix.'trace'];
  199. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  200. } else {
  201. $trace = array();
  202. }
  203. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  204. if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
  205. array_unshift($trace, array(
  206. 'function' => $xClass ? 'new '.$xClass : null,
  207. 'file' => $a[Caster::PREFIX_PROTECTED.'file'],
  208. 'line' => $a[Caster::PREFIX_PROTECTED.'line'],
  209. ));
  210. }
  211. $a[$xPrefix.'trace'] = new TraceStub($trace, self::$traceArgs);
  212. }
  213. if (empty($a[$xPrefix.'previous'])) {
  214. unset($a[$xPrefix.'previous']);
  215. }
  216. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  217. return $a;
  218. }
  219. private static function extractSource(array $srcArray, $line, $srcContext)
  220. {
  221. $src = array();
  222. for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
  223. $src[] = (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
  224. }
  225. $ltrim = 0;
  226. do {
  227. $pad = null;
  228. for ($i = $srcContext << 1; $i >= 0; --$i) {
  229. if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
  230. if (null === $pad) {
  231. $pad = $c;
  232. }
  233. if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
  234. break;
  235. }
  236. }
  237. }
  238. ++$ltrim;
  239. } while (0 > $i && null !== $pad);
  240. if (--$ltrim) {
  241. foreach ($src as $i => $line) {
  242. $src[$i] = isset($line[$ltrim]) && "\r" !== $line[$ltrim] ? substr($line, $ltrim) : ltrim($line, " \t");
  243. }
  244. }
  245. return implode('', $src);
  246. }
  247. }