FatalError.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Error;
  11. class FatalError extends \Error
  12. {
  13. /**
  14. * @param array $error An array as returned by error_get_last()
  15. */
  16. public function __construct(
  17. string $message,
  18. int $code,
  19. private array $error,
  20. ?int $traceOffset = null,
  21. bool $traceArgs = true,
  22. ?array $trace = null,
  23. ) {
  24. parent::__construct($message, $code);
  25. if (null !== $trace) {
  26. if (!$traceArgs) {
  27. foreach ($trace as &$frame) {
  28. unset($frame['args'], $frame['this'], $frame);
  29. }
  30. }
  31. } elseif (null !== $traceOffset) {
  32. if (\function_exists('xdebug_get_function_stack') && \in_array(\ini_get('xdebug.mode'), ['develop', false], true) && $trace = @xdebug_get_function_stack()) {
  33. if (0 < $traceOffset) {
  34. array_splice($trace, -$traceOffset);
  35. }
  36. foreach ($trace as &$frame) {
  37. if (!isset($frame['type'])) {
  38. // XDebug pre 2.1.1 doesn't currently set the call type key http://bugs.xdebug.org/view.php?id=695
  39. if (isset($frame['class'])) {
  40. $frame['type'] = '::';
  41. }
  42. } elseif ('dynamic' === $frame['type']) {
  43. $frame['type'] = '->';
  44. } elseif ('static' === $frame['type']) {
  45. $frame['type'] = '::';
  46. }
  47. // XDebug also has a different name for the parameters array
  48. if (!$traceArgs) {
  49. unset($frame['params'], $frame['args']);
  50. } elseif (isset($frame['params']) && !isset($frame['args'])) {
  51. $frame['args'] = $frame['params'];
  52. unset($frame['params']);
  53. }
  54. }
  55. unset($frame);
  56. $trace = array_reverse($trace);
  57. } else {
  58. $trace = [];
  59. }
  60. }
  61. foreach ([
  62. 'file' => $error['file'],
  63. 'line' => $error['line'],
  64. 'trace' => $trace,
  65. ] as $property => $value) {
  66. if (null !== $value) {
  67. $refl = new \ReflectionProperty(\Error::class, $property);
  68. $refl->setValue($this, $value);
  69. }
  70. }
  71. }
  72. public function getError(): array
  73. {
  74. return $this->error;
  75. }
  76. }