1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-23 09:36:11 +02:00
Files
php-monolog/src/Monolog/Handler/ProcessableHandlerTrait.php
2016-05-27 13:44:00 +01:00

66 lines
1.5 KiB
PHP

<?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;
/**
* Helper trait for implementing ProcessableInterface
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
trait ProcessableHandlerTrait
{
/**
* @var callable[]
*/
protected $processors = [];
/**
* {@inheritdoc}
*/
public function pushProcessor(callable $callback): HandlerInterface
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
}
array_unshift($this->processors, $callback);
return $this;
}
/**
* {@inheritdoc}
*/
public function popProcessor(): callable
{
if (!$this->processors) {
throw new \LogicException('You tried to pop from an empty processor stack.');
}
return array_shift($this->processors);
}
/**
* Processes a record.
*
* @param array $record
* @return array
*/
protected function processRecord(array $record)
{
foreach ($this->processors as $processor) {
$record = $processor($record);
}
return $record;
}
}