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 Monolog\ResettableInterface; 15use Monolog\Processor\ProcessorInterface; 16 17/** 18 * Helper trait for implementing ProcessableInterface 19 * 20 * @author Jordi Boggiano <j.boggiano@seld.be> 21 * 22 * @phpstan-import-type Record from \Monolog\Logger 23 */ 24trait ProcessableHandlerTrait 25{ 26 /** 27 * @var callable[] 28 * @phpstan-var array<ProcessorInterface|callable(Record): Record> 29 */ 30 protected $processors = []; 31 32 /** 33 * {@inheritDoc} 34 */ 35 public function pushProcessor(callable $callback): HandlerInterface 36 { 37 array_unshift($this->processors, $callback); 38 39 return $this; 40 } 41 42 /** 43 * {@inheritDoc} 44 */ 45 public function popProcessor(): callable 46 { 47 if (!$this->processors) { 48 throw new \LogicException('You tried to pop from an empty processor stack.'); 49 } 50 51 return array_shift($this->processors); 52 } 53 54 /** 55 * Processes a record. 56 * 57 * @phpstan-param Record $record 58 * @phpstan-return Record 59 */ 60 protected function processRecord(array $record): array 61 { 62 foreach ($this->processors as $processor) { 63 $record = $processor($record); 64 } 65 66 return $record; 67 } 68 69 protected function resetProcessors(): void 70 { 71 foreach ($this->processors as $processor) { 72 if ($processor instanceof ResettableInterface) { 73 $processor->reset(); 74 } 75 } 76 } 77} 78