ExceptionCaster.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 $traceArgs = true;
  21. public static $errorTypes = array(
  22. E_DEPRECATED => 'E_DEPRECATED',
  23. E_USER_DEPRECATED => 'E_USER_DEPRECATED',
  24. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  25. E_ERROR => 'E_ERROR',
  26. E_WARNING => 'E_WARNING',
  27. E_PARSE => 'E_PARSE',
  28. E_NOTICE => 'E_NOTICE',
  29. E_CORE_ERROR => 'E_CORE_ERROR',
  30. E_CORE_WARNING => 'E_CORE_WARNING',
  31. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  32. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  33. E_USER_ERROR => 'E_USER_ERROR',
  34. E_USER_WARNING => 'E_USER_WARNING',
  35. E_USER_NOTICE => 'E_USER_NOTICE',
  36. E_STRICT => 'E_STRICT',
  37. );
  38. public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
  39. {
  40. return self::filterExceptionArray($a, "\0Error\0", $filter);
  41. }
  42. public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0)
  43. {
  44. return self::filterExceptionArray($a, "\0Exception\0", $filter);
  45. }
  46. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
  47. {
  48. if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) {
  49. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  50. }
  51. return $a;
  52. }
  53. public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
  54. {
  55. $prefix = Caster::PREFIX_PROTECTED;
  56. $xPrefix = "\0Exception\0";
  57. if (isset($a[$xPrefix.'previous'], $a[$xPrefix.'trace'][0])) {
  58. $b = (array) $a[$xPrefix.'previous'];
  59. $b[$xPrefix.'trace'][0] += array(
  60. 'file' => $b[$prefix.'file'],
  61. 'line' => $b[$prefix.'line'],
  62. );
  63. array_splice($b[$xPrefix.'trace'], -1 - count($a[$xPrefix.'trace']));
  64. static::filterTrace($b[$xPrefix.'trace'], false);
  65. $a[Caster::PREFIX_VIRTUAL.'trace'] = $b[$xPrefix.'trace'];
  66. }
  67. unset($a[$xPrefix.'trace'], $a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
  68. return $a;
  69. }
  70. public static function filterTrace(&$trace, $dumpArgs, $offset = 0)
  71. {
  72. if (0 > $offset || empty($trace[$offset])) {
  73. return $trace = null;
  74. }
  75. $t = $trace[$offset];
  76. if (empty($t['class']) && isset($t['function'])) {
  77. if ('user_error' === $t['function'] || 'trigger_error' === $t['function']) {
  78. ++$offset;
  79. }
  80. }
  81. if ($offset) {
  82. array_splice($trace, 0, $offset);
  83. }
  84. foreach ($trace as &$t) {
  85. $t = array(
  86. 'call' => (isset($t['class']) ? $t['class'].$t['type'] : '').$t['function'].'()',
  87. 'file' => isset($t['line']) ? "{$t['file']}:{$t['line']}" : '',
  88. 'args' => &$t['args'],
  89. );
  90. if (!isset($t['args']) || !$dumpArgs) {
  91. unset($t['args']);
  92. }
  93. }
  94. }
  95. private static function filterExceptionArray(array $a, $xPrefix, $filter)
  96. {
  97. if (isset($a[$xPrefix.'trace'])) {
  98. $trace = $a[$xPrefix.'trace'];
  99. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  100. } else {
  101. $trace = array();
  102. }
  103. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  104. static::filterTrace($trace, static::$traceArgs);
  105. if (null !== $trace) {
  106. $a[$xPrefix.'trace'] = $trace;
  107. }
  108. }
  109. if (empty($a[$xPrefix.'previous'])) {
  110. unset($a[$xPrefix.'previous']);
  111. }
  112. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  113. return $a;
  114. }
  115. }