comparison vendor/symfony/event-dispatcher/EventDispatcher.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
26 * @author Jordan Alliot <jordan.alliot@gmail.com> 26 * @author Jordan Alliot <jordan.alliot@gmail.com>
27 * @author Nicolas Grekas <p@tchwork.com> 27 * @author Nicolas Grekas <p@tchwork.com>
28 */ 28 */
29 class EventDispatcher implements EventDispatcherInterface 29 class EventDispatcher implements EventDispatcherInterface
30 { 30 {
31 private $listeners = array(); 31 private $listeners = [];
32 private $sorted = array(); 32 private $sorted = [];
33 33
34 /** 34 /**
35 * {@inheritdoc} 35 * {@inheritdoc}
36 */ 36 */
37 public function dispatch($eventName, Event $event = null) 37 public function dispatch($eventName, Event $event = null)
52 */ 52 */
53 public function getListeners($eventName = null) 53 public function getListeners($eventName = null)
54 { 54 {
55 if (null !== $eventName) { 55 if (null !== $eventName) {
56 if (empty($this->listeners[$eventName])) { 56 if (empty($this->listeners[$eventName])) {
57 return array(); 57 return [];
58 } 58 }
59 59
60 if (!isset($this->sorted[$eventName])) { 60 if (!isset($this->sorted[$eventName])) {
61 $this->sortListeners($eventName); 61 $this->sortListeners($eventName);
62 } 62 }
80 { 80 {
81 if (empty($this->listeners[$eventName])) { 81 if (empty($this->listeners[$eventName])) {
82 return; 82 return;
83 } 83 }
84 84
85 if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { 85 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
86 $listener[0] = $listener[0](); 86 $listener[0] = $listener[0]();
87 } 87 }
88 88
89 foreach ($this->listeners[$eventName] as $priority => $listeners) { 89 foreach ($this->listeners[$eventName] as $priority => $listeners) {
90 foreach ($listeners as $k => $v) { 90 foreach ($listeners as $k => $v) {
91 if ($v !== $listener && is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) { 91 if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
92 $v[0] = $v[0](); 92 $v[0] = $v[0]();
93 $this->listeners[$eventName][$priority][$k] = $v; 93 $this->listeners[$eventName][$priority][$k] = $v;
94 } 94 }
95 if ($v === $listener) { 95 if ($v === $listener) {
96 return $priority; 96 return $priority;
133 { 133 {
134 if (empty($this->listeners[$eventName])) { 134 if (empty($this->listeners[$eventName])) {
135 return; 135 return;
136 } 136 }
137 137
138 if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { 138 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
139 $listener[0] = $listener[0](); 139 $listener[0] = $listener[0]();
140 } 140 }
141 141
142 foreach ($this->listeners[$eventName] as $priority => $listeners) { 142 foreach ($this->listeners[$eventName] as $priority => $listeners) {
143 foreach ($listeners as $k => $v) { 143 foreach ($listeners as $k => $v) {
144 if ($v !== $listener && is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) { 144 if ($v !== $listener && \is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) {
145 $v[0] = $v[0](); 145 $v[0] = $v[0]();
146 } 146 }
147 if ($v === $listener) { 147 if ($v === $listener) {
148 unset($listeners[$k], $this->sorted[$eventName]); 148 unset($listeners[$k], $this->sorted[$eventName]);
149 } else { 149 } else {
163 * {@inheritdoc} 163 * {@inheritdoc}
164 */ 164 */
165 public function addSubscriber(EventSubscriberInterface $subscriber) 165 public function addSubscriber(EventSubscriberInterface $subscriber)
166 { 166 {
167 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { 167 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
168 if (is_string($params)) { 168 if (\is_string($params)) {
169 $this->addListener($eventName, array($subscriber, $params)); 169 $this->addListener($eventName, [$subscriber, $params]);
170 } elseif (is_string($params[0])) { 170 } elseif (\is_string($params[0])) {
171 $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0); 171 $this->addListener($eventName, [$subscriber, $params[0]], isset($params[1]) ? $params[1] : 0);
172 } else { 172 } else {
173 foreach ($params as $listener) { 173 foreach ($params as $listener) {
174 $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0); 174 $this->addListener($eventName, [$subscriber, $listener[0]], isset($listener[1]) ? $listener[1] : 0);
175 } 175 }
176 } 176 }
177 } 177 }
178 } 178 }
179 179
181 * {@inheritdoc} 181 * {@inheritdoc}
182 */ 182 */
183 public function removeSubscriber(EventSubscriberInterface $subscriber) 183 public function removeSubscriber(EventSubscriberInterface $subscriber)
184 { 184 {
185 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { 185 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
186 if (is_array($params) && is_array($params[0])) { 186 if (\is_array($params) && \is_array($params[0])) {
187 foreach ($params as $listener) { 187 foreach ($params as $listener) {
188 $this->removeListener($eventName, array($subscriber, $listener[0])); 188 $this->removeListener($eventName, [$subscriber, $listener[0]]);
189 } 189 }
190 } else { 190 } else {
191 $this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0])); 191 $this->removeListener($eventName, [$subscriber, \is_string($params) ? $params : $params[0]]);
192 } 192 }
193 } 193 }
194 } 194 }
195 195
196 /** 196 /**
219 * @param string $eventName The name of the event 219 * @param string $eventName The name of the event
220 */ 220 */
221 private function sortListeners($eventName) 221 private function sortListeners($eventName)
222 { 222 {
223 krsort($this->listeners[$eventName]); 223 krsort($this->listeners[$eventName]);
224 $this->sorted[$eventName] = array(); 224 $this->sorted[$eventName] = [];
225 225
226 foreach ($this->listeners[$eventName] as $priority => $listeners) { 226 foreach ($this->listeners[$eventName] as $priority => $listeners) {
227 foreach ($listeners as $k => $listener) { 227 foreach ($listeners as $k => $listener) {
228 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { 228 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) {
229 $listener[0] = $listener[0](); 229 $listener[0] = $listener[0]();