1 | <?php |
||
47 | class Event implements EventInterface |
||
48 | { |
||
49 | |||
50 | /** |
||
51 | * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]]. |
||
52 | * Event handlers may use this property to check what event it is handling. |
||
53 | */ |
||
54 | public $name; |
||
55 | |||
56 | /** |
||
57 | * @var object the sender of this event. If not set, this property will be |
||
58 | * set as the object whose "trigger()" method is called. |
||
59 | * This property may also be a `null` when this event is a |
||
60 | * class-level event which is triggered in a static context. |
||
61 | */ |
||
62 | public $sender; |
||
63 | |||
64 | /** |
||
65 | * @var boolean whether the event is handled. Defaults to false. |
||
66 | * When a handler sets this to be true, the event processing will stop and |
||
67 | * ignore the rest of the uninvoked event handlers. |
||
68 | */ |
||
69 | public $handled = false; |
||
70 | |||
71 | /** |
||
72 | * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler. |
||
73 | * Note that this varies according to which event handler is currently executing. |
||
74 | */ |
||
75 | public $data; |
||
76 | |||
77 | /** |
||
78 | * Array of events |
||
79 | * @var EventInterface[] |
||
80 | */ |
||
81 | private static $_events = []; |
||
82 | |||
83 | /** |
||
84 | * Attaches an event handler to a class-level event. |
||
85 | * |
||
86 | * When a class-level event is triggered, event handlers attached |
||
87 | * to that class and all parent classes will be invoked. |
||
88 | * |
||
89 | * For example, the following code attaches an event handler to document's |
||
90 | * `afterInsert` event: |
||
91 | * |
||
92 | * ~~~ |
||
93 | * Event::on($model, EntityManager::EventAfterInsert, function ($event) { |
||
94 | * var_dump(get_class($event->sender) . ' is inserted.'); |
||
95 | * }); |
||
96 | * ~~~ |
||
97 | * |
||
98 | * The handler will be invoked for every successful document insertion. |
||
99 | * |
||
100 | * **NOTE:** Each call will attach new event handler. When placing event |
||
101 | * initialization in class constructors etc. ensure that it is evaluated once, |
||
102 | * or it might trigger same event handler multiple times. |
||
103 | * |
||
104 | * @param AnnotatedInterface|string $model the object specifying the class-level event. |
||
105 | * @param string $name the event name. |
||
106 | * @param callable $handler the event handler. |
||
107 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
108 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
109 | * @param boolean $append whether to append new event handler to the end of the existing |
||
110 | * handler list. If false, the new handler will be inserted at the beginning of the existing |
||
111 | * handler list. |
||
112 | * @see off() |
||
113 | */ |
||
114 | public static function on($model, $name, $handler, $data = null, $append = true) |
||
126 | |||
127 | /** |
||
128 | * Detaches an event handler from a class-level event. |
||
129 | * |
||
130 | * This method is the opposite of [[on()]]. |
||
131 | * |
||
132 | * @param AnnotatedInterface|string $model the object specifying the class-level event. |
||
133 | * @param string $name the event name. |
||
134 | * @param callable $handler the event handler to be removed. |
||
135 | * If it is null, all handlers attached to the named event will be removed. |
||
136 | * @return boolean whether a handler is found and detached. |
||
137 | * @see on() |
||
138 | */ |
||
139 | public static function off($model, $name, $handler = null) |
||
169 | |||
170 | /** |
||
171 | * Triggers a class-level event. |
||
172 | * This method will cause invocation of event handlers that are attached to the named event |
||
173 | * for the specified class and all its parent classes. |
||
174 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
175 | * @param string $name the event name. |
||
176 | * @param ModelEvent $event the event parameter. If not set, a default [[Event]] object will be created. |
||
177 | * @return bool True if event was triggered. |
||
178 | */ |
||
179 | public static function trigger(AnnotatedInterface $model, $name, &$event = null) |
||
245 | |||
246 | /** |
||
247 | * Triggers a class-level event and checks if it's valid. |
||
248 | * If don't have event handler returns true. If event handler is set, return true if `Event::isValid`. |
||
249 | * This method will cause invocation of event handlers that are attached to the named event |
||
250 | * for the specified class and all its parent classes. |
||
251 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
252 | * @param string $name the event name. |
||
253 | * @param ModelEvent $event the event parameter. If not set, a default [[ModelEvent]] object will be created. |
||
254 | * @return bool True if event was triggered and is valid. |
||
255 | */ |
||
256 | public static function valid(AnnotatedInterface $model, $name, $event = null) |
||
267 | |||
268 | /** |
||
269 | * Triggers a class-level event and checks if it's handled. |
||
270 | * If don't have event handler returns true. If event handler is set, return true if `Event::handled`. |
||
271 | * This method will cause invocation of event handlers that are attached to the named event |
||
272 | * for the specified class and all its parent classes. |
||
273 | * @param AnnotatedInterface $model the object specifying the class-level event. |
||
274 | * @param string $name the event name. |
||
275 | * @param ModelEvent $event the event parameter. If not set, a default [[Event]] object will be created. |
||
276 | * @return bool|null True if handled, false otherway, null if not triggered |
||
277 | */ |
||
278 | public static function handled(AnnotatedInterface $model, $name, $event = null) |
||
286 | |||
287 | /** |
||
288 | * Check if model has event handler. |
||
289 | * **IMPORTANT**: It does not check for propagated events |
||
290 | * |
||
291 | * @param AnnotatedInterface|string $class the object specifying the class-level event |
||
292 | * @param string $name the event name. |
||
293 | * @return bool True if has handler |
||
294 | */ |
||
295 | public static function hasHandler($class, $name) |
||
309 | |||
310 | /** |
||
311 | * Get class name |
||
312 | * @param AnnotatedInterface|string $class |
||
313 | * @return string |
||
314 | */ |
||
315 | private static function _getName($class) |
||
330 | |||
331 | /** |
||
332 | * Propagate event |
||
333 | * @param AnnotatedInterface $model |
||
334 | * @param string $name |
||
335 | * @param ModelEvent|null $event |
||
336 | */ |
||
337 | private static function _propagate(AnnotatedInterface $model, $name, &$event = null) |
||
369 | |||
370 | } |
||
371 |