ExceptionCaster.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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'])) {
  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') && method_exists($f['class'], 'getDebugInfo')) {
  131. $template = isset($f['object']) ? $f['object'] : new $f['class'](new \Twig_Environment(new \Twig_Loader_Filesystem()));
  132. try {
  133. $templateName = $template->getTemplateName();
  134. $templateSrc = explode("\n", method_exists($template, 'getSource') ? $template->getSource() : $template->getEnvironment()->getLoader()->getSource($templateName));
  135. $templateInfo = $template->getDebugInfo();
  136. if (isset($templateInfo[$f['line']])) {
  137. $src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
  138. }
  139. } catch (\Twig_Error_Loader $e) {
  140. }
  141. }
  142. } else {
  143. $src[$f['file']] = $f['line'];
  144. }
  145. $a[$prefix.'src'] = new EnumStub($src);
  146. }
  147. unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
  148. if ($frame->inTraceStub) {
  149. unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
  150. }
  151. foreach ($a as $k => $v) {
  152. if (!$v) {
  153. unset($a[$k]);
  154. }
  155. }
  156. if ($frame->keepArgs && isset($f['args'])) {
  157. $a[$prefix.'args'] = $f['args'];
  158. }
  159. return $a;
  160. }
  161. private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
  162. {
  163. if (isset($a[$xPrefix.'trace'])) {
  164. $trace = $a[$xPrefix.'trace'];
  165. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  166. } else {
  167. $trace = array();
  168. }
  169. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  170. array_unshift($trace, array(
  171. 'function' => $xClass ? 'new '.$xClass : null,
  172. 'file' => $a[Caster::PREFIX_PROTECTED.'file'],
  173. 'line' => $a[Caster::PREFIX_PROTECTED.'line'],
  174. ));
  175. $a[$xPrefix.'trace'] = new TraceStub($trace, self::$traceArgs);
  176. }
  177. if (empty($a[$xPrefix.'previous'])) {
  178. unset($a[$xPrefix.'previous']);
  179. }
  180. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  181. return $a;
  182. }
  183. private static function extractSource(array $srcArray, $line, $srcContext)
  184. {
  185. $src = array();
  186. for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
  187. $src[] = (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
  188. }
  189. $ltrim = 0;
  190. do {
  191. $pad = null;
  192. for ($i = $srcContext << 1; $i >= 0; --$i) {
  193. if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
  194. if (null === $pad) {
  195. $pad = $c;
  196. }
  197. if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
  198. break;
  199. }
  200. }
  201. }
  202. ++$ltrim;
  203. } while (0 > $i && null !== $pad);
  204. if (--$ltrim) {
  205. foreach ($src as $i => $line) {
  206. $src[$i] = isset($line[$ltrim]) && "\r" !== $line[$ltrim] ? substr($line, $ltrim) : ltrim($line, " \t");
  207. }
  208. }
  209. return implode('', $src);
  210. }
  211. }