FatalError.php 2.8 KB

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