Rumah > Artikel > pembangunan bahagian belakang > Yii2的深入学习-yiibaseEvent 类
Yii2的深入学习--yii\base\Event 类
根据之前一篇文章,我们知道 Yii2 的事件分两类,一是类级别的事件,二是实例级别的事件。类级别的事件是基于 yii\base\Event 实现,实例级别的事件是基于 yii\base\Component 实现。
今天先来看下类级别事件的实现,代码是 yii\base\Event 类。
<span style="color: #000000;">phpnamespace yii\base;</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Event is the base class for all event classes. </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span> Event <span style="color: #0000ff;">extends</span> <span style="color: #0000ff;">Object</span><span style="color: #000000;">{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var string the event name. This property is set by [[Component::trigger()]] and [[trigger()]]. * Event handlers may use this property to check what event it is handling. * 事件的名字 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var object the sender of this event. If not set, this property will be * set as the object whose "trigger()" method is called. * This property may also be a `null` when this event is a * class-level event which is triggered in a static context. * 触发事件的对象 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$sender</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var boolean whether the event is handled. Defaults to false. * When a handler sets this to be true, the event processing will stop and * ignore the rest of the uninvoked event handlers. * 记录事件是否已被处理,当 handled 被设置为 true 时,执行到这个 event 的时候,会停止,并忽略剩下的 event </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$handled</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var mixed the data that is passed to [[Component::on()]] when attaching an event handler. * Note that this varies according to which event handler is currently executing. </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$data</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 存储所有的 event,因为是 static 的属性,所有的 event 对象/类都共享这一份数据 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$_events</span> =<span style="color: #000000;"> []; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Attaches an event handler to a class-level event. * * When a class-level event is triggered, event handlers attached * to that class and all parent classes will be invoked. * * For example, the following code attaches an event handler to `ActiveRecord`'s * `afterInsert` event: * * ~~~ * Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) { * Yii::trace(get_class($event->sender) . ' is inserted.'); * }); * ~~~ * * The handler will be invoked for EVERY successful ActiveRecord insertion. * * For more details about how to declare an event handler, please refer to [[Component::on()]]. * * 为一个类添加事件 * * @param string $class the fully qualified class name to which the event handler needs to attach. * @param string $name the event name. * @param callable $handler the event handler. * @param mixed $data the data to be passed to the event handler when the event is triggered. * When the event handler is invoked, this data can be accessed via [[Event::data]]. * @param boolean $append whether to append new event handler to the end of the existing * handler list. If false, the new handler will be inserted at the beginning of the existing * handler list. * @see off() </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> on(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$name</span>, <span style="color: #800080;">$handler</span>, <span style="color: #800080;">$data</span> = <span style="color: #0000ff;">null</span>, <span style="color: #800080;">$append</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 去掉 class 最左边的斜杠</span> <span style="color: #800080;">$class</span> = <span style="color: #008080;">ltrim</span>(<span style="color: #800080;">$class</span>, '\\'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 append 为true,就放到 $_events 中名字为 $name 的数组的最后,否则放到最前面</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$append</span> || <span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span><span style="color: #000000;">])) { self</span>::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span>][] = [<span style="color: #800080;">$handler</span>, <span style="color: #800080;">$data</span><span style="color: #000000;">]; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">array_unshift</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span>], [<span style="color: #800080;">$handler</span>, <span style="color: #800080;">$data</span><span style="color: #000000;">]); } } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Detaches an event handler from a class-level event. * * This method is the opposite of [[on()]]. * * 移除一个类的事件 * * @param string $class the fully qualified class name from which the event handler needs to be detached. * @param string $name the event name. * @param callable $handler the event handler to be removed. * If it is null, all handlers attached to the named event will be removed. * @return boolean whether a handler is found and detached. * @see on() </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> off(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$name</span>, <span style="color: #800080;">$handler</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #800080;">$class</span> = <span style="color: #008080;">ltrim</span>(<span style="color: #800080;">$class</span>, '\\'<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 不存在该事件</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$handler</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 handler 为空,直接将在该类下该事件移除,即移出所有的是这个名字的事件</span> <span style="color: #0000ff;">unset</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span><span style="color: #000000;">]); </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$removed</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $handler 不为空,循环 $_events 找到相应的 handler,只移除这个 handler 和 data 组成的数组</span> <span style="color: #0000ff;">foreach</span> (self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span>] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$i</span> => <span style="color: #800080;">$event</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$event</span>[0] === <span style="color: #800080;">$handler</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">unset</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span>][<span style="color: #800080;">$i</span><span style="color: #000000;">]); </span><span style="color: #800080;">$removed</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$removed</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 移除之后,使数组重新变成一个自然数组</span> self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span>] = <span style="color: #008080;">array_values</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span><span style="color: #000000;">]); } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$removed</span><span style="color: #000000;">; } } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Returns a value indicating whether there is any handler attached to the specified class-level event. * Note that this method will also check all parent classes to see if there is any handler attached * to the named event. * 检测在某个类或者对象是否具有某个事件 * @param string|object $class the object or the fully qualified class name specifying the class-level event. * @param string $name the event name. * @return boolean whether there is any handler attached to the event. </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> hasHandlers(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$name</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span><span style="color: #000000;">])) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 不存在,直接返回</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_object</span>(<span style="color: #800080;">$class</span><span style="color: #000000;">)) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果是一个 object,就获取其类名</span> <span style="color: #800080;">$class</span> = <span style="color: #008080;">get_class</span>(<span style="color: #800080;">$class</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果是一个类名,就去掉 class 最左边的斜杠</span> <span style="color: #800080;">$class</span> = <span style="color: #008080;">ltrim</span>(<span style="color: #800080;">$class</span>, '\\'<span style="color: #000000;">); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果该类中找不到,就去父类中找,直到找到或者没有父类了为止</span> <span style="color: #0000ff;">do</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span><span style="color: #000000;">])) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">while</span> ((<span style="color: #800080;">$class</span> = <span style="color: #008080;">get_parent_class</span>(<span style="color: #800080;">$class</span>)) !== <span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Triggers a class-level event. * This method will cause invocation of event handlers that are attached to the named event * for the specified class and all its parent classes. * 触发某个类或者对象的某个事件 * @param string|object $class the object or the fully qualified class name specifying the class-level event. * @param string $name the event name. * @param Event $event the event parameter. If not set, a default [[Event]] object will be created. </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> trigger(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$name</span>, <span style="color: #800080;">$event</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span><span style="color: #000000;">])) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$event</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 事件不存在,就创建一个 Event 对象</span> <span style="color: #800080;">$event</span> = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">static</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置event对象的属性,默认是未被处理的</span> <span style="color: #800080;">$event</span>->handled = <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #800080;">$event</span>->name = <span style="color: #800080;">$name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_object</span>(<span style="color: #800080;">$class</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$event</span>->sender === <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $class 是个对象,并且是 sender 为空,就将 $class 赋给 sender,即 $class 就是触发事件的对象</span> <span style="color: #800080;">$event</span>->sender = <span style="color: #800080;">$class</span><span style="color: #000000;">; } </span><span style="color: #800080;">$class</span> = <span style="color: #008080;">get_class</span>(<span style="color: #800080;">$class</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #800080;">$class</span> = <span style="color: #008080;">ltrim</span>(<span style="color: #800080;">$class</span>, '\\'<span style="color: #000000;">); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 循环类的 $_event,直到遇到 $event->handled 为真或者没有父类了为止</span> <span style="color: #0000ff;">do</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span><span style="color: #000000;">])) { </span><span style="color: #0000ff;">foreach</span> (self::<span style="color: #800080;">$_events</span>[<span style="color: #800080;">$name</span>][<span style="color: #800080;">$class</span>] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$handler</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 将参数赋到 event 对象的 data 属性上</span> <span style="color: #800080;">$event</span>->data = <span style="color: #800080;">$handler</span>[1<span style="color: #000000;">]; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用 $handler 方法 // 在方法中,可以用 $this->data 取到相应的参数 // 也可以在其中设置 $this->handled 的值,中断后续事件的触发</span> <span style="color: #008080;">call_user_func</span>(<span style="color: #800080;">$handler</span>[0], <span style="color: #800080;">$event</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 当某个 handled 被设置为 true 时,执行到这个事件的时候,会停止,并忽略剩下的事件</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$event</span>-><span style="color: #000000;">handled) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } } } } </span><span style="color: #0000ff;">while</span> ((<span style="color: #800080;">$class</span> = <span style="color: #008080;">get_parent_class</span>(<span style="color: #800080;">$class</span>)) !== <span style="color: #0000ff;">false</span><span style="color: #000000;">); }}</span>
通过上面代码可以看出,类级别的 Event,其本质就是在 Event 类中的 $_events 变量中存储事件,触发事件的时候,只需将其取出,执行即可。
$_events里面的数据结构大概如下:
<span style="color: #000000;">[ </span>'add' =><span style="color: #000000;"> [ </span>'Child' =><span style="color: #000000;"> [ [</span><span style="color: #0000ff;">function</span> (<span style="color: #800080;">$event</span>) { ... }, <span style="color: #800080;">$data</span>],<span style="color: #000000;"> [[</span><span style="color: #800080;">$object</span>, 'handleAdd'], <span style="color: #0000ff;">null</span>],<span style="color: #000000;"> [[</span>'ChildClass', 'handleAdd'], <span style="color: #800080;">$data</span>],<span style="color: #000000;"> [</span>'handleAdd', <span style="color: #800080;">$data</span><span style="color: #000000;">] ]</span>, 'ChildClass' =><span style="color: #000000;"> [ </span>...<span style="color: #000000;"> ] ]</span>, 'delete' =><span style="color: #000000;"> [ </span>...<span style="color: #000000;"> ]]</span>
之后讲到yii\base\Component类时,我们会再来说一下实例级别的事件。
对 Yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 Yii2 源码的注释,之后还会继续添加~
有兴趣的同学也可以参与进来,提交 Yii2 源码的注释。