Event.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. /**
  10. * Provides a great way to build de-coupled applications and allows plug-ins to tap
  11. * into the core of your application without modifying the code.
  12. *
  13. * Register a callback for a given event.
  14. *
  15. * <code>
  16. * // register a callback for the "start" event
  17. * Efs_Event::listen('start', function () {return 'Started!';});
  18. *
  19. * // register an object instance callback for the given event
  20. * Efs_Event::listen('event', array($object, 'method'));
  21. * </code>
  22. *
  23. * Fire an event and return the first response.
  24. *
  25. * <code>
  26. * // fire the "start" event
  27. * $response = Efs_Event::first('start');
  28. *
  29. * // fire the "start" event passing an array of parameters
  30. * $response = Efs_Event::first('start', array('Pimf', 'Framework'));
  31. * </code>
  32. *
  33. * Fire an event so that all listeners are called.
  34. *
  35. * <code>
  36. * // fire the "start" event
  37. * $responses = Efs_Event::fire('start');
  38. *
  39. * // fire the "start" event passing an array of parameters
  40. * $responses = Efs_Event::fire('start', array('Pimf', 'Framework'));
  41. *
  42. * // fire multiple events with the same parameters
  43. * $responses = Efs_Event::fire(array('start', 'loading'), $parameters);
  44. * </code>
  45. *
  46. * @package Pimf
  47. * @author Gjero Krsteski <[email protected]>
  48. */
  49. class Event
  50. {
  51. /**
  52. * All registered events.
  53. *
  54. * @var array
  55. */
  56. protected static $events = array();
  57. /**
  58. * Determine if an event has any registered listeners.
  59. *
  60. * @param string $event
  61. *
  62. * @return bool
  63. */
  64. public static function listeners($event)
  65. {
  66. return isset(static::$events[$event]);
  67. }
  68. /**
  69. * Register a callback for a given event.
  70. *
  71. * @param string $event
  72. * @param mixed $callback
  73. *
  74. * @return void
  75. */
  76. public static function listen($event, $callback)
  77. {
  78. static::$events[$event][] = $callback;
  79. }
  80. /**
  81. * Override all callbacks for a given event with a new callback.
  82. *
  83. * @param string $event
  84. * @param mixed $callback
  85. *
  86. * @return void
  87. */
  88. public static function override($event, $callback)
  89. {
  90. static::clear($event);
  91. static::listen($event, $callback);
  92. }
  93. /**
  94. * Clear all event listeners for a given event.
  95. *
  96. * @param string $event
  97. *
  98. * @return void
  99. */
  100. public static function clear($event)
  101. {
  102. unset(static::$events[$event]);
  103. }
  104. /**
  105. * Fire an event and return the first response.
  106. *
  107. * @param string $event
  108. * @param \Exception[] $parameters
  109. *
  110. * @return mixed
  111. */
  112. public static function first($event, $parameters = array())
  113. {
  114. $responses = static::fire($event, $parameters);
  115. return reset($responses);
  116. }
  117. /**
  118. * Fire an event and return the first response.
  119. * Execution will be halted after the first valid response is found.
  120. *
  121. * @param string $event
  122. * @param array $parameters
  123. *
  124. * @return mixed
  125. */
  126. public static function until($event, $parameters = array())
  127. {
  128. return static::fire($event, $parameters, true);
  129. }
  130. /**
  131. * Fire an event so that all listeners are called.
  132. *
  133. * @param string $events
  134. * @param array $parameters
  135. * @param bool $halt
  136. *
  137. * @return array|null
  138. */
  139. public static function fire($events, $parameters = array(), $halt = false)
  140. {
  141. $responses = array();
  142. $parameters = (array)$parameters;
  143. // If the event has listeners, iterate through them and call each listener,
  144. // passing in the parameters.
  145. foreach ((array)$events as $event) {
  146. if (static::listeners($event)) {
  147. foreach (static::$events[$event] as $callback) {
  148. $response = call_user_func_array($callback, $parameters);
  149. // If the event is set to halt,
  150. // return the first response that is not null.
  151. if ($halt and !is_null($response)) {
  152. return $response;
  153. }
  154. $responses[] = $response;
  155. }
  156. }
  157. }
  158. return $halt ? null : $responses;
  159. }
  160. }