Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / common / lib / Doctrine / Common / EventManager.php
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.
14  *
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>.
18  */
19
20 namespace Doctrine\Common;
21
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.
26  *
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>
32  */
33 class EventManager
34 {
35     /**
36      * Map of registered listeners.
37      * <event> => <listeners>
38      *
39      * @var array
40      */
41     private $_listeners = [];
42
43     /**
44      * Dispatches an event to all registered listeners.
45      *
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.
50      *
51      * @return void
52      */
53     public function dispatchEvent($eventName, EventArgs $eventArgs = null)
54     {
55         if (isset($this->_listeners[$eventName])) {
56             $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
57
58             foreach ($this->_listeners[$eventName] as $listener) {
59                 $listener->$eventName($eventArgs);
60             }
61         }
62     }
63
64     /**
65      * Gets the listeners of a specific event or all listeners.
66      *
67      * @param string|null $event The name of the event.
68      *
69      * @return array The event listeners for the specified event, or all event listeners.
70      */
71     public function getListeners($event = null)
72     {
73         return $event ? $this->_listeners[$event] : $this->_listeners;
74     }
75
76     /**
77      * Checks whether an event has any registered listeners.
78      *
79      * @param string $event
80      *
81      * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
82      */
83     public function hasListeners($event)
84     {
85         return !empty($this->_listeners[$event]);
86     }
87
88     /**
89      * Adds an event listener that listens on the specified events.
90      *
91      * @param string|array $events   The event(s) to listen on.
92      * @param object       $listener The listener object.
93      *
94      * @return void
95      */
96     public function addEventListener($events, $listener)
97     {
98         // Picks the hash code related to that listener
99         $hash = spl_object_hash($listener);
100
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;
105         }
106     }
107
108     /**
109      * Removes an event listener from the specified events.
110      *
111      * @param string|array $events
112      * @param object       $listener
113      *
114      * @return void
115      */
116     public function removeEventListener($events, $listener)
117     {
118         // Picks the hash code related to that listener
119         $hash = spl_object_hash($listener);
120
121         foreach ((array) $events as $event) {
122             unset($this->_listeners[$event][$hash]);
123         }
124     }
125
126     /**
127      * Adds an EventSubscriber. The subscriber is asked for all the events it is
128      * interested in and added as a listener for these events.
129      *
130      * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
131      *
132      * @return void
133      */
134     public function addEventSubscriber(EventSubscriber $subscriber)
135     {
136         $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
137     }
138
139     /**
140      * Removes an EventSubscriber. The subscriber is asked for all the events it is
141      * interested in and removed as a listener for these events.
142      *
143      * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
144      *
145      * @return void
146      */
147     public function removeEventSubscriber(EventSubscriber $subscriber)
148     {
149         $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
150     }
151 }