Event.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. /**
  9. * Event is the base class for all event classes.
  10. *
  11. * It encapsulates the parameters associated with an event.
  12. * The [[sender]] property describes who raises the event.
  13. * And the [[handled]] property indicates if the event is handled.
  14. * If an event handler sets [[handled]] to be `true`, the rest of the
  15. * uninvoked handlers will no longer be called to handle the event.
  16. *
  17. * Additionally, when attaching an event handler, extra data may be passed
  18. * and be available via the [[data]] property when the event handler is invoked.
  19. *
  20. * For more details and usage information on Event, see the [guide article on events](guide:concept-events).
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @since 2.0
  24. */
  25. class Event extends Object
  26. {
  27. /**
  28. * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]].
  29. * Event handlers may use this property to check what event it is handling.
  30. */
  31. public $name;
  32. /**
  33. * @var object the sender of this event. If not set, this property will be
  34. * set as the object whose `trigger()` method is called.
  35. * This property may also be a `null` when this event is a
  36. * class-level event which is triggered in a static context.
  37. */
  38. public $sender;
  39. /**
  40. * @var bool whether the event is handled. Defaults to `false`.
  41. * When a handler sets this to be `true`, the event processing will stop and
  42. * ignore the rest of the uninvoked event handlers.
  43. */
  44. public $handled = false;
  45. /**
  46. * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler.
  47. * Note that this varies according to which event handler is currently executing.
  48. */
  49. public $data;
  50. /**
  51. * @var array contains all globally registered event handlers.
  52. */
  53. private static $_events = [];
  54. /**
  55. * Attaches an event handler to a class-level event.
  56. *
  57. * When a class-level event is triggered, event handlers attached
  58. * to that class and all parent classes will be invoked.
  59. *
  60. * For example, the following code attaches an event handler to `ActiveRecord`'s
  61. * `afterInsert` event:
  62. *
  63. * ```php
  64. * Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) {
  65. * Yii::trace(get_class($event->sender) . ' is inserted.');
  66. * });
  67. * ```
  68. *
  69. * The handler will be invoked for EVERY successful ActiveRecord insertion.
  70. *
  71. * For more details about how to declare an event handler, please refer to [[Component::on()]].
  72. *
  73. * @param string $class the fully qualified class name to which the event handler needs to attach.
  74. * @param string $name the event name.
  75. * @param callable $handler the event handler.
  76. * @param mixed $data the data to be passed to the event handler when the event is triggered.
  77. * When the event handler is invoked, this data can be accessed via [[Event::data]].
  78. * @param bool $append whether to append new event handler to the end of the existing
  79. * handler list. If `false`, the new handler will be inserted at the beginning of the existing
  80. * handler list.
  81. * @see off()
  82. */
  83. public static function on($class, $name, $handler, $data = null, $append = true)
  84. {
  85. $class = ltrim($class, '\\');
  86. if ($append || empty(self::$_events[$name][$class])) {
  87. self::$_events[$name][$class][] = [$handler, $data];
  88. } else {
  89. array_unshift(self::$_events[$name][$class], [$handler, $data]);
  90. }
  91. }
  92. /**
  93. * Detaches an event handler from a class-level event.
  94. *
  95. * This method is the opposite of [[on()]].
  96. *
  97. * @param string $class the fully qualified class name from which the event handler needs to be detached.
  98. * @param string $name the event name.
  99. * @param callable $handler the event handler to be removed.
  100. * If it is `null`, all handlers attached to the named event will be removed.
  101. * @return bool whether a handler is found and detached.
  102. * @see on()
  103. */
  104. public static function off($class, $name, $handler = null)
  105. {
  106. $class = ltrim($class, '\\');
  107. if (empty(self::$_events[$name][$class])) {
  108. return false;
  109. }
  110. if ($handler === null) {
  111. unset(self::$_events[$name][$class]);
  112. return true;
  113. }
  114. $removed = false;
  115. foreach (self::$_events[$name][$class] as $i => $event) {
  116. if ($event[0] === $handler) {
  117. unset(self::$_events[$name][$class][$i]);
  118. $removed = true;
  119. }
  120. }
  121. if ($removed) {
  122. self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
  123. }
  124. return $removed;
  125. }
  126. /**
  127. * Detaches all registered class-level event handlers.
  128. * @see on()
  129. * @see off()
  130. * @since 2.0.10
  131. */
  132. public static function offAll()
  133. {
  134. self::$_events = [];
  135. }
  136. /**
  137. * Returns a value indicating whether there is any handler attached to the specified class-level event.
  138. * Note that this method will also check all parent classes to see if there is any handler attached
  139. * to the named event.
  140. * @param string|object $class the object or the fully qualified class name specifying the class-level event.
  141. * @param string $name the event name.
  142. * @return bool whether there is any handler attached to the event.
  143. */
  144. public static function hasHandlers($class, $name)
  145. {
  146. if (empty(self::$_events[$name])) {
  147. return false;
  148. }
  149. if (is_object($class)) {
  150. $class = get_class($class);
  151. } else {
  152. $class = ltrim($class, '\\');
  153. }
  154. $classes = array_merge(
  155. [$class],
  156. class_parents($class, true),
  157. class_implements($class, true)
  158. );
  159. foreach ($classes as $class) {
  160. if (!empty(self::$_events[$name][$class])) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * Triggers a class-level event.
  168. * This method will cause invocation of event handlers that are attached to the named event
  169. * for the specified class and all its parent classes.
  170. * @param string|object $class the object or the fully qualified class name specifying the class-level event.
  171. * @param string $name the event name.
  172. * @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
  173. */
  174. public static function trigger($class, $name, $event = null)
  175. {
  176. if (empty(self::$_events[$name])) {
  177. return;
  178. }
  179. if ($event === null) {
  180. $event = new static;
  181. }
  182. $event->handled = false;
  183. $event->name = $name;
  184. if (is_object($class)) {
  185. if ($event->sender === null) {
  186. $event->sender = $class;
  187. }
  188. $class = get_class($class);
  189. } else {
  190. $class = ltrim($class, '\\');
  191. }
  192. $classes = array_merge(
  193. [$class],
  194. class_parents($class, true),
  195. class_implements($class, true)
  196. );
  197. foreach ($classes as $class) {
  198. if (!empty(self::$_events[$name][$class])) {
  199. foreach (self::$_events[$name][$class] as $handler) {
  200. $event->data = $handler[1];
  201. call_user_func($handler[0], $event);
  202. if ($event->handled) {
  203. return;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }