HandlerWrapper.php 3.2 KB

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