GroupHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Handler;
  11. use Monolog\Formatter\FormatterInterface;
  12. use Monolog\ResettableInterface;
  13. /**
  14. * Forwards records to multiple handlers
  15. *
  16. * @author Lenar Lõhmus <lenar@city.ee>
  17. */
  18. class GroupHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface
  19. {
  20. use ProcessableHandlerTrait;
  21. protected $handlers;
  22. /**
  23. * @param HandlerInterface[] $handlers Array of Handlers.
  24. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  25. */
  26. public function __construct(array $handlers, bool $bubble = true)
  27. {
  28. foreach ($handlers as $handler) {
  29. if (!$handler instanceof HandlerInterface) {
  30. throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.');
  31. }
  32. }
  33. $this->handlers = $handlers;
  34. $this->bubble = $bubble;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function isHandling(array $record): bool
  40. {
  41. foreach ($this->handlers as $handler) {
  42. if ($handler->isHandling($record)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function handle(array $record): bool
  52. {
  53. if ($this->processors) {
  54. $record = $this->processRecord($record);
  55. }
  56. foreach ($this->handlers as $handler) {
  57. $handler->handle($record);
  58. }
  59. return false === $this->bubble;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function handleBatch(array $records): void
  65. {
  66. if ($this->processors) {
  67. $processed = [];
  68. foreach ($records as $record) {
  69. foreach ($this->processors as $processor) {
  70. $processed[] = call_user_func($processor, $record);
  71. }
  72. }
  73. $records = $processed;
  74. }
  75. foreach ($this->handlers as $handler) {
  76. $handler->handleBatch($records);
  77. }
  78. }
  79. public function reset()
  80. {
  81. $this->resetProcessors();
  82. foreach ($this->handlers as $handler) {
  83. if ($handler instanceof ResettableInterface) {
  84. $handler->reset();
  85. }
  86. }
  87. }
  88. public function close(): void
  89. {
  90. parent::close();
  91. foreach ($this->handlers as $handler) {
  92. $handler->close();
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  99. {
  100. foreach ($this->handlers as $handler) {
  101. $handler->setFormatter($formatter);
  102. }
  103. return $this;
  104. }
  105. }