friendica-addons/monolog/vendor/monolog/monolog/src/Monolog/Handler/GroupHandler.php

133 lines
3.2 KiB
PHP
Raw Normal View History

2022-10-17 21:25:03 +02:00
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\ResettableInterface;
/**
* Forwards records to multiple handlers
*
* @author Lenar Lõhmus <lenar@city.ee>
2023-01-18 00:17:49 +01:00
*
* @phpstan-import-type Record from \Monolog\Logger
2022-10-17 21:25:03 +02:00
*/
class GroupHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface
{
use ProcessableHandlerTrait;
/** @var HandlerInterface[] */
2023-01-18 00:17:49 +01:00
protected $handlers;
/** @var bool */
protected $bubble;
2022-10-17 21:25:03 +02:00
/**
* @param HandlerInterface[] $handlers Array of Handlers.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(array $handlers, bool $bubble = true)
{
foreach ($handlers as $handler) {
if (!$handler instanceof HandlerInterface) {
throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.');
}
}
$this->handlers = $handlers;
$this->bubble = $bubble;
}
/**
2023-01-18 00:17:49 +01:00
* {@inheritDoc}
2022-10-17 21:25:03 +02:00
*/
2023-01-18 00:17:49 +01:00
public function isHandling(array $record): bool
2022-10-17 21:25:03 +02:00
{
foreach ($this->handlers as $handler) {
if ($handler->isHandling($record)) {
return true;
}
}
return false;
}
/**
2023-01-18 00:17:49 +01:00
* {@inheritDoc}
2022-10-17 21:25:03 +02:00
*/
2023-01-18 00:17:49 +01:00
public function handle(array $record): bool
2022-10-17 21:25:03 +02:00
{
2023-01-18 00:17:49 +01:00
if ($this->processors) {
/** @var Record $record */
2022-10-17 21:25:03 +02:00
$record = $this->processRecord($record);
}
foreach ($this->handlers as $handler) {
$handler->handle($record);
}
return false === $this->bubble;
}
/**
2023-01-18 00:17:49 +01:00
* {@inheritDoc}
2022-10-17 21:25:03 +02:00
*/
public function handleBatch(array $records): void
{
2023-01-18 00:17:49 +01:00
if ($this->processors) {
2022-10-17 21:25:03 +02:00
$processed = [];
foreach ($records as $record) {
$processed[] = $this->processRecord($record);
}
2023-01-18 00:17:49 +01:00
/** @var Record[] $records */
2022-10-17 21:25:03 +02:00
$records = $processed;
}
foreach ($this->handlers as $handler) {
$handler->handleBatch($records);
}
}
2023-01-18 00:17:49 +01:00
public function reset()
2022-10-17 21:25:03 +02:00
{
$this->resetProcessors();
foreach ($this->handlers as $handler) {
if ($handler instanceof ResettableInterface) {
$handler->reset();
}
}
}
public function close(): void
{
parent::close();
foreach ($this->handlers as $handler) {
$handler->close();
}
}
/**
2023-01-18 00:17:49 +01:00
* {@inheritDoc}
2022-10-17 21:25:03 +02:00
*/
public function setFormatter(FormatterInterface $formatter): HandlerInterface
{
foreach ($this->handlers as $handler) {
if ($handler instanceof FormattableHandlerInterface) {
$handler->setFormatter($formatter);
}
}
return $this;
}
}