StaticEventManager.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. require_once 'Zend/EventManager/EventManager.php';
  21. require_once 'Zend/EventManager/StaticEventCollection.php';
  22. require_once 'Zend/Stdlib/CallbackHandler.php';
  23. /**
  24. * Static version of EventManager
  25. *
  26. * @category Zend
  27. * @package Zend_EventManager
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_EventManager_StaticEventManager implements Zend_EventManager_StaticEventCollection
  32. {
  33. /**
  34. * @var Zend_EventManager_StaticEventManager
  35. */
  36. protected static $instance;
  37. /**
  38. * Identifiers with event connections
  39. * @var array
  40. */
  41. protected $identifiers = array();
  42. /**
  43. * Singleton
  44. *
  45. * @return void
  46. */
  47. protected function __construct()
  48. {
  49. }
  50. /**
  51. * Singleton
  52. *
  53. * @return void
  54. */
  55. private function __clone()
  56. {
  57. }
  58. /**
  59. * Retrieve instance
  60. *
  61. * @return Zend_EventManager_StaticEventManager
  62. */
  63. public static function getInstance()
  64. {
  65. if (null === self::$instance) {
  66. self::$instance = new self();
  67. }
  68. return self::$instance;
  69. }
  70. /**
  71. * Reset the singleton instance
  72. *
  73. * @return void
  74. */
  75. public static function resetInstance()
  76. {
  77. self::$instance = null;
  78. }
  79. /**
  80. * Attach a listener to an event
  81. *
  82. * Allows attaching a callback to an event offerred by one or more
  83. * identifying components. As an example, the following connects to the
  84. * "getAll" event of both an AbstractResource and EntityResource:
  85. *
  86. * <code>
  87. * Zend_EventManager_StaticEventManager::getInstance()->connect(
  88. * array('My_Resource_AbstractResource', 'My_Resource_EntityResource'),
  89. * 'getOne',
  90. * function ($e) use ($cache) {
  91. * if (!$id = $e->getParam('id', false)) {
  92. * return;
  93. * }
  94. * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
  95. * return;
  96. * }
  97. * return $data;
  98. * }
  99. * );
  100. * </code>
  101. *
  102. * Note: a PHP 5.3 closure is used in this example only for brevity; you
  103. * may pass any valid PHP callback as a listener.
  104. *
  105. * @param string|array $id Identifier(s) for event emitting component(s)
  106. * @param string $event
  107. * @param callback $callback PHP Callback
  108. * @param int $priority Priority at which listener should execute
  109. * @return void
  110. */
  111. public function attach($id, $event, $callback, $priority = 1)
  112. {
  113. $ids = (array) $id;
  114. foreach ($ids as $id) {
  115. if (!array_key_exists($id, $this->identifiers)) {
  116. $this->identifiers[$id] = new Zend_EventManager_EventManager();
  117. }
  118. $this->identifiers[$id]->attach($event, $callback, $priority);
  119. }
  120. }
  121. /**
  122. * Detach a listener from an event offered by a given resource
  123. *
  124. * @param string|int $id
  125. * @param Zend_Stdlib_CallbackHandler $listener
  126. * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
  127. */
  128. public function detach($id, Zend_Stdlib_CallbackHandler $listener)
  129. {
  130. if (!array_key_exists($id, $this->identifiers)) {
  131. return false;
  132. }
  133. return $this->identifiers[$id]->detach($listener);
  134. }
  135. /**
  136. * Retrieve all registered events for a given resource
  137. *
  138. * @param string|int $id
  139. * @return array
  140. */
  141. public function getEvents($id)
  142. {
  143. if (!array_key_exists($id, $this->identifiers)) {
  144. return false;
  145. }
  146. return $this->identifiers[$id]->getEvents();
  147. }
  148. /**
  149. * Retrieve all listeners for a given identifier and event
  150. *
  151. * @param string|int $id
  152. * @param string|int $event
  153. * @return false|Zend_Stdlib_PriorityQueue
  154. */
  155. public function getListeners($id, $event)
  156. {
  157. if (!array_key_exists($id, $this->identifiers)) {
  158. return false;
  159. }
  160. return $this->identifiers[$id]->getListeners($event);
  161. }
  162. /**
  163. * Clear all listeners for a given identifier, optionally for a specific event
  164. *
  165. * @param string|int $id
  166. * @param null|string $event
  167. * @return bool
  168. */
  169. public function clearListeners($id, $event = null)
  170. {
  171. if (!array_key_exists($id, $this->identifiers)) {
  172. return false;
  173. }
  174. if (null === $event) {
  175. unset($this->identifiers[$id]);
  176. return true;
  177. }
  178. return $this->identifiers[$id]->clearListeners($event);
  179. }
  180. }