FatalError.php 2.7 KB

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