yii2的深入学习--yii\base\event 类,yii2--yii根据之前一篇文章,我们知道 yii2 的事件分两类,一是类级别的事件,二是实例级别的事件。类级别的事件是基于 yii\base\event 实现,实例级别的事件是基于 yii\base\component 实现。
今天先来看下类级别事件的实现,代码是 yii\base\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() */ public static function on($class, $name, $handler, $data = null, $append = true) { // 去掉 class 最左边的斜杠 $class = ltrim($class, '\\'); // 如果 append 为true,就放到 $_events 中名字为 $name 的数组的最后,否则放到最前面 if ($append || empty(self::$_events[$name][$class])) { self::$_events[$name][$class][] = [$handler, $data]; } else { array_unshift(self::$_events[$name][$class], [$handler, $data]); } } /** * 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() */ public static function off($class, $name, $handler = null) { $class = ltrim($class, '\\'); if (empty(self::$_events[$name][$class])) { // 不存在该事件 return false; } if ($handler === null) { // 如果 handler 为空,直接将在该类下该事件移除,即移出所有的是这个名字的事件 unset(self::$_events[$name][$class]); return true; } else { $removed = false; // 如果 $handler 不为空,循环 $_events 找到相应的 handler,只移除这个 handler 和 data 组成的数组 foreach (self::$_events[$name][$class] as $i => $event) { if ($event[0] === $handler) { unset(self::$_events[$name][$class][$i]); $removed = true; } } if ($removed) { // 移除之后,使数组重新变成一个自然数组 self::$_events[$name][$class] = array_values(self::$_events[$name][$class]); } return $removed; } } /** * 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. */ public static function hashandlers($class, $name) { if (empty(self::$_events[$name])) { // 不存在,直接返回 return false; } if (is_object($class)) { // 如果是一个 object,就获取其类名 $class = get_class($class); } else { // 如果是一个类名,就去掉 class 最左边的斜杠 $class = ltrim($class, '\\'); } // 如果该类中找不到,就去父类中找,直到找到或者没有父类了为止 do { if (!empty(self::$_events[$name][$class])) { return true; } } while (($class = get_parent_class($class)) !== false); return false; } /** * 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. */ public static function trigger($class, $name, $event = null) { if (empty(self::$_events[$name])) { return; } if ($event === null) { // 事件不存在,就创建一个 event 对象 $event = new static; } // 设置event对象的属性,默认是未被处理的 $event->handled = false; $event->name = $name; if (is_object($class)) { if ($event->sender === null) { // 如果 $class 是个对象,并且是 sender 为空,就将 $class 赋给 sender,即 $class 就是触发事件的对象 $event->sender = $class; } $class = get_class($class); } else { $class = ltrim($class, '\\'); } // 循环类的 $_event,直到遇到 $event->handled 为真或者没有父类了为止 do { if (!empty(self::$_events[$name][$class])) { foreach (self::$_events[$name][$class] as $handler) { // 将参数赋到 event 对象的 data 属性上 $event->data = $handler[1]; // 调用 $handler 方法 // 在方法中,可以用 $this->data 取到相应的参数 // 也可以在其中设置 $this->handled 的值,中断后续事件的触发 call_user_func($handler[0], $event); // 当某个 handled 被设置为 true 时,执行到这个事件的时候,会停止,并忽略剩下的事件 if ($event->handled) { return; } } } } while (($class = get_parent_class($class)) !== false); }}
通过上面代码可以看出,类级别的 event,其本质就是在 event 类中的 $_events 变量中存储事件,触发事件的时候,只需将其取出,执行即可。
$_events里面的数据结构大概如下:
[ 'add' => [ 'child' => [ [function ($event) { ... }, $data], [[$object, 'handleadd'], null], [['childclass', 'handleadd'], $data], ['handleadd', $data] ], 'childclass' => [ ... ] ], 'delete' => [ ... ]]
之后讲到yii\base\component类时,我们会再来说一下实例级别的事件。
对 yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 yii2 源码的注释,之后还会继续添加~
有兴趣的同学也可以参与进来,提交 yii2 源码的注释。
http://www.bkjia.com/phpjc/1085573.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1085573.htmltecharticleyii2的深入学习--yii\base\event 类,yii2--yii 根据之前一篇文章,我们知道 yii2 的事件分两类,一是类级别的事件,二是实例级别的事件。类级别...