event.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php namespace Laravel;
  2. class Event {
  3. /**
  4. * All of the registered events.
  5. *
  6. * @var array
  7. */
  8. public static $events = array();
  9. /**
  10. * The queued events waiting for flushing.
  11. *
  12. * @var array
  13. */
  14. public static $queued = array();
  15. /**
  16. * All of the registered queue flusher callbacks.
  17. *
  18. * @var array
  19. */
  20. public static $flushers = array();
  21. /**
  22. * Determine if an event has any registered listeners.
  23. *
  24. * @param string $event
  25. * @return bool
  26. */
  27. public static function listeners($event)
  28. {
  29. return isset(static::$events[$event]);
  30. }
  31. /**
  32. * Register a callback for a given event.
  33. *
  34. * <code>
  35. * // Register a callback for the "start" event
  36. * Event::listen('start', function() {return 'Started!';});
  37. *
  38. * // Register an object instance callback for the given event
  39. * Event::listen('event', array($object, 'method'));
  40. * </code>
  41. *
  42. * @param string $event
  43. * @param mixed $callback
  44. * @return void
  45. */
  46. public static function listen($event, $callback)
  47. {
  48. static::$events[$event][] = $callback;
  49. }
  50. /**
  51. * Override all callbacks for a given event with a new callback.
  52. *
  53. * @param string $event
  54. * @param mixed $callback
  55. * @return void
  56. */
  57. public static function override($event, $callback)
  58. {
  59. static::clear($event);
  60. static::listen($event, $callback);
  61. }
  62. /**
  63. * Add an item to an event queue for processing.
  64. *
  65. * @param string $queue
  66. * @param string $key
  67. * @param mixed $data
  68. * @return void
  69. */
  70. public static function queue($queue, $key, $data = array())
  71. {
  72. static::$queued[$queue][$key] = $data;
  73. }
  74. /**
  75. * Register a queue flusher callback.
  76. *
  77. * @param string $queue
  78. * @param mixed $callback
  79. * @return void
  80. */
  81. public static function flusher($queue, $callback)
  82. {
  83. static::$flushers[$queue][] = $callback;
  84. }
  85. /**
  86. * Clear all event listeners for a given event.
  87. *
  88. * @param string $event
  89. * @return void
  90. */
  91. public static function clear($event)
  92. {
  93. unset(static::$events[$event]);
  94. }
  95. /**
  96. * Fire an event and return the first response.
  97. *
  98. * <code>
  99. * // Fire the "start" event
  100. * $response = Event::first('start');
  101. *
  102. * // Fire the "start" event passing an array of parameters
  103. * $response = Event::first('start', array('Laravel', 'Framework'));
  104. * </code>
  105. *
  106. * @param string $event
  107. * @param array $parameters
  108. * @return mixed
  109. */
  110. public static function first($event, $parameters = array())
  111. {
  112. return head(static::fire($event, $parameters));
  113. }
  114. /**
  115. * Fire an event and return the the first response.
  116. *
  117. * Execution will be halted after the first valid response is found.
  118. *
  119. * @param string $event
  120. * @param array $parameters
  121. * @return mixed
  122. */
  123. public static function until($event, $parameters = array())
  124. {
  125. return static::fire($event, $parameters, true);
  126. }
  127. /**
  128. * Flush an event queue, firing the flusher for each payload.
  129. *
  130. * @param string $queue
  131. * @return void
  132. */
  133. public static function flush($queue)
  134. {
  135. foreach (static::$flushers[$queue] as $flusher)
  136. {
  137. // We will simply spin through each payload registered for the event and
  138. // fire the flusher, passing each payloads as we go. This allows all
  139. // the events on the queue to be processed by the flusher easily.
  140. foreach (static::$queued[$queue] as $key => $payload)
  141. {
  142. array_unshift($payload, $key);
  143. call_user_func_array($flusher, $payload);
  144. }
  145. }
  146. }
  147. /**
  148. * Fire an event so that all listeners are called.
  149. *
  150. * <code>
  151. * // Fire the "start" event
  152. * $responses = Event::fire('start');
  153. *
  154. * // Fire the "start" event passing an array of parameters
  155. * $responses = Event::fire('start', array('Laravel', 'Framework'));
  156. *
  157. * // Fire multiple events with the same parameters
  158. * $responses = Event::fire(array('start', 'loading'), $parameters);
  159. * </code>
  160. *
  161. * @param string|array $event
  162. * @param array $parameters
  163. * @param bool $halt
  164. * @return array
  165. */
  166. public static function fire($events, $parameters = array(), $halt = false)
  167. {
  168. $responses = array();
  169. $parameters = (array) $parameters;
  170. // If the event has listeners, we will simply iterate through them and call
  171. // each listener, passing in the parameters. We will add the responses to
  172. // an array of event responses and return the array.
  173. foreach ((array) $events as $event)
  174. {
  175. if (static::listeners($event))
  176. {
  177. foreach (static::$events[$event] as $callback)
  178. {
  179. $response = call_user_func_array($callback, $parameters);
  180. // If the event is set to halt, we will return the first response
  181. // that is not null. This allows the developer to easily stack
  182. // events but still get the first valid response.
  183. if ($halt and ! is_null($response))
  184. {
  185. return $response;
  186. }
  187. // After the handler has been called, we'll add the response to
  188. // an array of responses and return the array to the caller so
  189. // all of the responses can be easily examined.
  190. $responses[] = $response;
  191. }
  192. }
  193. }
  194. return $halt ? null : $responses;
  195. }
  196. }