Sin descripción

ExceptionCaster.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 castException(\Exception $e, array $a, Stub $stub, $isNested)
  39. {
  40. $trace = $a["\0Exception\0trace"];
  41. unset($a["\0Exception\0trace"]); // Ensures the trace is always last
  42. static::filterTrace($trace, static::$traceArgs);
  43. if (null !== $trace) {
  44. $a["\0Exception\0trace"] = $trace;
  45. }
  46. if (empty($a["\0Exception\0previous"])) {
  47. unset($a["\0Exception\0previous"]);
  48. }
  49. unset($a["\0Exception\0string"], $a["\0+\0xdebug_message"], $a["\0+\0__destructorException"]);
  50. return $a;
  51. }
  52. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
  53. {
  54. if (isset($a[$s = "\0*\0severity"], self::$errorTypes[$a[$s]])) {
  55. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  56. }
  57. return $a;
  58. }
  59. public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
  60. {
  61. $b = (array) $a["\0Exception\0previous"];
  62. array_splice($b["\0Exception\0trace"], count($a["\0Exception\0trace"]));
  63. $t = static::$traceArgs;
  64. static::$traceArgs = false;
  65. $b = static::castException($a["\0Exception\0previous"], $b, $stub, $isNested);
  66. static::$traceArgs = $t;
  67. if (empty($a["\0*\0message"])) {
  68. $a["\0*\0message"] = "Unexpected exception thrown from a caster: ".get_class($a["\0Exception\0previous"]);
  69. }
  70. if (isset($b["\0*\0message"])) {
  71. $a["\0~\0message"] = $b["\0*\0message"];
  72. }
  73. if (isset($b["\0*\0file"])) {
  74. $a["\0~\0file"] = $b["\0*\0file"];
  75. }
  76. if (isset($b["\0*\0line"])) {
  77. $a["\0~\0line"] = $b["\0*\0line"];
  78. }
  79. if (isset($b["\0Exception\0trace"])) {
  80. $a["\0~\0trace"] = $b["\0Exception\0trace"];
  81. }
  82. unset($a["\0Exception\0trace"], $a["\0Exception\0previous"], $a["\0*\0code"], $a["\0*\0file"], $a["\0*\0line"]);
  83. return $a;
  84. }
  85. public static function filterTrace(&$trace, $dumpArgs, $offset = 0)
  86. {
  87. if (0 > $offset || empty($trace[$offset])) {
  88. return $trace = null;
  89. }
  90. $t = $trace[$offset];
  91. if (empty($t['class']) && isset($t['function'])) {
  92. if ('user_error' === $t['function'] || 'trigger_error' === $t['function']) {
  93. ++$offset;
  94. }
  95. }
  96. if ($offset) {
  97. array_splice($trace, 0, $offset);
  98. }
  99. foreach ($trace as &$t) {
  100. $t = array(
  101. 'call' => (isset($t['class']) ? $t['class'].$t['type'] : '').$t['function'].'()',
  102. 'file' => isset($t['line']) ? "{$t['file']}:{$t['line']}" : '',
  103. 'args' => &$t['args'],
  104. );
  105. if (!isset($t['args']) || !$dumpArgs) {
  106. unset($t['args']);
  107. }
  108. }
  109. }
  110. }