Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / EventManager.php
1 <?php
2 /*
3  *  $Id$
4  *
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.
16  *
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>.
20  */
21
22 namespace Doctrine\Common;
23
24 /**
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
27  * manager.
28  *
29  * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30  * @link    www.doctrine-project.org
31  * @since   2.0
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>
36  */
37 class EventManager
38 {
39     /**
40      * Map of registered listeners.
41      * <event> => <listeners>
42      *
43      * @var array
44      */
45     private $_listeners = array();
46
47     /**
48      * Dispatches an event to all registered listeners.
49      *
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.
54      * @return boolean
55      */
56     public function dispatchEvent($eventName, EventArgs $eventArgs = null)
57     {
58         if (isset($this->_listeners[$eventName])) {
59             $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
60
61             foreach ($this->_listeners[$eventName] as $listener) {
62                 $listener->$eventName($eventArgs);
63             }
64         }
65     }
66
67     /**
68      * Gets the listeners of a specific event or all listeners.
69      *
70      * @param string $event The name of the event.
71      * @return array The event listeners for the specified event, or all event listeners.
72      */
73     public function getListeners($event = null)
74     {
75         return $event ? $this->_listeners[$event] : $this->_listeners;
76     }
77
78     /**
79      * Checks whether an event has any registered listeners.
80      *
81      * @param string $event
82      * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
83      */
84     public function hasListeners($event)
85     {
86         return isset($this->_listeners[$event]) && $this->_listeners[$event];
87     }
88
89     /**
90      * Adds an event listener that listens on the specified events.
91      *
92      * @param string|array $events The event(s) to listen on.
93      * @param object $listener The listener object.
94      */
95     public function addEventListener($events, $listener)
96     {
97         // Picks the hash code related to that listener
98         $hash = spl_object_hash($listener);
99
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;
104         }
105     }
106
107     /**
108      * Removes an event listener from the specified events.
109      *
110      * @param string|array $events
111      * @param object $listener
112      */
113     public function removeEventListener($events, $listener)
114     {
115         // Picks the hash code related to that listener
116         $hash = spl_object_hash($listener);
117
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]);
122             }
123         }
124     }
125
126     /**
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.
129      *
130      * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
131      */
132     public function addEventSubscriber(EventSubscriber $subscriber)
133     {
134         $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
135     }
136         
137     /**
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.
140      *
141      * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber.
142      */
143     public function removeEventSubscriber(EventSubscriber $subscriber)
144     {
145         $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
146     }   
147 }