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

vendor/yiisoft/yii2/base/Module. php

    /**
     * 返回一个ID,惟一标识此模块在所有模块在当前应用程序.
     * @return string the unique ID of the module.
     */
    public function getUniqueId()
    {   //如果该模块是一个应用程序,将返回一个空字符串.
        return $this->module ? ltrim($this->module->getUniqueId() . '/' . $this->id, '/') : $this->id;
    }

    /**
     * Returns the root directory of the module.
     * It defaults to the directory containing the module class file.
     * @return string the root directory of the module.
     */
    public function getBasePath()
    {   //返回模块的根目录
        if ($this->_basePath === null) {
            $class = new ReflectionClass($this);
            $this->_basePath = dirname($class->getFileName());
        }

        return $this->_basePath;
    }

    /**
     * 设置模块的根目录.
     * 这种方法只能在开始调用构造函数的使用
     * @param string $path the root directory of the module. This can be either a directory name or a path alias.
     * @throws InvalidParamException if the directory does not exist.
     */
    public function setBasePath($path)
    {
        $path = Yii::getAlias($path);
        // realpath — 返回规范化的绝对路径名
        $p = realpath($path);
        if ($p !== false && is_dir($p)) {
            // 路径存在,且是一个目录,就将它存到当前对象的$_basePath中
            $this->_basePath = $p;
        } else {
            throw new InvalidParamException("The directory does not exist: $path");
        }
    }

    /**
     * 返回目录包含控制器类根据[[controllerNamespace]]
     * @return string the directory that contains the controller classes.
     * @throws InvalidParamException if there is no alias defined for the root namespace of [[controllerNamespace]].
     */
    public function getControllerPath()
    {   //如果没有别名为的根名称空间就返回定义[[controllerNamespace]]。
        return Yii::getAlias('@' . str_replace('\', '/', $this->controllerNamespace));
    }


    /**
     * 集包含视图的目录文件.
     * @param string $path the root directory of view files.
     * @throws InvalidParamException if the directory is invalid
     */
    public function setViewPath($path)
    {
        $this->_viewPath = Yii::getAlias($path);
    }


    /**
     * 定义了路径别名.
     * 这个方法调用[[Yii:setAlias()]]注册路径别名.
     * 这种方法提供,这样您就可以定义路径别名配置模块.
     * @property array list of path aliases to be defined. The array keys are alias names
     * (must start with '@') and the array values are the corresponding paths or aliases.
     * See [[setAliases()]] for an example.
     * @param array $aliases list of path aliases to be defined. The array keys are alias names
     * (must start with '@') and the array values are the corresponding paths or aliases.
     * For example,
     *
     * ~~~
     * [
     *     '@models' => '@app/models', // an existing alias
     *     '@backend' => __DIR__ . '/../backend',  // a directory
     * ]
     * ~~~
     */
    public function setAliases($aliases)
    {
        foreach ($aliases as $name => $alias) {
            Yii::setAlias($name, $alias);
        }
    }

    /**
     * Checks whether the child module of the specified ID exists.
     * This method supports checking the existence of both child and grand child modules.
     * @param string $id module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
     * @return boolean whether the named module exists. Both loaded and unloaded modules
     * are considered.
     */
    public function hasModule($id)
    {
        if (($pos = strpos($id, '/')) !== false) {
            // 检查是否指定的子模块ID存在
            $module = $this->getModule(substr($id, 0, $pos));

            return $module === null ? false : $module->hasModule(substr($id, $pos + 1));
        } else {
            return isset($this->_modules[$id]);
        }
    }
原文地址:https://www.cnblogs.com/xwzj/p/5453655.html