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 Throwable;
15
16/**
17 * Forwards records to at most one handler
18 *
19 * If a handler fails, the exception is suppressed and the record is forwarded to the next handler.
20 *
21 * As soon as one handler handles a record successfully, the handling stops there.
22 *
23 * @phpstan-import-type Record from \Monolog\Logger
24 */
25class FallbackGroupHandler extends GroupHandler
26{
27    /**
28     * {@inheritDoc}
29     */
30    public function handle(array $record): bool
31    {
32        if ($this->processors) {
33            /** @var Record $record */
34            $record = $this->processRecord($record);
35        }
36        foreach ($this->handlers as $handler) {
37            try {
38                $handler->handle($record);
39                break;
40            } catch (Throwable $e) {
41                // What throwable?
42            }
43        }
44
45        return false === $this->bubble;
46    }
47
48    /**
49     * {@inheritDoc}
50     */
51    public function handleBatch(array $records): void
52    {
53        if ($this->processors) {
54            $processed = [];
55            foreach ($records as $record) {
56                $processed[] = $this->processRecord($record);
57            }
58            /** @var Record[] $records */
59            $records = $processed;
60        }
61
62        foreach ($this->handlers as $handler) {
63            try {
64                $handler->handleBatch($records);
65                break;
66            } catch (Throwable $e) {
67                // What throwable?
68            }
69        }
70    }
71}
72