web/lib/Zend/EventManager/SharedEventManager.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_EventManager
       
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  */
       
    20 
       
    21 require_once 'Zend/EventManager/EventManager.php';
       
    22 require_once 'Zend/EventManager/SharedEventCollection.php';
       
    23 
       
    24 /**
       
    25  * Shared/contextual EventManager
       
    26  *
       
    27  * Allows attaching to EMs composed by other classes without having an instance first.
       
    28  * The assumption is that the SharedEventManager will be injected into EventManager 
       
    29  * instances, and then queried for additional listeners when triggering an event.
       
    30  *
       
    31  * @category   Zend
       
    32  * @package    Zend_EventManager
       
    33  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    35  */
       
    36 class Zend_EventManager_SharedEventManager implements Zend_EventManager_SharedEventCollection
       
    37 {
       
    38     /**
       
    39      * Identifiers with event connections
       
    40      * @var array
       
    41      */
       
    42     protected $identifiers = array();
       
    43 
       
    44     /**
       
    45      * Attach a listener to an event
       
    46      *
       
    47      * Allows attaching a callback to an event offerred by one or more 
       
    48      * identifying components. As an example, the following connects to the 
       
    49      * "getAll" event of both an AbstractResource and EntityResource:
       
    50      *
       
    51      * <code>
       
    52      * SharedEventManager::getInstance()->connect(
       
    53      *     array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
       
    54      *     'getOne',
       
    55      *     function ($e) use ($cache) {
       
    56      *         if (!$id = $e->getParam('id', false)) {
       
    57      *             return;
       
    58      *         }
       
    59      *         if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
       
    60      *             return;
       
    61      *         }
       
    62      *         return $data;
       
    63      *     }
       
    64      * );
       
    65      * </code>
       
    66      * 
       
    67      * @param  string|array $id Identifier(s) for event emitting component(s)
       
    68      * @param  string $event 
       
    69      * @param  callback $callback PHP Callback
       
    70      * @param  int $priority Priority at which listener should execute
       
    71      * @return void
       
    72      */
       
    73     public function attach($id, $event, $callback, $priority = 1)
       
    74     {
       
    75         $ids = (array) $id;
       
    76         foreach ($ids as $id) {
       
    77             if (!array_key_exists($id, $this->identifiers)) {
       
    78                 $this->identifiers[$id] = new Zend_EventManager_EventManager();
       
    79             }
       
    80             $this->identifiers[$id]->attach($event, $callback, $priority);
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * Detach a listener from an event offered by a given resource
       
    86      * 
       
    87      * @param  string|int $id
       
    88      * @param  Zend_Stdlib_CallbackHandler $listener 
       
    89      * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
       
    90      */
       
    91     public function detach($id, Zend_Stdlib_CallbackHandler $listener)
       
    92     {
       
    93         if (!array_key_exists($id, $this->identifiers)) {
       
    94             return false;
       
    95         }
       
    96         return $this->identifiers[$id]->detach($listener);
       
    97     }
       
    98 
       
    99     /**
       
   100      * Retrieve all registered events for a given resource
       
   101      * 
       
   102      * @param  string|int $id
       
   103      * @return array
       
   104      */
       
   105     public function getEvents($id)
       
   106     {
       
   107         if (!array_key_exists($id, $this->identifiers)) {
       
   108             return false;
       
   109         }
       
   110         return $this->identifiers[$id]->getEvents();
       
   111     }
       
   112 
       
   113     /**
       
   114      * Retrieve all listeners for a given identifier and event
       
   115      * 
       
   116      * @param  string|int $id
       
   117      * @param  string|int $event 
       
   118      * @return false|Zend_Stdlib_PriorityQueue
       
   119      */
       
   120     public function getListeners($id, $event)
       
   121     {
       
   122         if (!array_key_exists($id, $this->identifiers)) {
       
   123             return false;
       
   124         }
       
   125         return $this->identifiers[$id]->getListeners($event);
       
   126     }
       
   127 
       
   128     /**
       
   129      * Clear all listeners for a given identifier, optionally for a specific event
       
   130      * 
       
   131      * @param  string|int $id 
       
   132      * @param  null|string $event 
       
   133      * @return bool
       
   134      */
       
   135     public function clearListeners($id, $event = null)
       
   136     {
       
   137         if (!array_key_exists($id, $this->identifiers)) {
       
   138             return false;
       
   139         }
       
   140 
       
   141         if (null === $event) {
       
   142             unset($this->identifiers[$id]);
       
   143             return true;
       
   144         }
       
   145 
       
   146         return $this->identifiers[$id]->clearListeners($event);
       
   147     }
       
   148 }