Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/event-dispatcher/EventDispatcher.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
22 * @author Roman Borschel <roman@code-factory.org> | 22 * @author Roman Borschel <roman@code-factory.org> |
23 * @author Bernhard Schussek <bschussek@gmail.com> | 23 * @author Bernhard Schussek <bschussek@gmail.com> |
24 * @author Fabien Potencier <fabien@symfony.com> | 24 * @author Fabien Potencier <fabien@symfony.com> |
25 * @author Jordi Boggiano <j.boggiano@seld.be> | 25 * @author Jordi Boggiano <j.boggiano@seld.be> |
26 * @author Jordan Alliot <jordan.alliot@gmail.com> | 26 * @author Jordan Alliot <jordan.alliot@gmail.com> |
27 * @author Nicolas Grekas <p@tchwork.com> | |
27 */ | 28 */ |
28 class EventDispatcher implements EventDispatcherInterface | 29 class EventDispatcher implements EventDispatcherInterface |
29 { | 30 { |
30 private $listeners = array(); | 31 private $listeners = array(); |
31 private $sorted = array(); | 32 private $sorted = array(); |
50 * {@inheritdoc} | 51 * {@inheritdoc} |
51 */ | 52 */ |
52 public function getListeners($eventName = null) | 53 public function getListeners($eventName = null) |
53 { | 54 { |
54 if (null !== $eventName) { | 55 if (null !== $eventName) { |
55 if (!isset($this->listeners[$eventName])) { | 56 if (empty($this->listeners[$eventName])) { |
56 return array(); | 57 return array(); |
57 } | 58 } |
58 | 59 |
59 if (!isset($this->sorted[$eventName])) { | 60 if (!isset($this->sorted[$eventName])) { |
60 $this->sortListeners($eventName); | 61 $this->sortListeners($eventName); |
75 /** | 76 /** |
76 * {@inheritdoc} | 77 * {@inheritdoc} |
77 */ | 78 */ |
78 public function getListenerPriority($eventName, $listener) | 79 public function getListenerPriority($eventName, $listener) |
79 { | 80 { |
80 if (!isset($this->listeners[$eventName])) { | 81 if (empty($this->listeners[$eventName])) { |
81 return; | 82 return; |
82 } | 83 } |
83 | 84 |
85 if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { | |
86 $listener[0] = $listener[0](); | |
87 } | |
88 | |
84 foreach ($this->listeners[$eventName] as $priority => $listeners) { | 89 foreach ($this->listeners[$eventName] as $priority => $listeners) { |
85 if (false !== in_array($listener, $listeners, true)) { | 90 foreach ($listeners as $k => $v) { |
86 return $priority; | 91 if ($v !== $listener && is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) { |
92 $v[0] = $v[0](); | |
93 $this->listeners[$eventName][$priority][$k] = $v; | |
94 } | |
95 if ($v === $listener) { | |
96 return $priority; | |
97 } | |
87 } | 98 } |
88 } | 99 } |
89 } | 100 } |
90 | 101 |
91 /** | 102 /** |
92 * {@inheritdoc} | 103 * {@inheritdoc} |
93 */ | 104 */ |
94 public function hasListeners($eventName = null) | 105 public function hasListeners($eventName = null) |
95 { | 106 { |
96 return (bool) $this->getListeners($eventName); | 107 if (null !== $eventName) { |
108 return !empty($this->listeners[$eventName]); | |
109 } | |
110 | |
111 foreach ($this->listeners as $eventListeners) { | |
112 if ($eventListeners) { | |
113 return true; | |
114 } | |
115 } | |
116 | |
117 return false; | |
97 } | 118 } |
98 | 119 |
99 /** | 120 /** |
100 * {@inheritdoc} | 121 * {@inheritdoc} |
101 */ | 122 */ |
108 /** | 129 /** |
109 * {@inheritdoc} | 130 * {@inheritdoc} |
110 */ | 131 */ |
111 public function removeListener($eventName, $listener) | 132 public function removeListener($eventName, $listener) |
112 { | 133 { |
113 if (!isset($this->listeners[$eventName])) { | 134 if (empty($this->listeners[$eventName])) { |
114 return; | 135 return; |
115 } | 136 } |
116 | 137 |
138 if (is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { | |
139 $listener[0] = $listener[0](); | |
140 } | |
141 | |
117 foreach ($this->listeners[$eventName] as $priority => $listeners) { | 142 foreach ($this->listeners[$eventName] as $priority => $listeners) { |
118 if (false !== ($key = array_search($listener, $listeners, true))) { | 143 foreach ($listeners as $k => $v) { |
119 unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]); | 144 if ($v !== $listener && is_array($v) && isset($v[0]) && $v[0] instanceof \Closure) { |
145 $v[0] = $v[0](); | |
146 } | |
147 if ($v === $listener) { | |
148 unset($listeners[$k], $this->sorted[$eventName]); | |
149 } else { | |
150 $listeners[$k] = $v; | |
151 } | |
152 } | |
153 | |
154 if ($listeners) { | |
155 $this->listeners[$eventName][$priority] = $listeners; | |
156 } else { | |
157 unset($this->listeners[$eventName][$priority]); | |
120 } | 158 } |
121 } | 159 } |
122 } | 160 } |
123 | 161 |
124 /** | 162 /** |
169 { | 207 { |
170 foreach ($listeners as $listener) { | 208 foreach ($listeners as $listener) { |
171 if ($event->isPropagationStopped()) { | 209 if ($event->isPropagationStopped()) { |
172 break; | 210 break; |
173 } | 211 } |
174 call_user_func($listener, $event, $eventName, $this); | 212 \call_user_func($listener, $event, $eventName, $this); |
175 } | 213 } |
176 } | 214 } |
177 | 215 |
178 /** | 216 /** |
179 * Sorts the internal list of listeners for the given event by priority. | 217 * Sorts the internal list of listeners for the given event by priority. |
181 * @param string $eventName The name of the event | 219 * @param string $eventName The name of the event |
182 */ | 220 */ |
183 private function sortListeners($eventName) | 221 private function sortListeners($eventName) |
184 { | 222 { |
185 krsort($this->listeners[$eventName]); | 223 krsort($this->listeners[$eventName]); |
186 $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); | 224 $this->sorted[$eventName] = array(); |
225 | |
226 foreach ($this->listeners[$eventName] as $priority => $listeners) { | |
227 foreach ($listeners as $k => $listener) { | |
228 if (\is_array($listener) && isset($listener[0]) && $listener[0] instanceof \Closure) { | |
229 $listener[0] = $listener[0](); | |
230 $this->listeners[$eventName][$priority][$k] = $listener; | |
231 } | |
232 $this->sorted[$eventName][] = $listener; | |
233 } | |
234 } | |
187 } | 235 } |
188 } | 236 } |