1 | <?php |
||
27 | class Event extends Object |
||
28 | { |
||
29 | /** |
||
30 | * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]]. |
||
31 | * Event handlers may use this property to check what event it is handling. |
||
32 | */ |
||
33 | public $name; |
||
34 | /** |
||
35 | * @var object the sender of this event. If not set, this property will be |
||
36 | * set as the object whose `trigger()` method is called. |
||
37 | * This property may also be a `null` when this event is a |
||
38 | * class-level event which is triggered in a static context. |
||
39 | */ |
||
40 | public $sender; |
||
41 | /** |
||
42 | * @var bool whether the event is handled. Defaults to `false`. |
||
43 | * When a handler sets this to be `true`, the event processing will stop and |
||
44 | * ignore the rest of the uninvoked event handlers. |
||
45 | */ |
||
46 | public $handled = false; |
||
47 | /** |
||
48 | * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler. |
||
49 | * Note that this varies according to which event handler is currently executing. |
||
50 | */ |
||
51 | public $data; |
||
52 | |||
53 | /** |
||
54 | * @var array contains all globally registered event handlers. |
||
55 | */ |
||
56 | private static $_events = []; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Attaches an event handler to a class-level event. |
||
61 | * |
||
62 | * When a class-level event is triggered, event handlers attached |
||
63 | * to that class and all parent classes will be invoked. |
||
64 | * |
||
65 | * For example, the following code attaches an event handler to `ActiveRecord`'s |
||
66 | * `afterInsert` event: |
||
67 | * |
||
68 | * ```php |
||
69 | * Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) { |
||
70 | * Yii::trace(get_class($event->sender) . ' is inserted.'); |
||
71 | * }); |
||
72 | * ``` |
||
73 | * |
||
74 | * The handler will be invoked for EVERY successful ActiveRecord insertion. |
||
75 | * |
||
76 | * For more details about how to declare an event handler, please refer to [[Component::on()]]. |
||
77 | * |
||
78 | * @param string $class the fully qualified class name to which the event handler needs to attach. |
||
79 | * @param string $name the event name. |
||
80 | * @param callable $handler the event handler. |
||
81 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
82 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
83 | * @param bool $append whether to append new event handler to the end of the existing |
||
84 | * handler list. If `false`, the new handler will be inserted at the beginning of the existing |
||
85 | * handler list. |
||
86 | * @see off() |
||
87 | */ |
||
88 | 14 | public static function on($class, $name, $handler, $data = null, $append = true) |
|
97 | |||
98 | /** |
||
99 | * Detaches an event handler from a class-level event. |
||
100 | * |
||
101 | * This method is the opposite of [[on()]]. |
||
102 | * |
||
103 | * @param string $class the fully qualified class name from which the event handler needs to be detached. |
||
104 | * @param string $name the event name. |
||
105 | * @param callable $handler the event handler to be removed. |
||
106 | * If it is `null`, all handlers attached to the named event will be removed. |
||
107 | * @return bool whether a handler is found and detached. |
||
108 | * @see on() |
||
109 | */ |
||
110 | 12 | public static function off($class, $name, $handler = null) |
|
133 | |||
134 | /** |
||
135 | * Detaches all registered class-level event handlers. |
||
136 | * @see on() |
||
137 | * @see off() |
||
138 | * @since 2.0.10 |
||
139 | */ |
||
140 | 3 | public static function offAll() |
|
144 | |||
145 | /** |
||
146 | * Returns a value indicating whether there is any handler attached to the specified class-level event. |
||
147 | * Note that this method will also check all parent classes to see if there is any handler attached |
||
148 | * to the named event. |
||
149 | * @param string|object $class the object or the fully qualified class name specifying the class-level event. |
||
150 | * @param string $name the event name. |
||
151 | * @return bool whether there is any handler attached to the event. |
||
152 | */ |
||
153 | 72 | public static function hasHandlers($class, $name) |
|
178 | |||
179 | /** |
||
180 | * Triggers a class-level event. |
||
181 | * This method will cause invocation of event handlers that are attached to the named event |
||
182 | * for the specified class and all its parent classes. |
||
183 | * @param string|object $class the object or the fully qualified class name specifying the class-level event. |
||
184 | * @param string $name the event name. |
||
185 | * @param Event $event the event parameter. If not set, a default [[Event]] object will be created. |
||
186 | */ |
||
187 | 789 | public static function trigger($class, $name, $event = null) |
|
225 | } |
||
226 |