PluginProbe
ManageWP Worker / 4.9.35
ManageWP Worker v4.9.35
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / Symfony / EventDispatcher / EventDispatcher.php

EventDispatcher.php in ManageWP Worker 4.9.35, at src/Symfony/EventDispatcher/EventDispatcher.php

182 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 /**
13 * The EventDispatcherInterface is the central point of Symfony's event listener system.
14 *
15 * Listeners are registered on the manager and events are dispatched through the
16 * manager.
17 *
18 * @author Guilherme Blanco <[email protected]>
19 * @author Jonathan Wage <[email protected]>
20 * @author Roman Borschel <[email protected]>
21 * @author Bernhard Schussek <[email protected]>
22 * @author Fabien Potencier <[email protected]>
23 * @author Jordi Boggiano <[email protected]>
24 * @author Jordan Alliot <[email protected]>
25 *
26 * @api
27 */
28 class Symfony_EventDispatcher_EventDispatcher implements Symfony_EventDispatcher_EventDispatcherInterface
29 {
30
31 private $listeners = array();
32
33 private $sorted = array();
34
35 /**
36 * @see EventDispatcherInterface::dispatch
37 *
38 * @api
39 *
40 * Note: Type hint removed from $event parameter to fix PHP 8.4+ deprecation warning
41 * about implicitly nullable parameters while maintaining backward compatibility with PHP 5.5+.
42 * The nullable type syntax (?Symfony_EventDispatcher_Event) is not supported in PHP 5.5-7.0.
43 */
44 public function dispatch($eventName, $event = null)
45 {
46 if (null === $event) {
47 $event = new Symfony_EventDispatcher_Event();
48 }
49 if (!isset($this->listeners[$eventName])) {
50 return $event;
51 }
52 $this->doDispatch($this->getListeners($eventName), $eventName, $event);
53
54 return $event;
55 }
56
57 /**
58 * @see EventDispatcherInterface::getListeners
59 */
60 public function getListeners($eventName = null)
61 {
62 if (null !== $eventName) {
63 if (!isset($this->sorted[$eventName])) {
64 $this->sortListeners($eventName);
65 }
66
67 return $this->sorted[$eventName];
68 }
69 foreach (array_keys($this->listeners) as $eventName) {
70 if (!isset($this->sorted[$eventName])) {
71 $this->sortListeners($eventName);
72 }
73 }
74
75 return array_filter($this->sorted);
76 }
77
78 /**
79 * @see EventDispatcherInterface::hasListeners
80 */
81 public function hasListeners($eventName = null)
82 {
83 return (bool) count($this->getListeners($eventName));
84 }
85
86 /**
87 * @see EventDispatcherInterface::addListener
88 *
89 * @api
90 */
91 public function addListener($eventName, $listener, $priority = 0)
92 {
93 $this->listeners[$eventName][$priority][] = $listener;
94 unset($this->sorted[$eventName]);
95 }
96
97 /**
98 * @see EventDispatcherInterface::removeListener
99 */
100 public function removeListener($eventName, $listener)
101 {
102 if (!isset($this->listeners[$eventName])) {
103 return;
104 }
105 foreach ($this->listeners[$eventName] as $priority => $listeners) {
106 if (false !== ($key = array_search($listener, $listeners, true))) {
107 unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]);
108 }
109 }
110 }
111
112 /**
113 * @see Symfony_EventDispatcher_EventSubscriberInterface::addSubscriber
114 *
115 * @api
116 */
117 public function addSubscriber(Symfony_EventDispatcher_EventSubscriberInterface $subscriber)
118 {
119 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
120 if (is_string($params)) {
121 $this->addListener($eventName, array($subscriber, $params));
122 } elseif (is_string($params[0])) {
123 $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
124 } else {
125 foreach ($params as $listener) {
126 $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
127 }
128 }
129 }
130 }
131
132 /**
133 * @see Symfony_EventDispatcher_EventSubscriberInterface::removeSubscriber
134 */
135 public function removeSubscriber(Symfony_EventDispatcher_EventSubscriberInterface $subscriber)
136 {
137 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
138 if (is_array($params) && is_array($params[0])) {
139 foreach ($params as $listener) {
140 $this->removeListener($eventName, array($subscriber, $listener[0]));
141 }
142 } else {
143 $this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
144 }
145 }
146 }
147
148 /**
149 * Triggers the listeners of an event.
150 *
151 * This method can be overridden to add functionality that is executed
152 * for each listener.
153 *
154 * @param callable[] $listeners The event listeners.
155 * @param string $eventName The name of the event to dispatch.
156 * @param Symfony_EventDispatcher_Event $event The event object to pass to the event handlers/listeners.
157 */
158 protected function doDispatch($listeners, $eventName, Symfony_EventDispatcher_Event $event)
159 {
160 foreach ($listeners as $listener) {
161 call_user_func($listener, $event, $eventName, $this);
162 if ($event->isPropagationStopped()) {
163 break;
164 }
165 }
166 }
167
168 /**
169 * Sorts the internal list of listeners for the given event by priority.
170 *
171 * @param string $eventName The name of the event.
172 */
173 private function sortListeners($eventName)
174 {
175 $this->sorted[$eventName] = array();
176 if (isset($this->listeners[$eventName])) {
177 krsort($this->listeners[$eventName]);
178 $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
179 }
180 }
181 }
182