1<?php declare(strict_types=1);
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
14use Monolog\Logger;
15use Monolog\ResettableInterface;
16use Monolog\Formatter\FormatterInterface;
17use Psr\Log\LogLevel;
18
19/**
20 * Simple handler wrapper that filters records based on a list of levels
21 *
22 * It can be configured with an exact list of levels to allow, or a min/max level.
23 *
24 * @author Hennadiy Verkh
25 * @author Jordi Boggiano <j.boggiano@seld.be>
26 *
27 * @phpstan-import-type Record from \Monolog\Logger
28 * @phpstan-import-type Level from \Monolog\Logger
29 * @phpstan-import-type LevelName from \Monolog\Logger
30 */
31class FilterHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface, FormattableHandlerInterface
32{
33    use ProcessableHandlerTrait;
34
35    /**
36     * Handler or factory callable($record, $this)
37     *
38     * @var callable|HandlerInterface
39     * @phpstan-var callable(?Record, HandlerInterface): HandlerInterface|HandlerInterface
40     */
41    protected $handler;
42
43    /**
44     * Minimum level for logs that are passed to handler
45     *
46     * @var int[]
47     * @phpstan-var array<Level, int>
48     */
49    protected $acceptedLevels;
50
51    /**
52     * Whether the messages that are handled can bubble up the stack or not
53     *
54     * @var bool
55     */
56    protected $bubble;
57
58    /**
59     * @psalm-param HandlerInterface|callable(?Record, HandlerInterface): HandlerInterface $handler
60     *
61     * @param callable|HandlerInterface $handler        Handler or factory callable($record|null, $filterHandler).
62     * @param int|array                 $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided
63     * @param int|string                $maxLevel       Maximum level to accept, only used if $minLevelOrList is not an array
64     * @param bool                      $bubble         Whether the messages that are handled can bubble up the stack or not
65     *
66     * @phpstan-param Level|LevelName|LogLevel::*|array<Level|LevelName|LogLevel::*> $minLevelOrList
67     * @phpstan-param Level|LevelName|LogLevel::* $maxLevel
68     */
69    public function __construct($handler, $minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, bool $bubble = true)
70    {
71        $this->handler  = $handler;
72        $this->bubble   = $bubble;
73        $this->setAcceptedLevels($minLevelOrList, $maxLevel);
74
75        if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
76            throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
77        }
78    }
79
80    /**
81     * @phpstan-return array<int, Level>
82     */
83    public function getAcceptedLevels(): array
84    {
85        return array_flip($this->acceptedLevels);
86    }
87
88    /**
89     * @param int|string|array $minLevelOrList A list of levels to accept or a minimum level or level name if maxLevel is provided
90     * @param int|string       $maxLevel       Maximum level or level name to accept, only used if $minLevelOrList is not an array
91     *
92     * @phpstan-param Level|LevelName|LogLevel::*|array<Level|LevelName|LogLevel::*> $minLevelOrList
93     * @phpstan-param Level|LevelName|LogLevel::*                                    $maxLevel
94     */
95    public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY): self
96    {
97        if (is_array($minLevelOrList)) {
98            $acceptedLevels = array_map('Monolog\Logger::toMonologLevel', $minLevelOrList);
99        } else {
100            $minLevelOrList = Logger::toMonologLevel($minLevelOrList);
101            $maxLevel = Logger::toMonologLevel($maxLevel);
102            $acceptedLevels = array_values(array_filter(Logger::getLevels(), function ($level) use ($minLevelOrList, $maxLevel) {
103                return $level >= $minLevelOrList && $level <= $maxLevel;
104            }));
105        }
106        $this->acceptedLevels = array_flip($acceptedLevels);
107
108        return $this;
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    public function isHandling(array $record): bool
115    {
116        return isset($this->acceptedLevels[$record['level']]);
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    public function handle(array $record): bool
123    {
124        if (!$this->isHandling($record)) {
125            return false;
126        }
127
128        if ($this->processors) {
129            /** @var Record $record */
130            $record = $this->processRecord($record);
131        }
132
133        $this->getHandler($record)->handle($record);
134
135        return false === $this->bubble;
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    public function handleBatch(array $records): void
142    {
143        $filtered = [];
144        foreach ($records as $record) {
145            if ($this->isHandling($record)) {
146                $filtered[] = $record;
147            }
148        }
149
150        if (count($filtered) > 0) {
151            $this->getHandler($filtered[count($filtered) - 1])->handleBatch($filtered);
152        }
153    }
154
155    /**
156     * Return the nested handler
157     *
158     * If the handler was provided as a factory callable, this will trigger the handler's instantiation.
159     *
160     * @return HandlerInterface
161     *
162     * @phpstan-param Record $record
163     */
164    public function getHandler(array $record = null)
165    {
166        if (!$this->handler instanceof HandlerInterface) {
167            $this->handler = ($this->handler)($record, $this);
168            if (!$this->handler instanceof HandlerInterface) {
169                throw new \RuntimeException("The factory callable should return a HandlerInterface");
170            }
171        }
172
173        return $this->handler;
174    }
175
176    /**
177     * {@inheritDoc}
178     */
179    public function setFormatter(FormatterInterface $formatter): HandlerInterface
180    {
181        $handler = $this->getHandler();
182        if ($handler instanceof FormattableHandlerInterface) {
183            $handler->setFormatter($formatter);
184
185            return $this;
186        }
187
188        throw new \UnexpectedValueException('The nested handler of type '.get_class($handler).' does not support formatters.');
189    }
190
191    /**
192     * {@inheritDoc}
193     */
194    public function getFormatter(): FormatterInterface
195    {
196        $handler = $this->getHandler();
197        if ($handler instanceof FormattableHandlerInterface) {
198            return $handler->getFormatter();
199        }
200
201        throw new \UnexpectedValueException('The nested handler of type '.get_class($handler).' does not support formatters.');
202    }
203
204    public function reset()
205    {
206        $this->resetProcessors();
207
208        if ($this->getHandler() instanceof ResettableInterface) {
209            $this->getHandler()->reset();
210        }
211    }
212}
213