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\ResettableInterface;
|
|
|
|
use Monolog\Processor\ProcessorInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper trait for implementing ProcessableInterface
|
|
|
|
*
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2023-07-02 23:57:24 +02:00
|
|
|
*
|
|
|
|
* @phpstan-import-type Record from \Monolog\Logger
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
trait ProcessableHandlerTrait
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var callable[]
|
2023-07-02 23:57:24 +02:00
|
|
|
* @phpstan-var array<ProcessorInterface|callable(Record): Record>
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
protected $processors = [];
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
public function pushProcessor(callable $callback): HandlerInterface
|
|
|
|
{
|
|
|
|
array_unshift($this->processors, $callback);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
public function popProcessor(): callable
|
|
|
|
{
|
2023-07-02 23:57:24 +02:00
|
|
|
if (!$this->processors) {
|
2022-10-17 21:25:03 +02:00
|
|
|
throw new \LogicException('You tried to pop from an empty processor stack.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_shift($this->processors);
|
|
|
|
}
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
/**
|
|
|
|
* Processes a record.
|
|
|
|
*
|
|
|
|
* @phpstan-param Record $record
|
|
|
|
* @phpstan-return Record
|
|
|
|
*/
|
|
|
|
protected function processRecord(array $record): array
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
foreach ($this->processors as $processor) {
|
|
|
|
$record = $processor($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function resetProcessors(): void
|
|
|
|
{
|
|
|
|
foreach ($this->processors as $processor) {
|
|
|
|
if ($processor instanceof ResettableInterface) {
|
|
|
|
$processor->reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|