|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Contributte\EventDispatcher\Diagnostics; |
| 4 | + |
| 5 | +use Psr\Log\LoggerInterface; |
| 6 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 7 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 8 | + |
| 9 | +class DebugDispatcher implements EventDispatcherInterface |
| 10 | +{ |
| 11 | + |
| 12 | + private EventDispatcherInterface $original; |
| 13 | + |
| 14 | + /** @var LoggerInterface[] */ |
| 15 | + private array $loggers = []; |
| 16 | + |
| 17 | + public function __construct(EventDispatcherInterface $original) |
| 18 | + { |
| 19 | + $this->original = $original; |
| 20 | + } |
| 21 | + |
| 22 | + public function addLogger(LoggerInterface $logger): void |
| 23 | + { |
| 24 | + $this->loggers[] = $logger; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param LoggerInterface[] $loggers |
| 29 | + */ |
| 30 | + public function setLoggers(array $loggers = []): void |
| 31 | + { |
| 32 | + $this->loggers = $loggers; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return LoggerInterface[] |
| 37 | + */ |
| 38 | + public function getLoggers(): array |
| 39 | + { |
| 40 | + return $this->loggers; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + public function addListener(string $eventName, callable $listener, int $priority = 0): void |
| 47 | + { |
| 48 | + $this->original->addListener($eventName, $listener, $priority); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + */ |
| 54 | + public function addSubscriber(EventSubscriberInterface $subscriber): void |
| 55 | + { |
| 56 | + $this->original->addSubscriber($subscriber); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function removeListener(string $eventName, callable $listener): void |
| 63 | + { |
| 64 | + $this->original->removeListener($eventName, $listener); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function removeSubscriber(EventSubscriberInterface $subscriber): void |
| 71 | + { |
| 72 | + $this->original->removeSubscriber($subscriber); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + public function getListeners(?string $eventName = null): array |
| 79 | + { |
| 80 | + return $this->original->getListeners($eventName); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritdoc} |
| 85 | + */ |
| 86 | + public function getListenerPriority(string $eventName, callable $listener): ?int |
| 87 | + { |
| 88 | + return $this->original->getListenerPriority($eventName, $listener); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * {@inheritdoc} |
| 93 | + */ |
| 94 | + public function hasListeners(?string $eventName = null): bool |
| 95 | + { |
| 96 | + return $this->original->hasListeners($eventName); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * {@inheritdoc} |
| 101 | + */ |
| 102 | + public function dispatch(object $event, ?string $eventName = null): object |
| 103 | + { |
| 104 | + $trace = new EventTrace($event, $eventName); |
| 105 | + |
| 106 | + // Iterate over all loggers |
| 107 | + foreach ($this->loggers as $logger) { |
| 108 | + $logger->debug(sprintf('EventDispatcher@%s: event started', $trace->name), ['event' => $trace]); |
| 109 | + } |
| 110 | + |
| 111 | + // Start timer |
| 112 | + $start = microtime(true); |
| 113 | + |
| 114 | + // Dispatch event |
| 115 | + $return = $this->original->dispatch($event, $eventName); |
| 116 | + |
| 117 | + // If event was handled, mark it |
| 118 | + if ($this->original->hasListeners($trace->name)) { |
| 119 | + $trace->handled = true; |
| 120 | + } |
| 121 | + |
| 122 | + // Calculate duration |
| 123 | + $trace->duration = microtime(true) - $start; |
| 124 | + |
| 125 | + foreach ($this->loggers as $logger) { |
| 126 | + $logger->debug(sprintf('EventDispatcher@%s: event dispatched', $trace->name), ['event' => $trace]); |
| 127 | + } |
| 128 | + |
| 129 | + return $return; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments