FatalError.php 2.8 KB

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