Event.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @author Qiang Xue <[email protected]>
  21. * @since 2.0
  22. */
  23. class Event extends Object
  24. {
  25. /**
  26. * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]].
  27. * Event handlers may use this property to check what event it is handling.
  28. */
  29. public $name;
  30. /**
  31. * @var object the sender of this event. If not set, this property will be
  32. * set as the object whose "trigger()" method is called.
  33. * This property may also be a `null` when this event is a
  34. * class-level event which is triggered in a static context.
  35. */
  36. public $sender;
  37. /**
  38. * @var boolean whether the event is handled. Defaults to false.
  39. * When a handler sets this to be true, the event processing will stop and
  40. * ignore the rest of the uninvoked event handlers.
  41. */
  42. public $handled = false;
  43. /**
  44. * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler.
  45. * Note that this varies according to which event handler is currently executing.
  46. */
  47. public $data;
  48. private static $_events = [];
  49. /**
  50. * Attaches an event handler to a class-level event.
  51. *
  52. * When a class-level event is triggered, event handlers attached
  53. * to that class and all parent classes will be invoked.
  54. *
  55. * For example, the following code attaches an event handler to `ActiveRecord`'s
  56. * `afterInsert` event:
  57. *
  58. * ~~~
  59. * Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) {
  60. * Yii::trace(get_class($event->sender) . ' is inserted.');
  61. * });
  62. * ~~~
  63. *
  64. * The handler will be invoked for EVERY successful ActiveRecord insertion.
  65. *
  66. * For more details about how to declare an event handler, please refer to [[Component::on()]].
  67. *
  68. * @param string $class the fully qualified class name to which the event handler needs to attach.
  69. * @param string $name the event name.
  70. * @param callback $handler the event handler.
  71. * @param mixed $data the data to be passed to the event handler when the event is triggered.
  72. * When the event handler is invoked, this data can be accessed via [[Event::data]].
  73. * @see off()
  74. */
  75. public static function on($class, $name, $handler, $data = null)
  76. {
  77. self::$_events[$name][ltrim($class, '\\')][] = [$handler, $data];
  78. }
  79. /**
  80. * Detaches an event handler from a class-level event.
  81. *
  82. * This method is the opposite of [[on()]].
  83. *
  84. * @param string $class the fully qualified class name from which the event handler needs to be detached.
  85. * @param string $name the event name.
  86. * @param callback $handler the event handler to be removed.
  87. * If it is null, all handlers attached to the named event will be removed.
  88. * @return boolean whether a handler is found and detached.
  89. * @see on()
  90. */
  91. public static function off($class, $name, $handler = null)
  92. {
  93. $class = ltrim($class, '\\');
  94. if (empty(self::$_events[$name][$class])) {
  95. return false;
  96. }
  97. if ($handler === null) {
  98. unset(self::$_events[$name][$class]);
  99. return true;
  100. } else {
  101. $removed = false;
  102. foreach (self::$_events[$name][$class] as $i => $event) {
  103. if ($event[0] === $handler) {
  104. unset(self::$_events[$name][$class][$i]);
  105. $removed = true;
  106. }
  107. }
  108. if ($removed) {
  109. self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
  110. }
  111. return $removed;
  112. }
  113. }
  114. /**
  115. * Returns a value indicating whether there is any handler attached to the specified class-level event.
  116. * Note that this method will also check all parent classes to see if there is any handler attached
  117. * to the named event.
  118. * @param string|object $class the object or the fully qualified class name specifying the class-level event.
  119. * @param string $name the event name.
  120. * @return boolean whether there is any handler attached to the event.
  121. */
  122. public static function hasHandlers($class, $name)
  123. {
  124. if (empty(self::$_events[$name])) {
  125. return false;
  126. }
  127. if (is_object($class)) {
  128. $class = get_class($class);
  129. } else {
  130. $class = ltrim($class, '\\');
  131. }
  132. do {
  133. if (!empty(self::$_events[$name][$class])) {
  134. return true;
  135. }
  136. } while (($class = get_parent_class($class)) !== false);
  137. return false;
  138. }
  139. /**
  140. * Triggers a class-level event.
  141. * This method will cause invocation of event handlers that are attached to the named event
  142. * for the specified class and all its parent classes.
  143. * @param string|object $class the object or the fully qualified class name specifying the class-level event.
  144. * @param string $name the event name.
  145. * @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
  146. */
  147. public static function trigger($class, $name, $event = null)
  148. {
  149. if (empty(self::$_events[$name])) {
  150. return;
  151. }
  152. if ($event === null) {
  153. $event = new static;
  154. }
  155. $event->handled = false;
  156. $event->name = $name;
  157. if (is_object($class)) {
  158. if ($event->sender === null) {
  159. $event->sender = $class;
  160. }
  161. $class = get_class($class);
  162. } else {
  163. $class = ltrim($class, '\\');
  164. }
  165. do {
  166. if (!empty(self::$_events[$name][$class])) {
  167. foreach (self::$_events[$name][$class] as $handler) {
  168. $event->data = $handler[1];
  169. call_user_func($handler[0], $event);
  170. if ($event->handled) {
  171. return;
  172. }
  173. }
  174. }
  175. } while (($class = get_parent_class($class)) !== false);
  176. }
  177. }