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