@@ -37,121 +37,121 @@ |
||
| 37 | 37 | |
| 38 | 38 | class SymfonyAdapter implements EventDispatcherInterface { |
| 39 | 39 | |
| 40 | - /** @var EventDispatcher */ |
|
| 41 | - private $eventDispatcher; |
|
| 42 | - /** @var ILogger */ |
|
| 43 | - private $logger; |
|
| 44 | - |
|
| 45 | - public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) { |
|
| 46 | - $this->eventDispatcher = $eventDispatcher; |
|
| 47 | - $this->logger = $logger; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Dispatches an event to all registered listeners. |
|
| 52 | - * |
|
| 53 | - * @param string $eventName The name of the event to dispatch. The name of |
|
| 54 | - * the event is the name of the method that is |
|
| 55 | - * invoked on listeners. |
|
| 56 | - * @param Event|null $event The event to pass to the event handlers/listeners |
|
| 57 | - * If not supplied, an empty Event instance is created |
|
| 58 | - * |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public function dispatch($eventName, $event = null) { |
|
| 62 | - // type hinting is not possible, due to usage of GenericEvent |
|
| 63 | - if ($event instanceof Event) { |
|
| 64 | - $this->eventDispatcher->dispatch($eventName, $event); |
|
| 65 | - } else { |
|
| 66 | - if ($event instanceof GenericEvent && get_class($event) === GenericEvent::class) { |
|
| 67 | - $newEvent = new GenericEventWrapper($this->logger, $eventName, $event); |
|
| 68 | - } else { |
|
| 69 | - $newEvent = $event; |
|
| 70 | - |
|
| 71 | - // Legacy event |
|
| 72 | - $this->logger->info( |
|
| 73 | - 'Deprecated event type for {name}: {class}', |
|
| 74 | - ['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null'] |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $newEvent); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Adds an event listener that listens on the specified events. |
|
| 83 | - * |
|
| 84 | - * @param string $eventName The event to listen on |
|
| 85 | - * @param callable $listener The listener |
|
| 86 | - * @param int $priority The higher this value, the earlier an event |
|
| 87 | - * listener will be triggered in the chain (defaults to 0) |
|
| 88 | - */ |
|
| 89 | - public function addListener($eventName, $listener, $priority = 0) { |
|
| 90 | - if (is_callable($listener)) { |
|
| 91 | - $this->eventDispatcher->addListener($eventName, $listener, $priority); |
|
| 92 | - } else { |
|
| 93 | - // Legacy listener |
|
| 94 | - $this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority); |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Adds an event subscriber. |
|
| 100 | - * |
|
| 101 | - * The subscriber is asked for all the events it is |
|
| 102 | - * interested in and added as a listener for these events. |
|
| 103 | - */ |
|
| 104 | - public function addSubscriber(EventSubscriberInterface $subscriber) { |
|
| 105 | - $this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Removes an event listener from the specified events. |
|
| 110 | - * |
|
| 111 | - * @param string $eventName The event to remove a listener from |
|
| 112 | - * @param callable $listener The listener to remove |
|
| 113 | - */ |
|
| 114 | - public function removeListener($eventName, $listener) { |
|
| 115 | - $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function removeSubscriber(EventSubscriberInterface $subscriber) { |
|
| 119 | - $this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Gets the listeners of a specific event or all listeners sorted by descending priority. |
|
| 124 | - * |
|
| 125 | - * @param string|null $eventName The name of the event |
|
| 126 | - * |
|
| 127 | - * @return array The event listeners for the specified event, or all event listeners by event name |
|
| 128 | - */ |
|
| 129 | - public function getListeners($eventName = null) { |
|
| 130 | - return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Gets the listener priority for a specific event. |
|
| 135 | - * |
|
| 136 | - * Returns null if the event or the listener does not exist. |
|
| 137 | - * |
|
| 138 | - * @param string $eventName The name of the event |
|
| 139 | - * @param callable $listener The listener |
|
| 140 | - * |
|
| 141 | - * @return int|null The event listener priority |
|
| 142 | - */ |
|
| 143 | - public function getListenerPriority($eventName, $listener) { |
|
| 144 | - return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Checks whether an event has any registered listeners. |
|
| 149 | - * |
|
| 150 | - * @param string|null $eventName The name of the event |
|
| 151 | - * |
|
| 152 | - * @return bool true if the specified event has any listeners, false otherwise |
|
| 153 | - */ |
|
| 154 | - public function hasListeners($eventName = null) { |
|
| 155 | - return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName); |
|
| 156 | - } |
|
| 40 | + /** @var EventDispatcher */ |
|
| 41 | + private $eventDispatcher; |
|
| 42 | + /** @var ILogger */ |
|
| 43 | + private $logger; |
|
| 44 | + |
|
| 45 | + public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) { |
|
| 46 | + $this->eventDispatcher = $eventDispatcher; |
|
| 47 | + $this->logger = $logger; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Dispatches an event to all registered listeners. |
|
| 52 | + * |
|
| 53 | + * @param string $eventName The name of the event to dispatch. The name of |
|
| 54 | + * the event is the name of the method that is |
|
| 55 | + * invoked on listeners. |
|
| 56 | + * @param Event|null $event The event to pass to the event handlers/listeners |
|
| 57 | + * If not supplied, an empty Event instance is created |
|
| 58 | + * |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public function dispatch($eventName, $event = null) { |
|
| 62 | + // type hinting is not possible, due to usage of GenericEvent |
|
| 63 | + if ($event instanceof Event) { |
|
| 64 | + $this->eventDispatcher->dispatch($eventName, $event); |
|
| 65 | + } else { |
|
| 66 | + if ($event instanceof GenericEvent && get_class($event) === GenericEvent::class) { |
|
| 67 | + $newEvent = new GenericEventWrapper($this->logger, $eventName, $event); |
|
| 68 | + } else { |
|
| 69 | + $newEvent = $event; |
|
| 70 | + |
|
| 71 | + // Legacy event |
|
| 72 | + $this->logger->info( |
|
| 73 | + 'Deprecated event type for {name}: {class}', |
|
| 74 | + ['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null'] |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $newEvent); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Adds an event listener that listens on the specified events. |
|
| 83 | + * |
|
| 84 | + * @param string $eventName The event to listen on |
|
| 85 | + * @param callable $listener The listener |
|
| 86 | + * @param int $priority The higher this value, the earlier an event |
|
| 87 | + * listener will be triggered in the chain (defaults to 0) |
|
| 88 | + */ |
|
| 89 | + public function addListener($eventName, $listener, $priority = 0) { |
|
| 90 | + if (is_callable($listener)) { |
|
| 91 | + $this->eventDispatcher->addListener($eventName, $listener, $priority); |
|
| 92 | + } else { |
|
| 93 | + // Legacy listener |
|
| 94 | + $this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Adds an event subscriber. |
|
| 100 | + * |
|
| 101 | + * The subscriber is asked for all the events it is |
|
| 102 | + * interested in and added as a listener for these events. |
|
| 103 | + */ |
|
| 104 | + public function addSubscriber(EventSubscriberInterface $subscriber) { |
|
| 105 | + $this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Removes an event listener from the specified events. |
|
| 110 | + * |
|
| 111 | + * @param string $eventName The event to remove a listener from |
|
| 112 | + * @param callable $listener The listener to remove |
|
| 113 | + */ |
|
| 114 | + public function removeListener($eventName, $listener) { |
|
| 115 | + $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function removeSubscriber(EventSubscriberInterface $subscriber) { |
|
| 119 | + $this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Gets the listeners of a specific event or all listeners sorted by descending priority. |
|
| 124 | + * |
|
| 125 | + * @param string|null $eventName The name of the event |
|
| 126 | + * |
|
| 127 | + * @return array The event listeners for the specified event, or all event listeners by event name |
|
| 128 | + */ |
|
| 129 | + public function getListeners($eventName = null) { |
|
| 130 | + return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Gets the listener priority for a specific event. |
|
| 135 | + * |
|
| 136 | + * Returns null if the event or the listener does not exist. |
|
| 137 | + * |
|
| 138 | + * @param string $eventName The name of the event |
|
| 139 | + * @param callable $listener The listener |
|
| 140 | + * |
|
| 141 | + * @return int|null The event listener priority |
|
| 142 | + */ |
|
| 143 | + public function getListenerPriority($eventName, $listener) { |
|
| 144 | + return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Checks whether an event has any registered listeners. |
|
| 149 | + * |
|
| 150 | + * @param string|null $eventName The name of the event |
|
| 151 | + * |
|
| 152 | + * @return bool true if the specified event has any listeners, false otherwise |
|
| 153 | + */ |
|
| 154 | + public function hasListeners($eventName = null) { |
|
| 155 | + return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName); |
|
| 156 | + } |
|
| 157 | 157 | } |