ErrorHandlerConfigurator.php 3.4 KB

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