File indexing completed on 2025-02-02 05:49:01
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_EventManager 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 */ 0020 0021 // require_once 'Zend/EventManager/EventManager.php'; 0022 // require_once 'Zend/EventManager/SharedEventCollection.php'; 0023 0024 /** 0025 * Shared/contextual EventManager 0026 * 0027 * Allows attaching to EMs composed by other classes without having an instance first. 0028 * The assumption is that the SharedEventManager will be injected into EventManager 0029 * instances, and then queried for additional listeners when triggering an event. 0030 * 0031 * @category Zend 0032 * @package Zend_EventManager 0033 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0034 * @license http://framework.zend.com/license/new-bsd New BSD License 0035 */ 0036 class Zend_EventManager_SharedEventManager implements Zend_EventManager_SharedEventCollection 0037 { 0038 /** 0039 * Identifiers with event connections 0040 * @var array 0041 */ 0042 protected $identifiers = array(); 0043 0044 /** 0045 * Attach a listener to an event 0046 * 0047 * Allows attaching a callback to an event offerred by one or more 0048 * identifying components. As an example, the following connects to the 0049 * "getAll" event of both an AbstractResource and EntityResource: 0050 * 0051 * <code> 0052 * SharedEventManager::getInstance()->connect( 0053 * array('My\Resource\AbstractResource', 'My\Resource\EntityResource'), 0054 * 'getOne', 0055 * function ($e) use ($cache) { 0056 * if (!$id = $e->getParam('id', false)) { 0057 * return; 0058 * } 0059 * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) { 0060 * return; 0061 * } 0062 * return $data; 0063 * } 0064 * ); 0065 * </code> 0066 * 0067 * @param string|array $id Identifier(s) for event emitting component(s) 0068 * @param string $event 0069 * @param callback $callback PHP Callback 0070 * @param int $priority Priority at which listener should execute 0071 * @return void 0072 */ 0073 public function attach($id, $event, $callback, $priority = 1) 0074 { 0075 $ids = (array) $id; 0076 foreach ($ids as $id) { 0077 if (!array_key_exists($id, $this->identifiers)) { 0078 $this->identifiers[$id] = new Zend_EventManager_EventManager(); 0079 } 0080 $this->identifiers[$id]->attach($event, $callback, $priority); 0081 } 0082 } 0083 0084 /** 0085 * Detach a listener from an event offered by a given resource 0086 * 0087 * @param string|int $id 0088 * @param Zend_Stdlib_CallbackHandler $listener 0089 * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found 0090 */ 0091 public function detach($id, Zend_Stdlib_CallbackHandler $listener) 0092 { 0093 if (!array_key_exists($id, $this->identifiers)) { 0094 return false; 0095 } 0096 return $this->identifiers[$id]->detach($listener); 0097 } 0098 0099 /** 0100 * Retrieve all registered events for a given resource 0101 * 0102 * @param string|int $id 0103 * @return array 0104 */ 0105 public function getEvents($id) 0106 { 0107 if (!array_key_exists($id, $this->identifiers)) { 0108 return false; 0109 } 0110 return $this->identifiers[$id]->getEvents(); 0111 } 0112 0113 /** 0114 * Retrieve all listeners for a given identifier and event 0115 * 0116 * @param string|int $id 0117 * @param string|int $event 0118 * @return false|Zend_Stdlib_PriorityQueue 0119 */ 0120 public function getListeners($id, $event) 0121 { 0122 if (!array_key_exists($id, $this->identifiers)) { 0123 return false; 0124 } 0125 return $this->identifiers[$id]->getListeners($event); 0126 } 0127 0128 /** 0129 * Clear all listeners for a given identifier, optionally for a specific event 0130 * 0131 * @param string|int $id 0132 * @param null|string $event 0133 * @return bool 0134 */ 0135 public function clearListeners($id, $event = null) 0136 { 0137 if (!array_key_exists($id, $this->identifiers)) { 0138 return false; 0139 } 0140 0141 if (null === $event) { 0142 unset($this->identifiers[$id]); 0143 return true; 0144 } 0145 0146 return $this->identifiers[$id]->clearListeners($event); 0147 } 0148 }