HandlerWrapper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * This simple wrapper class can be used to extend handlers functionality.
  14. *
  15. * Example: A filtering handle. Inherit from this class, override isHandling() like this
  16. *
  17. * public function isHandling(array $record)
  18. * {
  19. * if ($record meets certain conditions) {
  20. * return false;
  21. * }
  22. * return $this->handler->isHandling($record);
  23. * }
  24. *
  25. * @author Alexey Karapetov <alexey@karapetov.com>
  26. */
  27. class HandlerWrapper implements HandlerInterface, ProcessableHandlerInterface, FormattableHandlerInterface
  28. {
  29. /**
  30. * @var HandlerInterface
  31. */
  32. protected $handler;
  33. /**
  34. * HandlerWrapper constructor.
  35. * @param HandlerInterface $handler
  36. */
  37. public function __construct(HandlerInterface $handler)
  38. {
  39. $this->handler = $handler;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function isHandling(array $record): bool
  45. {
  46. return $this->handler->isHandling($record);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function handle(array $record): bool
  52. {
  53. return $this->handler->handle($record);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function handleBatch(array $records)
  59. {
  60. return $this->handler->handleBatch($records);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function close()
  66. {
  67. return $this->handler->close();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function pushProcessor(callable $callback): HandlerInterface
  73. {
  74. if ($this->handler instanceof ProcessableHandlerInterface) {
  75. $this->handler->pushProcessor($callback);
  76. return $this;
  77. }
  78. throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function popProcessor(): callable
  84. {
  85. if ($this->handler instanceof ProcessableHandlerInterface) {
  86. return $this->handler->popProcessor();
  87. }
  88. throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  94. {
  95. if ($this->handler instanceof FormattableHandlerInterface) {
  96. $this->handler->setFormatter($formatter);
  97. }
  98. throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getFormatter(): FormatterInterface
  104. {
  105. if ($this->handler instanceof FormattableHandlerInterface) {
  106. return $this->handler->getFormatter($formatter);
  107. }
  108. throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
  109. }
  110. }