event.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Determine if an event has any registered listeners.
  11. *
  12. * @param string $event
  13. * @return bool
  14. */
  15. public static function listeners($event)
  16. {
  17. return isset(static::$events[$event]);
  18. }
  19. /**
  20. * Register a callback for a given event.
  21. *
  22. * <code>
  23. * // Register a callback for the "start" event
  24. * Event::listen('start', function() {return 'Started!';});
  25. *
  26. * // Register an object instance callback for the given event
  27. * Event::listen('event', array($object, 'method'));
  28. * </code>
  29. *
  30. * @param string $event
  31. * @param mixed $callback
  32. * @return void
  33. */
  34. public static function listen($event, $callback)
  35. {
  36. static::$events[$event][] = $callback;
  37. }
  38. /**
  39. * Override all callbacks for a given event with a new callback.
  40. *
  41. * @param string $event
  42. * @param mixed $callback
  43. * @return void
  44. */
  45. public static function override($event, $callback)
  46. {
  47. static::clear($event);
  48. static::listen($event, $callback);
  49. }
  50. /**
  51. * Clear all event listeners for a given event.
  52. *
  53. * @param string $event
  54. * @return void
  55. */
  56. public static function clear($event)
  57. {
  58. unset(static::$events[$event]);
  59. }
  60. /**
  61. * Fire an event and return the first response.
  62. *
  63. * <code>
  64. * // Fire the "start" event
  65. * $response = Event::first('start');
  66. *
  67. * // Fire the "start" event passing an array of parameters
  68. * $response = Event::first('start', array('Laravel', 'Framework'));
  69. * </code>
  70. *
  71. * @param string $event
  72. * @param array $parameters
  73. * @return mixed
  74. */
  75. public static function first($event, $parameters = array())
  76. {
  77. return head(static::fire($event, $parameters));
  78. }
  79. /**
  80. * Fire an event and return the the first response.
  81. *
  82. * Execution will be halted after the first valid response is found.
  83. *
  84. * @param string $event
  85. * @param array $parameters
  86. * @return mixed
  87. */
  88. public static function until($event, $parameters = array())
  89. {
  90. return static::fire($event, $parameters, true);
  91. }
  92. /**
  93. * Fire an event so that all listeners are called.
  94. *
  95. * <code>
  96. * // Fire the "start" event
  97. * $responses = Event::fire('start');
  98. *
  99. * // Fire the "start" event passing an array of parameters
  100. * $responses = Event::fire('start', array('Laravel', 'Framework'));
  101. *
  102. * // Fire multiple events with the same parameters
  103. * $responses = Event::fire(array('start', 'loading'), $parameters);
  104. * </code>
  105. *
  106. * @param string|array $event
  107. * @param array $parameters
  108. * @param bool $halt
  109. * @return array
  110. */
  111. public static function fire($events, $parameters = array(), $halt = false)
  112. {
  113. $responses = array();
  114. $parameters = (array) $parameters;
  115. // If the event has listeners, we will simply iterate through them and call
  116. // each listener, passing in the parameters. We will add the responses to
  117. // an array of event responses and return the array.
  118. foreach ((array) $events as $event)
  119. {
  120. if (static::listeners($event))
  121. {
  122. foreach (static::$events[$event] as $callback)
  123. {
  124. $response = call_user_func_array($callback, $parameters);
  125. // If the event is set to halt, we will return the first response
  126. // that is not null. This allows the developer to easily stack
  127. // events but still get the first valid response.
  128. if ($halt and ! is_null($response))
  129. {
  130. return $response;
  131. }
  132. // After the handler has been called, we'll add the response to
  133. // an array of responses and return the array to the caller so
  134. // all of the responses can be easily examined.
  135. $responses[] = $response;
  136. }
  137. }
  138. }
  139. return $responses;
  140. }
  141. }