GroupHandler.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  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 AbstractHandler
  19. {
  20. protected $handlers;
  21. /**
  22. * @param array $handlers Array of Handlers.
  23. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  24. */
  25. public function __construct(array $handlers, $bubble = true)
  26. {
  27. foreach ($handlers as $handler) {
  28. if (!$handler instanceof HandlerInterface) {
  29. throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.');
  30. }
  31. }
  32. $this->handlers = $handlers;
  33. $this->bubble = $bubble;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function isHandling(array $record)
  39. {
  40. foreach ($this->handlers as $handler) {
  41. if ($handler->isHandling($record)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function handle(array $record)
  51. {
  52. if ($this->processors) {
  53. foreach ($this->processors as $processor) {
  54. $record = call_user_func($processor, $record);
  55. }
  56. }
  57. foreach ($this->handlers as $handler) {
  58. $handler->handle($record);
  59. }
  60. return false === $this->bubble;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function handleBatch(array $records)
  66. {
  67. if ($this->processors) {
  68. $processed = array();
  69. foreach ($records as $record) {
  70. foreach ($this->processors as $processor) {
  71. $record = call_user_func($processor, $record);
  72. }
  73. $processed[] = $record;
  74. }
  75. $records = $processed;
  76. }
  77. foreach ($this->handlers as $handler) {
  78. $handler->handleBatch($records);
  79. }
  80. }
  81. public function reset()
  82. {
  83. parent::reset();
  84. foreach ($this->handlers as $handler) {
  85. if ($handler instanceof ResettableInterface) {
  86. $handler->reset();
  87. }
  88. }
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function setFormatter(FormatterInterface $formatter)
  94. {
  95. foreach ($this->handlers as $handler) {
  96. $handler->setFormatter($formatter);
  97. }
  98. return $this;
  99. }
  100. }