/vendor/yiisoft/yii2/base/Event.php

https://gitlab.com/makkooz/nikestreetbeat · PHP · 208 lines · 99 code · 13 blank · 96 comment · 19 complexity · c616d94c195c717f46b290b793fb54ad MD5 · raw file

  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 <qiang.xue@gmail.com>
  21. * @since 2.0
  22. */
  23. class Event extends Object
  24. {
  25. private static $_events = [];
  26. /**
  27. * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]].
  28. * Event handlers may use this property to check what event it is handling.
  29. */
  30. public $name;
  31. /**
  32. * @var object the sender of this event. If not set, this property will be
  33. * set as the object whose "trigger()" method is called.
  34. * This property may also be a `null` when this event is a
  35. * class-level event which is triggered in a static context.
  36. */
  37. public $sender;
  38. /**
  39. * @var boolean whether the event is handled. Defaults to false.
  40. * When a handler sets this to be true, the event processing will stop and
  41. * ignore the rest of the uninvoked event handlers.
  42. */
  43. public $handled = false;
  44. /**
  45. * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler.
  46. * Note that this varies according to which event handler is currently executing.
  47. */
  48. public $data;
  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. * ```php
  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 callable $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. * @param boolean $append whether to append new event handler to the end of the existing
  74. * handler list. If false, the new handler will be inserted at the beginning of the existing
  75. * handler list.
  76. * @see off()
  77. */
  78. public static function on($class, $name, $handler, $data = null, $append = true)
  79. {
  80. $class = ltrim($class, '\\');
  81. if ($append || empty(self::$_events[$name][$class])) {
  82. self::$_events[$name][$class][] = [$handler, $data];
  83. } else {
  84. array_unshift(self::$_events[$name][$class], [$handler, $data]);
  85. }
  86. }
  87. /**
  88. * Detaches an event handler from a class-level event.
  89. *
  90. * This method is the opposite of [[on()]].
  91. *
  92. * @param string $class the fully qualified class name from which the event handler needs to be detached.
  93. * @param string $name the event name.
  94. * @param callable $handler the event handler to be removed.
  95. * If it is null, all handlers attached to the named event will be removed.
  96. * @return boolean whether a handler is found and detached.
  97. * @see on()
  98. */
  99. public static function off($class, $name, $handler = null)
  100. {
  101. $class = ltrim($class, '\\');
  102. if (empty(self::$_events[$name][$class])) {
  103. return false;
  104. }
  105. if ($handler === null) {
  106. unset(self::$_events[$name][$class]);
  107. return true;
  108. } else {
  109. $removed = false;
  110. foreach (self::$_events[$name][$class] as $i => $event) {
  111. if ($event[0] === $handler) {
  112. unset(self::$_events[$name][$class][$i]);
  113. $removed = true;
  114. }
  115. }
  116. if ($removed) {
  117. self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
  118. }
  119. return $removed;
  120. }
  121. }
  122. /**
  123. * Returns a value indicating whether there is any handler attached to the specified class-level event.
  124. * Note that this method will also check all parent classes to see if there is any handler attached
  125. * to the named event.
  126. * @param string|object $class the object or the fully qualified class name specifying the class-level event.
  127. * @param string $name the event name.
  128. * @return boolean whether there is any handler attached to the event.
  129. */
  130. public static function hasHandlers($class, $name)
  131. {
  132. if (empty(self::$_events[$name])) {
  133. return false;
  134. }
  135. if (is_object($class)) {
  136. $class = get_class($class);
  137. } else {
  138. $class = ltrim($class, '\\');
  139. }
  140. $classes = array_merge(
  141. [$class],
  142. class_parents($class, true),
  143. class_implements($class, true)
  144. );
  145. foreach ($classes as $class) {
  146. if (!empty(self::$_events[$name][$class])) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. /**
  153. * Triggers a class-level event.
  154. * This method will cause invocation of event handlers that are attached to the named event
  155. * for the specified class and all its parent classes.
  156. * @param string|object $class the object or the fully qualified class name specifying the class-level event.
  157. * @param string $name the event name.
  158. * @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
  159. */
  160. public static function trigger($class, $name, $event = null)
  161. {
  162. if (empty(self::$_events[$name])) {
  163. return;
  164. }
  165. if ($event === null) {
  166. $event = new static;
  167. }
  168. $event->handled = false;
  169. $event->name = $name;
  170. if (is_object($class)) {
  171. if ($event->sender === null) {
  172. $event->sender = $class;
  173. }
  174. $class = get_class($class);
  175. } else {
  176. $class = ltrim($class, '\\');
  177. }
  178. $classes = array_merge(
  179. [$class],
  180. class_parents($class, true),
  181. class_implements($class, true)
  182. );
  183. foreach ($classes as $class) {
  184. if (!empty(self::$_events[$name][$class])) {
  185. foreach (self::$_events[$name][$class] as $handler) {
  186. $event->data = $handler[1];
  187. call_user_func($handler[0], $event);
  188. if ($event->handled) {
  189. return;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }