WildcardEmitterTrait.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\Event;
  4. /**
  5. * Wildcard Emitter Trait.
  6. *
  7. * This trait provides the implementation for WildCardEmitter
  8. * Refer to that class for the full documentation about this
  9. * trait.
  10. *
  11. * Normally you can just instantiate that class, but if you want to add
  12. * emitter functionality to existing classes, using the trait might be a
  13. * better way to do this.
  14. *
  15. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  16. * @author Evert Pot (http://evertpot.com/)
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. trait WildcardEmitterTrait
  20. {
  21. /**
  22. * Subscribe to an event.
  23. */
  24. public function on(string $eventName, callable $callBack, int $priority = 100)
  25. {
  26. // If it ends with a wildcard, we use the wildcardListeners array
  27. if ('*' === $eventName[\strlen($eventName) - 1]) {
  28. $eventName = \substr($eventName, 0, -1);
  29. $listeners = &$this->wildcardListeners;
  30. } else {
  31. $listeners = &$this->listeners;
  32. }
  33. // Always fully reset the listener index. This is fairly sane for most
  34. // applications, because there's a clear "event registering" and "event
  35. // emitting" phase, but can be slow if there's a lot adding and removing
  36. // of listeners during emitting of events.
  37. $this->listenerIndex = [];
  38. if (!isset($listeners[$eventName])) {
  39. $listeners[$eventName] = [];
  40. }
  41. $listeners[$eventName][] = [$priority, $callBack];
  42. }
  43. /**
  44. * Subscribe to an event exactly once.
  45. */
  46. public function once(string $eventName, callable $callBack, int $priority = 100)
  47. {
  48. $wrapper = null;
  49. $wrapper = function () use ($eventName, $callBack, &$wrapper) {
  50. $this->removeListener($eventName, $wrapper);
  51. return \call_user_func_array($callBack, \func_get_args());
  52. };
  53. $this->on($eventName, $wrapper, $priority);
  54. }
  55. /**
  56. * Emits an event.
  57. *
  58. * This method will return true if 0 or more listeners were successfully
  59. * handled. false is returned if one of the events broke the event chain.
  60. *
  61. * If the continueCallBack is specified, this callback will be called every
  62. * time before the next event handler is called.
  63. *
  64. * If the continueCallback returns false, event propagation stops. This
  65. * allows you to use the eventEmitter as a means for listeners to implement
  66. * functionality in your application, and break the event loop as soon as
  67. * some condition is fulfilled.
  68. *
  69. * Note that returning false from an event subscriber breaks propagation
  70. * and returns false, but if the continue-callback stops propagation, this
  71. * is still considered a 'successful' operation and returns true.
  72. *
  73. * Lastly, if there are 5 event handlers for an event. The continueCallback
  74. * will be called at most 4 times.
  75. */
  76. public function emit(string $eventName, array $arguments = [], ?callable $continueCallBack = null): bool
  77. {
  78. if (\is_null($continueCallBack)) {
  79. foreach ($this->listeners($eventName) as $listener) {
  80. $result = \call_user_func_array($listener, $arguments);
  81. if (false === $result) {
  82. return false;
  83. }
  84. }
  85. } else {
  86. $listeners = $this->listeners($eventName);
  87. $counter = \count($listeners);
  88. foreach ($listeners as $listener) {
  89. --$counter;
  90. $result = \call_user_func_array($listener, $arguments);
  91. if (false === $result) {
  92. return false;
  93. }
  94. if ($counter > 0) {
  95. if (!$continueCallBack()) {
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * Returns the list of listeners for an event.
  105. *
  106. * The list is returned as an array, and the list of events are sorted by
  107. * their priority.
  108. *
  109. * @return callable[]
  110. */
  111. public function listeners(string $eventName): array
  112. {
  113. if (!\array_key_exists($eventName, $this->listenerIndex)) {
  114. // Create a new index.
  115. $listeners = [];
  116. $listenersPriority = [];
  117. if (isset($this->listeners[$eventName])) {
  118. foreach ($this->listeners[$eventName] as $listener) {
  119. $listenersPriority[] = $listener[0];
  120. $listeners[] = $listener[1];
  121. }
  122. }
  123. foreach ($this->wildcardListeners as $wcEvent => $wcListeners) {
  124. // Wildcard match
  125. if (\substr($eventName, 0, \strlen($wcEvent)) === $wcEvent) {
  126. foreach ($wcListeners as $listener) {
  127. $listenersPriority[] = $listener[0];
  128. $listeners[] = $listener[1];
  129. }
  130. }
  131. }
  132. // Sorting by priority
  133. \array_multisort($listenersPriority, SORT_NUMERIC, $listeners);
  134. // Creating index
  135. $this->listenerIndex[$eventName] = $listeners;
  136. }
  137. return $this->listenerIndex[$eventName];
  138. }
  139. /**
  140. * Removes a specific listener from an event.
  141. *
  142. * If the listener could not be found, this method will return false. If it
  143. * was removed it will return true.
  144. */
  145. public function removeListener(string $eventName, callable $listener): bool
  146. {
  147. // If it ends with a wildcard, we use the wildcardListeners array
  148. if ('*' === $eventName[\strlen($eventName) - 1]) {
  149. $eventName = \substr($eventName, 0, -1);
  150. $listeners = &$this->wildcardListeners;
  151. } else {
  152. $listeners = &$this->listeners;
  153. }
  154. if (!isset($listeners[$eventName])) {
  155. return false;
  156. }
  157. foreach ($listeners[$eventName] as $index => $check) {
  158. if ($check[1] === $listener) {
  159. // Remove listener
  160. unset($listeners[$eventName][$index]);
  161. // Reset index
  162. $this->listenerIndex = [];
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * Removes all listeners.
  170. *
  171. * If the eventName argument is specified, all listeners for that event are
  172. * removed. If it is not specified, every listener for every event is
  173. * removed.
  174. */
  175. public function removeAllListeners(?string $eventName = null)
  176. {
  177. if (\is_null($eventName)) {
  178. $this->listeners = [];
  179. $this->wildcardListeners = [];
  180. } else {
  181. if ('*' === $eventName[\strlen($eventName) - 1]) {
  182. // Wildcard event
  183. unset($this->wildcardListeners[\substr($eventName, 0, -1)]);
  184. } else {
  185. unset($this->listeners[$eventName]);
  186. }
  187. }
  188. // Reset index
  189. $this->listenerIndex = [];
  190. }
  191. /**
  192. * The list of listeners.
  193. */
  194. protected $listeners = [];
  195. /**
  196. * The list of "wildcard listeners".
  197. */
  198. protected $wildcardListeners = [];
  199. /**
  200. * An index of listeners for a specific event name. This helps speeding
  201. * up emitting events after all listeners have been set.
  202. *
  203. * If the list of listeners changes though, the index clears.
  204. */
  205. protected $listenerIndex = [];
  206. }