You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
11 KiB

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