zoukankan      html  css  js  c++  java
  • yii2阅读随笔13

    下面我们来看一下Event.php

    <?php
    /**
     * @link http://www.yiiframework.com/
     * @copyright Copyright (c) 2008 Yii Software LLC
     * @license http://www.yiiframework.com/license/
     */
    namespace yiiase;
    /**
     * Event is the base class for all event classes.
     * 事件是所有事件类的基础。
     * It encapsulates the parameters associated with an event.
     * The [[sender]] property describes who raises the event.
     * 它封装了与事件相关的参数 
     * And the [[handled]] property indicates if the event is handled.
     * 属性指示事件处理。
     *
     * Additionally, when attaching an event handler, extra data may be passed
     * and be available via the [[data]] property when the event handler is invoked.
     * 此外,当附加事件处理程序时,可能会通过额外的数据 ,当事件处理程序被调用时,可通过[ data]属性提供。
     * @author Qiang Xue <qiang.xue@gmail.com>
     * @since 2.0
     */
    class Event extends Object
    {
        /**
         * @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.
         * 事件的名字
         */
        public $name;
        /**
         * @var object the sender of this event. If not set, this property will be
         * 触发事件的对象
         */
        public $sender;
        /**
         * @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
         */
        public $handled = false;
        /**
         * @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.
         */
        public $data;
        /**
         * 存储所有的 event,因为是 static 的属性,所有的 event 对象/类都共享这一份数据
         */
        private static $_events = [];
        /**
         * 为一个类添加事件
         *
         * @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])) {
                // 不存在,直接返回false
                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;
        }
  • 相关阅读:
    普通类型(Trivial Type)和标准布局类型(Standard-layout Type)以及POD类型
    设计模式
    网络相关的学习和命令总结
    sheel命令学习和工作总结。
    Makefile的学习
    [UI基础][实现]九宫格之应用程序管理
    [嵌入式][分享][交流]发布一个消息地图的模块
    [UI基础][不会说话的汤姆猫]
    [UI基础][QQ登陆界面]
    volatile的陷阱
  • 原文地址:https://www.cnblogs.com/taokai/p/5427496.html
Copyright © 2011-2022 走看看