FlattenException.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 = [])
  32. {
  33. return static::createFromThrowable($exception, $statusCode, $headers);
  34. }
  35. public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = []): 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 = [];
  64. foreach (array_merge([$this], $this->getAllPrevious()) as $exception) {
  65. $exceptions[] = [
  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. /**
  78. * @return $this
  79. */
  80. public function setStatusCode($code)
  81. {
  82. $this->statusCode = $code;
  83. return $this;
  84. }
  85. public function getHeaders()
  86. {
  87. return $this->headers;
  88. }
  89. /**
  90. * @return $this
  91. */
  92. public function setHeaders(array $headers)
  93. {
  94. $this->headers = $headers;
  95. return $this;
  96. }
  97. public function getClass()
  98. {
  99. return $this->class;
  100. }
  101. /**
  102. * @return $this
  103. */
  104. public function setClass($class)
  105. {
  106. $this->class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
  107. return $this;
  108. }
  109. public function getFile()
  110. {
  111. return $this->file;
  112. }
  113. /**
  114. * @return $this
  115. */
  116. public function setFile($file)
  117. {
  118. $this->file = $file;
  119. return $this;
  120. }
  121. public function getLine()
  122. {
  123. return $this->line;
  124. }
  125. /**
  126. * @return $this
  127. */
  128. public function setLine($line)
  129. {
  130. $this->line = $line;
  131. return $this;
  132. }
  133. public function getMessage()
  134. {
  135. return $this->message;
  136. }
  137. /**
  138. * @return $this
  139. */
  140. public function setMessage($message)
  141. {
  142. if (false !== strpos($message, "class@anonymous\0")) {
  143. $message = preg_replace_callback('/class@anonymous\x00.*?\.php0x?[0-9a-fA-F]++/', function ($m) {
  144. return \class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
  145. }, $message);
  146. }
  147. $this->message = $message;
  148. return $this;
  149. }
  150. public function getCode()
  151. {
  152. return $this->code;
  153. }
  154. /**
  155. * @return $this
  156. */
  157. public function setCode($code)
  158. {
  159. $this->code = $code;
  160. return $this;
  161. }
  162. public function getPrevious()
  163. {
  164. return $this->previous;
  165. }
  166. /**
  167. * @return $this
  168. */
  169. public function setPrevious(self $previous)
  170. {
  171. $this->previous = $previous;
  172. return $this;
  173. }
  174. public function getAllPrevious()
  175. {
  176. $exceptions = [];
  177. $e = $this;
  178. while ($e = $e->getPrevious()) {
  179. $exceptions[] = $e;
  180. }
  181. return $exceptions;
  182. }
  183. public function getTrace()
  184. {
  185. return $this->trace;
  186. }
  187. /**
  188. * @deprecated since 4.1, use {@see setTraceFromThrowable()} instead.
  189. */
  190. public function setTraceFromException(\Exception $exception)
  191. {
  192. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1, use "setTraceFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);
  193. $this->setTraceFromThrowable($exception);
  194. }
  195. public function setTraceFromThrowable(\Throwable $throwable)
  196. {
  197. return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
  198. }
  199. /**
  200. * @return $this
  201. */
  202. public function setTrace($trace, $file, $line)
  203. {
  204. $this->trace = [];
  205. $this->trace[] = [
  206. 'namespace' => '',
  207. 'short_class' => '',
  208. 'class' => '',
  209. 'type' => '',
  210. 'function' => '',
  211. 'file' => $file,
  212. 'line' => $line,
  213. 'args' => [],
  214. ];
  215. foreach ($trace as $entry) {
  216. $class = '';
  217. $namespace = '';
  218. if (isset($entry['class'])) {
  219. $parts = explode('\\', $entry['class']);
  220. $class = array_pop($parts);
  221. $namespace = implode('\\', $parts);
  222. }
  223. $this->trace[] = [
  224. 'namespace' => $namespace,
  225. 'short_class' => $class,
  226. 'class' => isset($entry['class']) ? $entry['class'] : '',
  227. 'type' => isset($entry['type']) ? $entry['type'] : '',
  228. 'function' => isset($entry['function']) ? $entry['function'] : null,
  229. 'file' => isset($entry['file']) ? $entry['file'] : null,
  230. 'line' => isset($entry['line']) ? $entry['line'] : null,
  231. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : [],
  232. ];
  233. }
  234. return $this;
  235. }
  236. private function flattenArgs($args, $level = 0, &$count = 0)
  237. {
  238. $result = [];
  239. foreach ($args as $key => $value) {
  240. if (++$count > 1e4) {
  241. return ['array', '*SKIPPED over 10000 entries*'];
  242. }
  243. if ($value instanceof \__PHP_Incomplete_Class) {
  244. // is_object() returns false on PHP<=7.1
  245. $result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
  246. } elseif (\is_object($value)) {
  247. $result[$key] = ['object', \get_class($value)];
  248. } elseif (\is_array($value)) {
  249. if ($level > 10) {
  250. $result[$key] = ['array', '*DEEP NESTED ARRAY*'];
  251. } else {
  252. $result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
  253. }
  254. } elseif (null === $value) {
  255. $result[$key] = ['null', null];
  256. } elseif (\is_bool($value)) {
  257. $result[$key] = ['boolean', $value];
  258. } elseif (\is_int($value)) {
  259. $result[$key] = ['integer', $value];
  260. } elseif (\is_float($value)) {
  261. $result[$key] = ['float', $value];
  262. } elseif (\is_resource($value)) {
  263. $result[$key] = ['resource', get_resource_type($value)];
  264. } else {
  265. $result[$key] = ['string', (string) $value];
  266. }
  267. }
  268. return $result;
  269. }
  270. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  271. {
  272. $array = new \ArrayObject($value);
  273. return $array['__PHP_Incomplete_Class_Name'];
  274. }
  275. }