FlattenException.php 7.6KB

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