説明なし

FlattenException.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\Debug\Exception;
  11. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  12. /**
  13. * FlattenException wraps a PHP Exception to be able to serialize it.
  14. *
  15. * Basically, this class removes all objects from the trace.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class FlattenException
  20. {
  21. private $message;
  22. private $code;
  23. private $previous;
  24. private $trace;
  25. private $class;
  26. private $statusCode;
  27. private $headers;
  28. private $file;
  29. private $line;
  30. public static function create(\Exception $exception, $statusCode = null, array $headers = array())
  31. {
  32. $e = new static();
  33. $e->setMessage($exception->getMessage());
  34. $e->setCode($exception->getCode());
  35. if ($exception instanceof HttpExceptionInterface) {
  36. $statusCode = $exception->getStatusCode();
  37. $headers = array_merge($headers, $exception->getHeaders());
  38. }
  39. if (null === $statusCode) {
  40. $statusCode = 500;
  41. }
  42. $e->setStatusCode($statusCode);
  43. $e->setHeaders($headers);
  44. $e->setTraceFromException($exception);
  45. $e->setClass(get_class($exception));
  46. $e->setFile($exception->getFile());
  47. $e->setLine($exception->getLine());
  48. $previous = $exception->getPrevious();
  49. if ($previous instanceof \Exception) {
  50. $e->setPrevious(static::create($previous));
  51. } elseif ($previous instanceof \Throwable) {
  52. $e->setPrevious(static::create(new FatalThrowableError($previous)));
  53. }
  54. return $e;
  55. }
  56. public function toArray()
  57. {
  58. $exceptions = array();
  59. foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
  60. $exceptions[] = array(
  61. 'message' => $exception->getMessage(),
  62. 'class' => $exception->getClass(),
  63. 'trace' => $exception->getTrace(),
  64. );
  65. }
  66. return $exceptions;
  67. }
  68. public function getStatusCode()
  69. {
  70. return $this->statusCode;
  71. }
  72. public function setStatusCode($code)
  73. {
  74. $this->statusCode = $code;
  75. }
  76. public function getHeaders()
  77. {
  78. return $this->headers;
  79. }
  80. public function setHeaders(array $headers)
  81. {
  82. $this->headers = $headers;
  83. }
  84. public function getClass()
  85. {
  86. return $this->class;
  87. }
  88. public function setClass($class)
  89. {
  90. $this->class = $class;
  91. }
  92. public function getFile()
  93. {
  94. return $this->file;
  95. }
  96. public function setFile($file)
  97. {
  98. $this->file = $file;
  99. }
  100. public function getLine()
  101. {
  102. return $this->line;
  103. }
  104. public function setLine($line)
  105. {
  106. $this->line = $line;
  107. }
  108. public function getMessage()
  109. {
  110. return $this->message;
  111. }
  112. public function setMessage($message)
  113. {
  114. $this->message = $message;
  115. }
  116. public function getCode()
  117. {
  118. return $this->code;
  119. }
  120. public function setCode($code)
  121. {
  122. $this->code = $code;
  123. }
  124. public function getPrevious()
  125. {
  126. return $this->previous;
  127. }
  128. public function setPrevious(FlattenException $previous)
  129. {
  130. $this->previous = $previous;
  131. }
  132. public function getAllPrevious()
  133. {
  134. $exceptions = array();
  135. $e = $this;
  136. while ($e = $e->getPrevious()) {
  137. $exceptions[] = $e;
  138. }
  139. return $exceptions;
  140. }
  141. public function getTrace()
  142. {
  143. return $this->trace;
  144. }
  145. public function setTraceFromException(\Exception $exception)
  146. {
  147. $this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
  148. }
  149. public function setTrace($trace, $file, $line)
  150. {
  151. $this->trace = array();
  152. $this->trace[] = array(
  153. 'namespace' => '',
  154. 'short_class' => '',
  155. 'class' => '',
  156. 'type' => '',
  157. 'function' => '',
  158. 'file' => $file,
  159. 'line' => $line,
  160. 'args' => array(),
  161. );
  162. foreach ($trace as $entry) {
  163. $class = '';
  164. $namespace = '';
  165. if (isset($entry['class'])) {
  166. $parts = explode('\\', $entry['class']);
  167. $class = array_pop($parts);
  168. $namespace = implode('\\', $parts);
  169. }
  170. $this->trace[] = array(
  171. 'namespace' => $namespace,
  172. 'short_class' => $class,
  173. 'class' => isset($entry['class']) ? $entry['class'] : '',
  174. 'type' => isset($entry['type']) ? $entry['type'] : '',
  175. 'function' => isset($entry['function']) ? $entry['function'] : null,
  176. 'file' => isset($entry['file']) ? $entry['file'] : null,
  177. 'line' => isset($entry['line']) ? $entry['line'] : null,
  178. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : array(),
  179. );
  180. }
  181. }
  182. private function flattenArgs($args, $level = 0, &$count = 0)
  183. {
  184. $result = array();
  185. foreach ($args as $key => $value) {
  186. if (++$count > 1e4) {
  187. return array('array', '*SKIPPED over 10000 entries*');
  188. }
  189. if ($value instanceof \__PHP_Incomplete_Class) {
  190. // is_object() returns false on PHP<=7.1
  191. $result[$key] = array('incomplete-object', $this->getClassNameFromIncomplete($value));
  192. } elseif (is_object($value)) {
  193. $result[$key] = array('object', get_class($value));
  194. } elseif (is_array($value)) {
  195. if ($level > 10) {
  196. $result[$key] = array('array', '*DEEP NESTED ARRAY*');
  197. } else {
  198. $result[$key] = array('array', $this->flattenArgs($value, $level + 1, $count));
  199. }
  200. } elseif (null === $value) {
  201. $result[$key] = array('null', null);
  202. } elseif (is_bool($value)) {
  203. $result[$key] = array('boolean', $value);
  204. } elseif (is_int($value)) {
  205. $result[$key] = array('integer', $value);
  206. } elseif (is_float($value)) {
  207. $result[$key] = array('float', $value);
  208. } elseif (is_resource($value)) {
  209. $result[$key] = array('resource', get_resource_type($value));
  210. } else {
  211. $result[$key] = array('string', (string) $value);
  212. }
  213. }
  214. return $result;
  215. }
  216. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  217. {
  218. $array = new \ArrayObject($value);
  219. return $array['__PHP_Incomplete_Class_Name'];
  220. }
  221. }