SharedEventManager.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\EventManager;
  10. use Zend\Stdlib\CallbackHandler;
  11. use Zend\Stdlib\PriorityQueue;
  12. /**
  13. * Shared/contextual EventManager
  14. *
  15. * Allows attaching to EMs composed by other classes without having an instance first.
  16. * The assumption is that the SharedEventManager will be injected into EventManager
  17. * instances, and then queried for additional listeners when triggering an event.
  18. */
  19. class SharedEventManager implements
  20. SharedEventAggregateAwareInterface,
  21. SharedEventManagerInterface
  22. {
  23. /**
  24. * Identifiers with event connections
  25. * @var array
  26. */
  27. protected $identifiers = [];
  28. /**
  29. * Attach a listener to an event
  30. *
  31. * Allows attaching a callback to an event offered by one or more
  32. * identifying components. As an example, the following connects to the
  33. * "getAll" event of both an AbstractResource and EntityResource:
  34. *
  35. * <code>
  36. * $sharedEventManager = new SharedEventManager();
  37. * $sharedEventManager->attach(
  38. * array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
  39. * 'getAll',
  40. * function ($e) use ($cache) {
  41. * if (!$id = $e->getParam('id', false)) {
  42. * return;
  43. * }
  44. * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
  45. * return;
  46. * }
  47. * return $data;
  48. * }
  49. * );
  50. * </code>
  51. *
  52. * @param string|array $id Identifier(s) for event emitting component(s)
  53. * @param string $event
  54. * @param callable $callback PHP Callback
  55. * @param int $priority Priority at which listener should execute
  56. * @return CallbackHandler|array Either CallbackHandler or array of CallbackHandlers
  57. */
  58. public function attach($id, $event, $callback, $priority = 1)
  59. {
  60. $ids = (array) $id;
  61. $listeners = [];
  62. foreach ($ids as $id) {
  63. if (!array_key_exists($id, $this->identifiers)) {
  64. $this->identifiers[$id] = new EventManager($id);
  65. }
  66. $listeners[] = $this->identifiers[$id]->attach($event, $callback, $priority);
  67. }
  68. if (count($listeners) > 1) {
  69. return $listeners;
  70. }
  71. return $listeners[0];
  72. }
  73. /**
  74. * Attach a listener aggregate
  75. *
  76. * Listener aggregates accept an EventManagerInterface instance, and call attachShared()
  77. * one or more times, typically to attach to multiple events using local
  78. * methods.
  79. *
  80. * @param SharedListenerAggregateInterface $aggregate
  81. * @param int $priority If provided, a suggested priority for the aggregate to use
  82. * @return mixed return value of {@link ListenerAggregateInterface::attachShared()}
  83. */
  84. public function attachAggregate(SharedListenerAggregateInterface $aggregate, $priority = 1)
  85. {
  86. return $aggregate->attachShared($this, $priority);
  87. }
  88. /**
  89. * Detach a listener from an event offered by a given resource
  90. *
  91. * @param string|int $id
  92. * @param CallbackHandler $listener
  93. * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
  94. */
  95. public function detach($id, CallbackHandler $listener)
  96. {
  97. if (!array_key_exists($id, $this->identifiers)) {
  98. return false;
  99. }
  100. return $this->identifiers[$id]->detach($listener);
  101. }
  102. /**
  103. * Detach a listener aggregate
  104. *
  105. * Listener aggregates accept a SharedEventManagerInterface instance, and call detachShared()
  106. * of all previously attached listeners.
  107. *
  108. * @param SharedListenerAggregateInterface $aggregate
  109. * @return mixed return value of {@link SharedListenerAggregateInterface::detachShared()}
  110. */
  111. public function detachAggregate(SharedListenerAggregateInterface $aggregate)
  112. {
  113. return $aggregate->detachShared($this);
  114. }
  115. /**
  116. * Retrieve all registered events for a given resource
  117. *
  118. * @deprecated This method is deprecated with 2.6.0, and will be removed in 3.0.0.
  119. * See {@link https://github.com/zendframework/zend-eventmanager/blob/develop/doc/book/migration/removed.md}
  120. * for details.
  121. * @param string|int $id
  122. * @return array
  123. */
  124. public function getEvents($id)
  125. {
  126. if (!array_key_exists($id, $this->identifiers)) {
  127. //Check if there are any id wildcards listeners
  128. if ('*' != $id && array_key_exists('*', $this->identifiers)) {
  129. return $this->identifiers['*']->getEvents();
  130. }
  131. return false;
  132. }
  133. return $this->identifiers[$id]->getEvents();
  134. }
  135. /**
  136. * Retrieve all listeners for a given identifier and event
  137. *
  138. * @param string|int $id
  139. * @param string|int $event
  140. * @return false|PriorityQueue
  141. */
  142. public function getListeners($id, $event)
  143. {
  144. if (!array_key_exists($id, $this->identifiers)) {
  145. return false;
  146. }
  147. return $this->identifiers[$id]->getListeners($event);
  148. }
  149. /**
  150. * Clear all listeners for a given identifier, optionally for a specific event
  151. *
  152. * @param string|int $id
  153. * @param null|string $event
  154. * @return bool
  155. */
  156. public function clearListeners($id, $event = null)
  157. {
  158. if (!array_key_exists($id, $this->identifiers)) {
  159. return false;
  160. }
  161. if (null === $event) {
  162. unset($this->identifiers[$id]);
  163. return true;
  164. }
  165. return $this->identifiers[$id]->clearListeners($event);
  166. }
  167. }