SharedEventManager.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-2015 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/SharedEventCollection.php';
  22. /**
  23. * Shared/contextual EventManager
  24. *
  25. * Allows attaching to EMs composed by other classes without having an instance first.
  26. * The assumption is that the SharedEventManager will be injected into EventManager
  27. * instances, and then queried for additional listeners when triggering an event.
  28. *
  29. * @category Zend
  30. * @package Zend_EventManager
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_EventManager_SharedEventManager implements Zend_EventManager_SharedEventCollection
  35. {
  36. /**
  37. * Identifiers with event connections
  38. * @var array
  39. */
  40. protected $identifiers = array();
  41. /**
  42. * Attach a listener to an event
  43. *
  44. * Allows attaching a callback to an event offerred by one or more
  45. * identifying components. As an example, the following connects to the
  46. * "getAll" event of both an AbstractResource and EntityResource:
  47. *
  48. * <code>
  49. * SharedEventManager::getInstance()->connect(
  50. * array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
  51. * 'getOne',
  52. * function ($e) use ($cache) {
  53. * if (!$id = $e->getParam('id', false)) {
  54. * return;
  55. * }
  56. * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
  57. * return;
  58. * }
  59. * return $data;
  60. * }
  61. * );
  62. * </code>
  63. *
  64. * @param string|array $id Identifier(s) for event emitting component(s)
  65. * @param string $event
  66. * @param callback $callback PHP Callback
  67. * @param int $priority Priority at which listener should execute
  68. * @return void
  69. */
  70. public function attach($id, $event, $callback, $priority = 1)
  71. {
  72. $ids = (array) $id;
  73. foreach ($ids as $id) {
  74. if (!array_key_exists($id, $this->identifiers)) {
  75. $this->identifiers[$id] = new Zend_EventManager_EventManager();
  76. }
  77. $this->identifiers[$id]->attach($event, $callback, $priority);
  78. }
  79. }
  80. /**
  81. * Detach a listener from an event offered by a given resource
  82. *
  83. * @param string|int $id
  84. * @param Zend_Stdlib_CallbackHandler $listener
  85. * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
  86. */
  87. public function detach($id, Zend_Stdlib_CallbackHandler $listener)
  88. {
  89. if (!array_key_exists($id, $this->identifiers)) {
  90. return false;
  91. }
  92. return $this->identifiers[$id]->detach($listener);
  93. }
  94. /**
  95. * Retrieve all registered events for a given resource
  96. *
  97. * @param string|int $id
  98. * @return array
  99. */
  100. public function getEvents($id)
  101. {
  102. if (!array_key_exists($id, $this->identifiers)) {
  103. return false;
  104. }
  105. return $this->identifiers[$id]->getEvents();
  106. }
  107. /**
  108. * Retrieve all listeners for a given identifier and event
  109. *
  110. * @param string|int $id
  111. * @param string|int $event
  112. * @return false|Zend_Stdlib_PriorityQueue
  113. */
  114. public function getListeners($id, $event)
  115. {
  116. if (!array_key_exists($id, $this->identifiers)) {
  117. return false;
  118. }
  119. return $this->identifiers[$id]->getListeners($event);
  120. }
  121. /**
  122. * Clear all listeners for a given identifier, optionally for a specific event
  123. *
  124. * @param string|int $id
  125. * @param null|string $event
  126. * @return bool
  127. */
  128. public function clearListeners($id, $event = null)
  129. {
  130. if (!array_key_exists($id, $this->identifiers)) {
  131. return false;
  132. }
  133. if (null === $event) {
  134. unset($this->identifiers[$id]);
  135. return true;
  136. }
  137. return $this->identifiers[$id]->clearListeners($event);
  138. }
  139. }