学习yii2.0框架阅读代码(十七)

vendor/yiisoft/yii2/base/Module.

模块类

每个模块都有一个继承yiiaseModule的模块类,该类文件直接放在模块的yiiaseModule::basePath目录下, 并且能被自动加载。当一个模块被访问,和应用主题实例类似会创建该模块类唯一实例,模块实例用来帮模块内代码共享数据和组件

class Module extends ServiceLocator
{
    /**
     * @event 在执行ActionEvent方法时触发事件
     * 你可以设置[[ActionEvent:isValid]]是错误的取消操作执行。.
     */
    const EVENT_BEFORE_ACTION = 'beforeAction';

    const EVENT_AFTER_ACTION = 'afterAction';

    /**
     * @var array custom module parameters (name => value).
     */
    public $params = [];
    /**
     * @var 控制器ID
     */
    public $id;
    /**
     * @var 所属模块
     */
    public $module;
    /**
     * @var string|boolean the layout that should be applied for views within this module. This refers to a view name
     * relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]]
     * will be taken. If this is false, layout will be disabled within this module.
     */
    public $layout;
    /**
     *  @var 数组从控制器ID映射到控制器配置
     * 每一个名称-值对将指定一个控制器的配置.
     * 一个控制器配置可以是一个字符串或一个数组
     * 如果是前者,字符串应该是控制器的完全限定类名.
     * 如果是后者,数组元素必须包含一个“类”
     * the controller's fully qualified class name, and the rest of the name-value pairs
     * 例子,
     *
     * ~~~
     * [
     *   'account' => 'appcontrollersUserController',
     *   'article' => [
     *      'class' => 'appcontrollersPostController',
     *      'pageTitle' => 'something new',
     *   ],
     * ]
     * ~~~
     */
    public $controllerMap = [];
    /**
     * @var控制器类的字符串名称空间.
     * 这个名称空间通过将控制器将用于负载控制器类
     * class name.
     *
     * If not set, it will use the `controllers` sub-namespace under the namespace of this module.
     * For example, if the namespace of this module is "fooar", then the default
     * controller namespace would be "fooarcontrollers".
     *
     * See also the [guide section on autoloading](guide:concept-autoloading) to learn more about
     * defining namespaces and how classes are loaded.
     */
    public $controllerNamespace;
    /**
     * @var 字符串的默认路由模块。默认为'default'.
     * 子模块ID,控制器的路线可能由ID,ID和/或行动.
     * For example, `help`, `post/create`, `admin/post/create`.
     * If action ID is not given, it will take the default value as specified in
     * [[Controller::defaultAction]].
     */
    public $defaultRoute = 'default';

    /**
     * @var 字符串模块的根目录.
     */
    private $_basePath;
    /**
     * @var 字符串模块的根目录
     */
    private $_viewPath;
    /**
     * @var 字符串模块的根目录.
     */
    private $_layoutPath;
    /**
     * @var 这个模块所属的子模块
     */
    private $_modules = [];


    /**
     * 定义一个构造函数.
     * @param string $id the ID of this module
     * @param Module $parent the parent module (if any)
     * @param array $config name-value pairs that will be used to initialize the object properties
     */
    public function __construct($id, $parent = null, $config = [])
    {
        $this->id = $id;
        $this->module = $parent;
        parent::__construct($config);
    }

    /**
     * 返回当前请求的这个模块类的实例
     * If the module class is not currently requested, null will be returned.
     * This method is provided so that you access the module instance from anywhere within the module.
     * @return static|null the currently requested instance of this module class, or null if the module class is not requested.
     */
    public static function getInstance()
    {
        $class = get_called_class();
        return isset(Yii::$app->loadedModules[$class]) ? Yii::$app->loadedModules[$class] : null;
    }

    /**
     * 设置当前请求这个模块类的实例.
     * @param Module|null $instance the currently requested instance of this module class.
     * If it is null, the instance of the calling class will be removed, if any.
     */
    public static function setInstance($instance)
    {
        // get_called_class和get_class获取的值都是带namespace的
        if ($instance === null) {
            // 如果实例不存在,就将其从$app的loadedModules移出掉
            unset(Yii::$app->loadedModules[get_called_class()]);
        } else {
            // 如果实例存在,就将其加入到$app的loadedModules里
            Yii::$app->loadedModules[get_class($instance)] = $instance;
        }
    }

    /**
     * 初始化模块.
     *
     * 调用此方法后,模块与属性值创建并初始化
     * 在配置。的默认实现将初始化[[controllerNamespace]]
     * if it is not set.
     *
     * If you override this method, please make sure you call the parent implementation.
     */
    
    
    public function init()
    {
        //取出控制器的命名空间,也可以理解为路径
        if ($this->controllerNamespace === null) {
            $class = get_class($this);
            if (($pos = strrpos($class, '\')) !== false) {
                $this->controllerNamespace = substr($class, 0, $pos) . '\controllers';
            }
        }
    }
原文地址:https://www.cnblogs.com/xwzj/p/5444952.html