composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / EventManager.php
blob0ee04a15a46e65cbf054c92f1b07232dc8a3c96d
1 <?php
2 /*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common;
22 /**
23 * The EventManager is the central point of Doctrine's event listener system.
24 * Listeners are registered on the manager and events are dispatched through the
25 * manager.
27 * @link www.doctrine-project.org
28 * @since 2.0
29 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
30 * @author Jonathan Wage <jonwage@gmail.com>
31 * @author Roman Borschel <roman@code-factory.org>
33 class EventManager
35 /**
36 * Map of registered listeners.
37 * <event> => <listeners>
39 * @var array
41 private $_listeners = [];
43 /**
44 * Dispatches an event to all registered listeners.
46 * @param string $eventName The name of the event to dispatch. The name of the event is
47 * the name of the method that is invoked on listeners.
48 * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners.
49 * If not supplied, the single empty EventArgs instance is used.
51 * @return boolean
53 public function dispatchEvent($eventName, EventArgs $eventArgs = null)
55 if (isset($this->_listeners[$eventName])) {
56 $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
58 foreach ($this->_listeners[$eventName] as $listener) {
59 $listener->$eventName($eventArgs);
64 /**
65 * Gets the listeners of a specific event or all listeners.
67 * @param string|null $event The name of the event.
69 * @return array The event listeners for the specified event, or all event listeners.
71 public function getListeners($event = null)
73 return $event ? $this->_listeners[$event] : $this->_listeners;
76 /**
77 * Checks whether an event has any registered listeners.
79 * @param string $event
81 * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
83 public function hasListeners($event)
85 return isset($this->_listeners[$event]) && $this->_listeners[$event];
88 /**
89 * Adds an event listener that listens on the specified events.
91 * @param string|array $events The event(s) to listen on.
92 * @param object $listener The listener object.
94 * @return void
96 public function addEventListener($events, $listener)
98 // Picks the hash code related to that listener
99 $hash = spl_object_hash($listener);
101 foreach ((array) $events as $event) {
102 // Overrides listener if a previous one was associated already
103 // Prevents duplicate listeners on same event (same instance only)
104 $this->_listeners[$event][$hash] = $listener;
109 * Removes an event listener from the specified events.
111 * @param string|array $events
112 * @param object $listener
114 * @return void
116 public function removeEventListener($events, $listener)
118 // Picks the hash code related to that listener
119 $hash = spl_object_hash($listener);
121 foreach ((array) $events as $event) {
122 // Check if actually have this listener associated
123 if (isset($this->_listeners[$event][$hash])) {
124 unset($this->_listeners[$event][$hash]);
130 * Adds an EventSubscriber. The subscriber is asked for all the events it is
131 * interested in and added as a listener for these events.
133 * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
135 * @return void
137 public function addEventSubscriber(EventSubscriber $subscriber)
139 $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
143 * Removes an EventSubscriber. The subscriber is asked for all the events it is
144 * interested in and removed as a listener for these events.
146 * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
148 * @return void
150 public function removeEventSubscriber(EventSubscriber $subscriber)
152 $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);