GroupHandler.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  13. * Forwards records to multiple handlers
  14. *
  15. * @author Lenar Lõhmus <lenar@city.ee>
  16. */
  17. class GroupHandler extends AbstractHandler
  18. {
  19. protected $handlers;
  20. /**
  21. * @param array $handlers Array of Handlers.
  22. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  23. */
  24. public function __construct(array $handlers, $bubble = true)
  25. {
  26. foreach ($handlers as $handler) {
  27. if (!$handler instanceof HandlerInterface) {
  28. throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.');
  29. }
  30. }
  31. $this->handlers = $handlers;
  32. $this->bubble = $bubble;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function isHandling(array $record)
  38. {
  39. foreach ($this->handlers as $handler) {
  40. if ($handler->isHandling($record)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function handle(array $record)
  50. {
  51. if ($this->processors) {
  52. foreach ($this->processors as $processor) {
  53. $record = call_user_func($processor, $record);
  54. }
  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)
  65. {
  66. if ($this->processors) {
  67. $processed = array();
  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. /**
  80. * {@inheritdoc}
  81. */
  82. public function setFormatter(FormatterInterface $formatter)
  83. {
  84. foreach ($this->handlers as $handler) {
  85. $handler->setFormatter($formatter);
  86. }
  87. return $this;
  88. }
  89. }