vendor/doctrine-common/lib/Doctrine/Common/EventManager.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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 LGPL. For more information, see
       
    19  * <http://www.doctrine-project.org>.
       
    20  */
       
    21 
       
    22 namespace Doctrine\Common;
       
    23 
       
    24 use Doctrine\Common\Events\Event;
       
    25 
       
    26 /**
       
    27  * The EventManager is the central point of Doctrine's event listener system.
       
    28  * Listeners are registered on the manager and events are dispatched through the
       
    29  * manager.
       
    30  * 
       
    31  * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
       
    32  * @link    www.doctrine-project.org
       
    33  * @since   2.0
       
    34  * @version $Revision: 3938 $
       
    35  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
       
    36  * @author  Jonathan Wage <jonwage@gmail.com>
       
    37  * @author  Roman Borschel <roman@code-factory.org>
       
    38  */
       
    39 class EventManager
       
    40 {
       
    41     /**
       
    42      * Map of registered listeners.
       
    43      * <event> => <listeners> 
       
    44      *
       
    45      * @var array
       
    46      */
       
    47     private $_listeners = array();
       
    48 
       
    49     /**
       
    50      * Dispatches an event to all registered listeners.
       
    51      *
       
    52      * @param string $eventName The name of the event to dispatch. The name of the event is
       
    53      *                          the name of the method that is invoked on listeners.
       
    54      * @param EventArgs $eventArgs The event arguments to pass to the event handlers/listeners.
       
    55      *                             If not supplied, the single empty EventArgs instance is used.
       
    56      * @return boolean
       
    57      */
       
    58     public function dispatchEvent($eventName, EventArgs $eventArgs = null)
       
    59     {
       
    60         if (isset($this->_listeners[$eventName])) {
       
    61             $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
       
    62             
       
    63             foreach ($this->_listeners[$eventName] as $listener) {
       
    64                 $listener->$eventName($eventArgs);
       
    65             }
       
    66         }
       
    67     }
       
    68 
       
    69     /**
       
    70      * Gets the listeners of a specific event or all listeners.
       
    71      *
       
    72      * @param string $event The name of the event.
       
    73      * @return array The event listeners for the specified event, or all event listeners.
       
    74      */
       
    75     public function getListeners($event = null)
       
    76     {
       
    77         return $event ? $this->_listeners[$event] : $this->_listeners;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Checks whether an event has any registered listeners.
       
    82      *
       
    83      * @param string $event
       
    84      * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
       
    85      */
       
    86     public function hasListeners($event)
       
    87     {
       
    88         return isset($this->_listeners[$event]) && $this->_listeners[$event];
       
    89     }
       
    90 
       
    91     /**
       
    92      * Adds an event listener that listens on the specified events.
       
    93      *
       
    94      * @param string|array $events The event(s) to listen on.
       
    95      * @param object $listener The listener object.
       
    96      */
       
    97     public function addEventListener($events, $listener)
       
    98     {
       
    99         // Picks the hash code related to that listener
       
   100         $hash = spl_object_hash($listener);
       
   101         
       
   102         foreach ((array) $events as $event) {
       
   103             // Overrides listener if a previous one was associated already
       
   104             // Prevents duplicate listeners on same event (same instance only)
       
   105             $this->_listeners[$event][$hash] = $listener;
       
   106         }
       
   107     }
       
   108     
       
   109     /**
       
   110      * Removes an event listener from the specified events.
       
   111      *
       
   112      * @param string|array $events
       
   113      * @param object $listener
       
   114      */
       
   115     public function removeEventListener($events, $listener)
       
   116     {
       
   117         // Picks the hash code related to that listener
       
   118         $hash = spl_object_hash($listener);
       
   119         
       
   120         foreach ((array) $events as $event) {
       
   121             // Check if actually have this listener associated
       
   122             if (isset($this->_listeners[$event][$hash])) {
       
   123                 unset($this->_listeners[$event][$hash]);
       
   124             }
       
   125         }
       
   126     }
       
   127     
       
   128     /**
       
   129      * Adds an EventSubscriber. The subscriber is asked for all the events he is
       
   130      * interested in and added as a listener for these events.
       
   131      * 
       
   132      * @param Doctrine\Common\EventSubscriber $subscriber The subscriber.
       
   133      */
       
   134     public function addEventSubscriber(EventSubscriber $subscriber)
       
   135     {
       
   136         $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
       
   137     }
       
   138 }