PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/error-handler/Exception/FlattenException.php

https://gitlab.com/madwanz64/laravel
PHP | 454 lines | 282 code | 77 blank | 95 comment | 19 complexity | 14f1a2575755fff363ded3868796d7ba MD5 | raw file
  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. }