GroupHandler.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  12. * Forwards records to multiple handlers
  13. *
  14. * @author Lenar Lõhmus <lenar@city.ee>
  15. */
  16. class GroupHandler extends AbstractHandler
  17. {
  18. protected $handlers;
  19. /**
  20. * @param array $handlers Array of Handlers.
  21. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  22. */
  23. public function __construct(array $handlers, $bubble = true)
  24. {
  25. foreach ($handlers as $handler) {
  26. if (!$handler instanceof HandlerInterface) {
  27. throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.');
  28. }
  29. }
  30. $this->handlers = $handlers;
  31. $this->bubble = $bubble;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function isHandling(array $record)
  37. {
  38. foreach ($this->handlers as $handler) {
  39. if ($handler->isHandling($record)) {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function handle(array $record)
  49. {
  50. foreach ($this->handlers as $handler) {
  51. $handler->handle($record);
  52. }
  53. return false === $this->bubble;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function handleBatch(array $records)
  59. {
  60. foreach ($this->handlers as $handler) {
  61. $handler->handleBatch($records);
  62. }
  63. }
  64. }