ErrorHandlerConfigurator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\HttpKernel\Debug;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\ErrorHandler\ErrorHandler;
  13. /**
  14. * Configures the error handler.
  15. *
  16. * @final
  17. *
  18. * @internal
  19. */
  20. class ErrorHandlerConfigurator
  21. {
  22. private ?LoggerInterface $logger;
  23. private ?LoggerInterface $deprecationLogger;
  24. private array|int|null $levels;
  25. private ?int $throwAt;
  26. private bool $scream;
  27. private bool $scope;
  28. /**
  29. * @param array|int|null $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  30. * @param int|null $throwAt Thrown errors in a bit field of E_* constants, or null to keep the current value
  31. * @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
  32. * @param bool $scope Enables/disables scoping mode
  33. */
  34. public function __construct(?LoggerInterface $logger = null, array|int|null $levels = \E_ALL, ?int $throwAt = \E_ALL, bool $scream = true, bool $scope = true, ?LoggerInterface $deprecationLogger = null)
  35. {
  36. $this->logger = $logger;
  37. $this->levels = $levels ?? \E_ALL;
  38. $this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt ? null : ($throwAt ? \E_ALL : null));
  39. $this->scream = $scream;
  40. $this->scope = $scope;
  41. $this->deprecationLogger = $deprecationLogger;
  42. }
  43. /**
  44. * Configures the error handler.
  45. */
  46. public function configure(ErrorHandler $handler): void
  47. {
  48. if ($this->logger || $this->deprecationLogger) {
  49. $this->setDefaultLoggers($handler);
  50. if (\is_array($this->levels)) {
  51. $levels = 0;
  52. foreach ($this->levels as $type => $log) {
  53. $levels |= $type;
  54. }
  55. } else {
  56. $levels = $this->levels;
  57. }
  58. if ($this->scream) {
  59. $handler->screamAt($levels);
  60. }
  61. if ($this->scope) {
  62. $handler->scopeAt($levels & ~\E_USER_DEPRECATED & ~\E_DEPRECATED);
  63. } else {
  64. $handler->scopeAt(0, true);
  65. }
  66. $this->logger = $this->deprecationLogger = $this->levels = null;
  67. }
  68. if (null !== $this->throwAt) {
  69. $handler->throwAt($this->throwAt, true);
  70. }
  71. }
  72. private function setDefaultLoggers(ErrorHandler $handler): void
  73. {
  74. if (\is_array($this->levels)) {
  75. $levelsDeprecatedOnly = [];
  76. $levelsWithoutDeprecated = [];
  77. foreach ($this->levels as $type => $log) {
  78. if (\E_DEPRECATED == $type || \E_USER_DEPRECATED == $type) {
  79. $levelsDeprecatedOnly[$type] = $log;
  80. } else {
  81. $levelsWithoutDeprecated[$type] = $log;
  82. }
  83. }
  84. } else {
  85. $levelsDeprecatedOnly = $this->levels & (\E_DEPRECATED | \E_USER_DEPRECATED);
  86. $levelsWithoutDeprecated = $this->levels & ~\E_DEPRECATED & ~\E_USER_DEPRECATED;
  87. }
  88. $defaultLoggerLevels = $this->levels;
  89. if ($this->deprecationLogger && $levelsDeprecatedOnly) {
  90. $handler->setDefaultLogger($this->deprecationLogger, $levelsDeprecatedOnly);
  91. $defaultLoggerLevels = $levelsWithoutDeprecated;
  92. }
  93. if ($this->logger && $defaultLoggerLevels) {
  94. $handler->setDefaultLogger($this->logger, $defaultLoggerLevels);
  95. }
  96. }
  97. }