5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the MIT license. For more information, see
19 * <http://www.doctrine-project.org>.
22 namespace Doctrine\Common;
25 * The EventManager is the central point of Doctrine's event listener system.
26 * Listeners are registered on the manager and events are dispatched through the
29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 * @link www.doctrine-project.org
32 * @version $Revision: 3938 $
33 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
34 * @author Jonathan Wage <jonwage@gmail.com>
35 * @author Roman Borschel <roman@code-factory.org>
40 * Map of registered listeners.
41 * <event> => <listeners>
45 private $_listeners = array();
48 * Dispatches an event to all registered listeners.
50 * @param string $eventName The name of the event to dispatch. The name of the event is
51 * the name of the method that is invoked on listeners.
52 * @param EventArgs $eventArgs The event arguments to pass to the event handlers/listeners.
53 * If not supplied, the single empty EventArgs instance is used.
56 public function dispatchEvent($eventName, EventArgs $eventArgs = null)
58 if (isset($this->_listeners[$eventName])) {
59 $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
61 foreach ($this->_listeners[$eventName] as $listener) {
62 $listener->$eventName($eventArgs);
68 * Gets the listeners of a specific event or all listeners.
70 * @param string $event The name of the event.
71 * @return array The event listeners for the specified event, or all event listeners.
73 public function getListeners($event = null)
75 return $event ? $this->_listeners[$event] : $this->_listeners;
79 * Checks whether an event has any registered listeners.
81 * @param string $event
82 * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
84 public function hasListeners($event)
86 return isset($this->_listeners[$event]) && $this->_listeners[$event];
90 * Adds an event listener that listens on the specified events.
92 * @param string|array $events The event(s) to listen on.
93 * @param object $listener The listener object.
95 public function addEventListener($events, $listener)
97 // Picks the hash code related to that listener
98 $hash = spl_object_hash($listener);
100 foreach ((array) $events as $event) {
101 // Overrides listener if a previous one was associated already
102 // Prevents duplicate listeners on same event (same instance only)
103 $this->_listeners[$event][$hash] = $listener;
108 * Removes an event listener from the specified events.
110 * @param string|array $events
111 * @param object $listener
113 public function removeEventListener($events, $listener)
115 // Picks the hash code related to that listener
116 $hash = spl_object_hash($listener);
118 foreach ((array) $events as $event) {
119 // Check if actually have this listener associated
120 if (isset($this->_listeners[$event][$hash])) {
121 unset($this->_listeners[$event][$hash]);
127 * Adds an EventSubscriber. The subscriber is asked for all the events he is
128 * interested in and added as a listener for these events.
130 * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
132 public function addEventSubscriber(EventSubscriber $subscriber)
134 $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
138 * Removes an EventSubscriber. The subscriber is asked for all the events it is
139 * interested in and removed as a listener for these events.
141 * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
143 public function removeEventSubscriber(EventSubscriber $subscriber)
145 $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);