annotate vendor/doctrine/common/lib/Doctrine/Common/EventManager.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The EventManager is the central point of Doctrine's event listener system.
Chris@0 24 * Listeners are registered on the manager and events are dispatched through the
Chris@0 25 * manager.
Chris@0 26 *
Chris@0 27 * @link www.doctrine-project.org
Chris@0 28 * @since 2.0
Chris@0 29 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
Chris@0 30 * @author Jonathan Wage <jonwage@gmail.com>
Chris@0 31 * @author Roman Borschel <roman@code-factory.org>
Chris@0 32 */
Chris@0 33 class EventManager
Chris@0 34 {
Chris@0 35 /**
Chris@0 36 * Map of registered listeners.
Chris@0 37 * <event> => <listeners>
Chris@0 38 *
Chris@0 39 * @var array
Chris@0 40 */
Chris@0 41 private $_listeners = [];
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Dispatches an event to all registered listeners.
Chris@0 45 *
Chris@0 46 * @param string $eventName The name of the event to dispatch. The name of the event is
Chris@0 47 * the name of the method that is invoked on listeners.
Chris@0 48 * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners.
Chris@0 49 * If not supplied, the single empty EventArgs instance is used.
Chris@0 50 *
Chris@0 51 * @return boolean
Chris@0 52 */
Chris@0 53 public function dispatchEvent($eventName, EventArgs $eventArgs = null)
Chris@0 54 {
Chris@0 55 if (isset($this->_listeners[$eventName])) {
Chris@0 56 $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
Chris@0 57
Chris@0 58 foreach ($this->_listeners[$eventName] as $listener) {
Chris@0 59 $listener->$eventName($eventArgs);
Chris@0 60 }
Chris@0 61 }
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Gets the listeners of a specific event or all listeners.
Chris@0 66 *
Chris@0 67 * @param string|null $event The name of the event.
Chris@0 68 *
Chris@0 69 * @return array The event listeners for the specified event, or all event listeners.
Chris@0 70 */
Chris@0 71 public function getListeners($event = null)
Chris@0 72 {
Chris@0 73 return $event ? $this->_listeners[$event] : $this->_listeners;
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Checks whether an event has any registered listeners.
Chris@0 78 *
Chris@0 79 * @param string $event
Chris@0 80 *
Chris@0 81 * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
Chris@0 82 */
Chris@0 83 public function hasListeners($event)
Chris@0 84 {
Chris@0 85 return isset($this->_listeners[$event]) && $this->_listeners[$event];
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Adds an event listener that listens on the specified events.
Chris@0 90 *
Chris@0 91 * @param string|array $events The event(s) to listen on.
Chris@0 92 * @param object $listener The listener object.
Chris@0 93 *
Chris@0 94 * @return void
Chris@0 95 */
Chris@0 96 public function addEventListener($events, $listener)
Chris@0 97 {
Chris@0 98 // Picks the hash code related to that listener
Chris@0 99 $hash = spl_object_hash($listener);
Chris@0 100
Chris@0 101 foreach ((array) $events as $event) {
Chris@0 102 // Overrides listener if a previous one was associated already
Chris@0 103 // Prevents duplicate listeners on same event (same instance only)
Chris@0 104 $this->_listeners[$event][$hash] = $listener;
Chris@0 105 }
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * Removes an event listener from the specified events.
Chris@0 110 *
Chris@0 111 * @param string|array $events
Chris@0 112 * @param object $listener
Chris@0 113 *
Chris@0 114 * @return void
Chris@0 115 */
Chris@0 116 public function removeEventListener($events, $listener)
Chris@0 117 {
Chris@0 118 // Picks the hash code related to that listener
Chris@0 119 $hash = spl_object_hash($listener);
Chris@0 120
Chris@0 121 foreach ((array) $events as $event) {
Chris@0 122 // Check if actually have this listener associated
Chris@0 123 if (isset($this->_listeners[$event][$hash])) {
Chris@0 124 unset($this->_listeners[$event][$hash]);
Chris@0 125 }
Chris@0 126 }
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * Adds an EventSubscriber. The subscriber is asked for all the events it is
Chris@0 131 * interested in and added as a listener for these events.
Chris@0 132 *
Chris@0 133 * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
Chris@0 134 *
Chris@0 135 * @return void
Chris@0 136 */
Chris@0 137 public function addEventSubscriber(EventSubscriber $subscriber)
Chris@0 138 {
Chris@0 139 $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * Removes an EventSubscriber. The subscriber is asked for all the events it is
Chris@0 144 * interested in and removed as a listener for these events.
Chris@0 145 *
Chris@0 146 * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
Chris@0 147 *
Chris@0 148 * @return void
Chris@0 149 */
Chris@0 150 public function removeEventSubscriber(EventSubscriber $subscriber)
Chris@0 151 {
Chris@0 152 $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
Chris@0 153 }
Chris@0 154 }