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
14/**
15 * Base Handler class providing the Handler structure, including processors and formatters
16 *
17 * Classes extending it should (in most cases) only implement write($record)
18 *
19 * @author Jordi Boggiano <j.boggiano@seld.be>
20 * @author Christophe Coevoet <stof@notk.org>
21 *
22 * @phpstan-import-type LevelName from \Monolog\Logger
23 * @phpstan-import-type Level from \Monolog\Logger
24 * @phpstan-import-type Record from \Monolog\Logger
25 * @phpstan-type FormattedRecord array{message: string, context: mixed[], level: Level, level_name: LevelName, channel: string, datetime: \DateTimeImmutable, extra: mixed[], formatted: mixed}
26 */
27abstract class AbstractProcessingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
28{
29    use ProcessableHandlerTrait;
30    use FormattableHandlerTrait;
31
32    /**
33     * {@inheritDoc}
34     */
35    public function handle(array $record): bool
36    {
37        if (!$this->isHandling($record)) {
38            return false;
39        }
40
41        if ($this->processors) {
42            /** @var Record $record */
43            $record = $this->processRecord($record);
44        }
45
46        $record['formatted'] = $this->getFormatter()->format($record);
47
48        $this->write($record);
49
50        return false === $this->bubble;
51    }
52
53    /**
54     * Writes the record down to the log of the implementing handler
55     *
56     * @phpstan-param FormattedRecord $record
57     */
58    abstract protected function write(array $record): void;
59
60    /**
61     * @return void
62     */
63    public function reset()
64    {
65        parent::reset();
66
67        $this->resetProcessors();
68    }
69}
70