Nenhuma descrição

FlattenException.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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\ErrorHandler\Exception;
  11. use Symfony\Component\Debug\Exception\FatalThrowableError;
  12. use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
  13. use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  16. /**
  17. * FlattenException wraps a PHP Error or Exception to be able to serialize it.
  18. *
  19. * Basically, this class removes all objects from the trace.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class FlattenException extends LegacyFlattenException
  24. {
  25. /** @var string */
  26. private $message;
  27. /** @var int|string */
  28. private $code;
  29. /** @var self|null */
  30. private $previous;
  31. /** @var array */
  32. private $trace;
  33. /** @var string */
  34. private $traceAsString;
  35. /** @var string */
  36. private $class;
  37. /** @var int */
  38. private $statusCode;
  39. /** @var string */
  40. private $statusText;
  41. /** @var array */
  42. private $headers;
  43. /** @var string */
  44. private $file;
  45. /** @var int */
  46. private $line;
  47. /** @var string|null */
  48. private $asString;
  49. /**
  50. * @return static
  51. */
  52. public static function create(\Exception $exception, $statusCode = null, array $headers = []): self
  53. {
  54. return static::createFromThrowable($exception, $statusCode, $headers);
  55. }
  56. /**
  57. * @return static
  58. */
  59. public static function createFromThrowable(\Throwable $exception, int $statusCode = null, array $headers = []): self
  60. {
  61. $e = new static();
  62. $e->setMessage($exception->getMessage());
  63. $e->setCode($exception->getCode());
  64. if ($exception instanceof HttpExceptionInterface) {
  65. $statusCode = $exception->getStatusCode();
  66. $headers = array_merge($headers, $exception->getHeaders());
  67. } elseif ($exception instanceof RequestExceptionInterface) {
  68. $statusCode = 400;
  69. }
  70. if (null === $statusCode) {
  71. $statusCode = 500;
  72. }
  73. if (class_exists(Response::class) && isset(Response::$statusTexts[$statusCode])) {
  74. $statusText = Response::$statusTexts[$statusCode];
  75. } else {
  76. $statusText = 'Whoops, looks like something went wrong.';
  77. }
  78. $e->setStatusText($statusText);
  79. $e->setStatusCode($statusCode);
  80. $e->setHeaders($headers);
  81. $e->setTraceFromThrowable($exception);
  82. $e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
  83. $e->setFile($exception->getFile());
  84. $e->setLine($exception->getLine());
  85. $previous = $exception->getPrevious();
  86. if ($previous instanceof \Throwable) {
  87. $e->setPrevious(static::createFromThrowable($previous));
  88. }
  89. return $e;
  90. }
  91. public function toArray(): array
  92. {
  93. $exceptions = [];
  94. foreach (array_merge([$this], $this->getAllPrevious()) as $exception) {
  95. $exceptions[] = [
  96. 'message' => $exception->getMessage(),
  97. 'class' => $exception->getClass(),
  98. 'trace' => $exception->getTrace(),
  99. ];
  100. }
  101. return $exceptions;
  102. }
  103. public function getStatusCode(): int
  104. {
  105. return $this->statusCode;
  106. }
  107. /**
  108. * @param int $code
  109. *
  110. * @return $this
  111. */
  112. public function setStatusCode($code): self
  113. {
  114. $this->statusCode = $code;
  115. return $this;
  116. }
  117. public function getHeaders(): array
  118. {
  119. return $this->headers;
  120. }
  121. /**
  122. * @return $this
  123. */
  124. public function setHeaders(array $headers): self
  125. {
  126. $this->headers = $headers;
  127. return $this;
  128. }
  129. public function getClass(): string
  130. {
  131. return $this->class;
  132. }
  133. /**
  134. * @param string $class
  135. *
  136. * @return $this
  137. */
  138. public function setClass($class): self
  139. {
  140. $this->class = false !== strpos($class, "@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : $class;
  141. return $this;
  142. }
  143. public function getFile(): string
  144. {
  145. return $this->file;
  146. }
  147. /**
  148. * @param string $file
  149. *
  150. * @return $this
  151. */
  152. public function setFile($file): self
  153. {
  154. $this->file = $file;
  155. return $this;
  156. }
  157. public function getLine(): int
  158. {
  159. return $this->line;
  160. }
  161. /**
  162. * @param int $line
  163. *
  164. * @return $this
  165. */
  166. public function setLine($line): self
  167. {
  168. $this->line = $line;
  169. return $this;
  170. }
  171. public function getStatusText(): string
  172. {
  173. return $this->statusText;
  174. }
  175. public function setStatusText(string $statusText): self
  176. {
  177. $this->statusText = $statusText;
  178. return $this;
  179. }
  180. public function getMessage(): string
  181. {
  182. return $this->message;
  183. }
  184. /**
  185. * @param string $message
  186. *
  187. * @return $this
  188. */
  189. public function setMessage($message): self
  190. {
  191. if (false !== strpos($message, "@anonymous\0")) {
  192. $message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
  193. return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
  194. }, $message);
  195. }
  196. $this->message = $message;
  197. return $this;
  198. }
  199. /**
  200. * @return int|string int most of the time (might be a string with PDOException)
  201. */
  202. public function getCode()
  203. {
  204. return $this->code;
  205. }
  206. /**
  207. * @param int|string $code
  208. *
  209. * @return $this
  210. */
  211. public function setCode($code): self
  212. {
  213. $this->code = $code;
  214. return $this;
  215. }
  216. /**
  217. * @return self|null
  218. */
  219. public function getPrevious()
  220. {
  221. return $this->previous;
  222. }
  223. /**
  224. * @return $this
  225. */
  226. final public function setPrevious(?LegacyFlattenException $previous): self
  227. {
  228. $this->previous = $previous;
  229. return $this;
  230. }
  231. /**
  232. * @return self[]
  233. */
  234. public function getAllPrevious(): array
  235. {
  236. $exceptions = [];
  237. $e = $this;
  238. while ($e = $e->getPrevious()) {
  239. $exceptions[] = $e;
  240. }
  241. return $exceptions;
  242. }
  243. public function getTrace(): array
  244. {
  245. return $this->trace;
  246. }
  247. /**
  248. * @deprecated since 4.1, use {@see setTraceFromThrowable()} instead.
  249. */
  250. public function setTraceFromException(\Exception $exception)
  251. {
  252. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1, use "setTraceFromThrowable()" instead.', __METHOD__), \E_USER_DEPRECATED);
  253. $this->setTraceFromThrowable($exception);
  254. }
  255. /**
  256. * @return $this
  257. */
  258. public function setTraceFromThrowable(\Throwable $throwable): self
  259. {
  260. $this->traceAsString = $throwable->getTraceAsString();
  261. return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
  262. }
  263. /**
  264. * @param array $trace
  265. * @param string|null $file
  266. * @param int|null $line
  267. *
  268. * @return $this
  269. */
  270. public function setTrace($trace, $file, $line): self
  271. {
  272. $this->trace = [];
  273. $this->trace[] = [
  274. 'namespace' => '',
  275. 'short_class' => '',
  276. 'class' => '',
  277. 'type' => '',
  278. 'function' => '',
  279. 'file' => $file,
  280. 'line' => $line,
  281. 'args' => [],
  282. ];
  283. foreach ($trace as $entry) {
  284. $class = '';
  285. $namespace = '';
  286. if (isset($entry['class'])) {
  287. $parts = explode('\\', $entry['class']);
  288. $class = array_pop($parts);
  289. $namespace = implode('\\', $parts);
  290. }
  291. $this->trace[] = [
  292. 'namespace' => $namespace,
  293. 'short_class' => $class,
  294. 'class' => $entry['class'] ?? '',
  295. 'type' => $entry['type'] ?? '',
  296. 'function' => $entry['function'] ?? null,
  297. 'file' => $entry['file'] ?? null,
  298. 'line' => $entry['line'] ?? null,
  299. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : [],
  300. ];
  301. }
  302. return $this;
  303. }
  304. private function flattenArgs(array $args, int $level = 0, int &$count = 0): array
  305. {
  306. $result = [];
  307. foreach ($args as $key => $value) {
  308. if (++$count > 1e4) {
  309. return ['array', '*SKIPPED over 10000 entries*'];
  310. }
  311. if ($value instanceof \__PHP_Incomplete_Class) {
  312. // is_object() returns false on PHP<=7.1
  313. $result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
  314. } elseif (\is_object($value)) {
  315. $result[$key] = ['object', \get_class($value)];
  316. } elseif (\is_array($value)) {
  317. if ($level > 10) {
  318. $result[$key] = ['array', '*DEEP NESTED ARRAY*'];
  319. } else {
  320. $result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
  321. }
  322. } elseif (null === $value) {
  323. $result[$key] = ['null', null];
  324. } elseif (\is_bool($value)) {
  325. $result[$key] = ['boolean', $value];
  326. } elseif (\is_int($value)) {
  327. $result[$key] = ['integer', $value];
  328. } elseif (\is_float($value)) {
  329. $result[$key] = ['float', $value];
  330. } elseif (\is_resource($value)) {
  331. $result[$key] = ['resource', get_resource_type($value)];
  332. } else {
  333. $result[$key] = ['string', (string) $value];
  334. }
  335. }
  336. return $result;
  337. }
  338. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value): string
  339. {
  340. $array = new \ArrayObject($value);
  341. return $array['__PHP_Incomplete_Class_Name'];
  342. }
  343. public function getTraceAsString(): string
  344. {
  345. return $this->traceAsString;
  346. }
  347. /**
  348. * @return $this
  349. */
  350. public function setAsString(?string $asString): self
  351. {
  352. $this->asString = $asString;
  353. return $this;
  354. }
  355. public function getAsString(): string
  356. {
  357. if (null !== $this->asString) {
  358. return $this->asString;
  359. }
  360. $message = '';
  361. $next = false;
  362. foreach (array_reverse(array_merge([$this], $this->getAllPrevious())) as $exception) {
  363. if ($next) {
  364. $message .= 'Next ';
  365. } else {
  366. $next = true;
  367. }
  368. $message .= $exception->getClass();
  369. if ('' != $exception->getMessage()) {
  370. $message .= ': '.$exception->getMessage();
  371. }
  372. $message .= ' in '.$exception->getFile().':'.$exception->getLine().
  373. "\nStack trace:\n".$exception->getTraceAsString()."\n\n";
  374. }
  375. return rtrim($message);
  376. }
  377. }