• yii2源码学习笔记(十五)


    这几天有点忙今天好些了,继续上次的module来吧

      1     /**
      2      * Returns the directory that contains the controller classes according to [[controllerNamespace]].
      3      *根据控制器的命名空间返回控制器的目录路径
      4      * Note that in order for this method to return a value, you must define
      5      * an alias for the root namespace of [[controllerNamespace]].
      6      * 为了使该方法返回正确的值,必须为[[controllerNamespace]]定义一个根别名
      7      * @return string the directory that contains the controller classes.
      8      * @throws InvalidParamException if there is no alias defined for the root namespace of [[controllerNamespace]].
      9      */
     10     public function getControllerPath()
     11     {
     12         //通过将命名空间转换为路径构造别名路径,然后通过getAlias方法取得控制器的绝对路径
     13         return Yii::getAlias('@' . str_replace('\', '/', $this->controllerNamespace));
     14     }
     15 
     16     /**
     17      * Returns the directory that contains the view files for this module.
     18      * 取得当前模块的视图文件目录路径
     19      * @return string the root directory of view files. Defaults to "[[basePath]]/views".
     20      */
     21     public function getViewPath()
     22     {
     23         if ($this->_viewPath !== null) {
     24             return $this->_viewPath;
     25         } else {
     26             //getBasePath()返回当前模块的根路径,然后拼接出视图文件路径
     27             return $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';
     28         }
     29     }
     30 
     31     /**
     32      * Sets the directory that contains the view files.设置视图文件目录路径
     33      * @param string $path the root directory of view files.
     34      * @throws InvalidParamException if the directory is invalid
     35      */
     36     public function setViewPath($path)
     37     {
     38         $this->_viewPath = Yii::getAlias($path);
     39     }
     40 
     41     /**
     42      * Returns the directory that contains layout view files for this module.
     43      * 取得当前模块的布局文件路径
     44      * @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts".
     45      */
     46     public function getLayoutPath()
     47     {
     48         if ($this->_layoutPath !== null) {
     49             return $this->_layoutPath;
     50         } else {//getBasePath()返回当前模块的根路径,然后拼接出布局文件目录路径
     51             return $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';
     52         }
     53     }
     54 
     55     /**
     56      * Sets the directory that contains the layout files.设置当前模块的布局文件路径
     57      * @param string $path the root directory or path alias of layout files.
     58      * @throws InvalidParamException if the directory is invalid
     59      */
     60     public function setLayoutPath($path)
     61     {
     62         $this->_layoutPath = Yii::getAlias($path);
     63     }
     64 
     65     /**
     66      * Defines path aliases. 定义路径别名
     67      * This method calls [[Yii::setAlias()]] to register the path aliases.
     68      * This method is provided so that you can define path aliases when configuring a module.
     69      * 通过调用[[Yii::setAlias()]]注册路径别名,方便在配置模块的时候定义路径别名 
     70      * @property array list of path aliases to be defined. The array keys are alias names
     71      * (must start with '@') and the array values are the corresponding paths or aliases.
     72      * See [[setAliases()]] for an example.
     73      * @param array $aliases list of path aliases to be defined. The array keys are alias names
     74      * (must start with '@') and the array values are the corresponding paths or aliases.
     75      * For example,
     76      *
     77      * ~~~
     78      * [
     79      *     '@models' => '@app/models', // an existing alias
     80      *     '@backend' => __DIR__ . '/../backend',  // a directory
     81      * ]
     82      * ~~~
     83      */
     84     public function setAliases($aliases)
     85     {
     86         foreach ($aliases as $name => $alias) {
     87             Yii::setAlias($name, $alias);//调用[[Yii::setAlias()]]路径别名
     88         }
     89     }
     90 
     91     /**
     92      * Checks whether the child module of the specified ID exists.
     93      * This method supports checking the existence of both child and grand child modules.
     94      * @param string $id module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
     95      * @return boolean whether the named module exists. Both loaded and unloaded modules
     96      * are considered.
     97      */
     98     public function hasModule($id)
     99     {
    100         if (($pos = strpos($id, '/')) !== false) {
    101             // sub-module   如果模块ID格式为 `admin/content` 取出当前模块的子模块
    102             $module = $this->getModule(substr($id, 0, $pos));
    103             //如果没有取到,返回false
    104             return $module === null ? false : $module->hasModule(substr($id, $pos + 1));
    105         } else {
    106             //模块ID没有父模块的情况,直接判断_modules数组中是否有值
    107             return isset($this->_modules[$id]);
    108         }
    109     }
    110 
    111     /**
    112      * Retrieves the child module of the specified ID.取出指定模块的子模块
    113      * This method supports retrieving both child modules and grand child modules.
    114      * 该方法支持检索子模块和子模块的子模块
    115      * @param string $id module ID (case-sensitive). To retrieve grand child modules,
    116      * use ID path relative to this module (e.g. `admin/content`).
    117      * 检索子模块,使用标识路径相对
    118      * @param boolean $load whether to load the module if it is not yet loaded. 是否加载模块
    119      * @return Module|null the module instance, null if the module does not exist. 不存在删除
    120      * @see hasModule()
    121      */
    122     public function getModule($id, $load = true)
    123     {
    124         if (($pos = strpos($id, '/')) !== false) {
    125             // sub-module 判断模块id格式是否为 `admin/content`  取子模块
    126             $module = $this->getModule(substr($id, 0, $pos));
    127 
    128             return $module === null ? null : $module->getModule(substr($id, $pos + 1), $load);
    129         }
    130 
    131         if (isset($this->_modules[$id])) {
    132             if ($this->_modules[$id] instanceof Module) {
    133                 return $this->_modules[$id];//如果_modules数组中有该模块,直接返回该模块
    134             } elseif ($load) {//否则,先实例化后返回
    135                 Yii::trace("Loading module: $id", __METHOD__);
    136                 /* @var $module Module */
    137                 $module = Yii::createObject($this->_modules[$id], [$id, $this]);
    138                 $module->setInstance($module);
    139                 return $this->_modules[$id] = $module;
    140             }
    141         }
    142         //不存在,返回null
    143         return null;
    144     }
    145 
    146     /**
    147      * Adds a sub-module to this module. 为当前模块添加子模块
    148      * @param string $id module ID  模块标识
    149      * @param Module|array|null $module the sub-module to be added to this module. This can
    150      * be one of the followings:
    151      *
    152      * - a [[Module]] object
    153      * - a configuration array: when [[getModule()]] is called initially, the array
    154      *   will be used to instantiate the sub-module
    155      * - null: the named sub-module will be removed from this module
    156      */
    157     public function setModule($id, $module)
    158     {
    159         if ($module === null) {
    160             //为空删除
    161             unset($this->_modules[$id]);
    162         } else {//存在则添加
    163             $this->_modules[$id] = $module;
    164         }
    165     }
  • 相关阅读:
    一个链表,奇数位升序偶数位降序,让链表变成升序的
    LeetCode 046 Permutations 全排列
    LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
    LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
    模板实现一个栈(内部使用动态申请的数组作为存储结构)
    004 Median of Two Sorted Arrays 两个有序数组的中位数
    静态链接与动态链接
    sizeof和strlen的区别
    const和define的区别
    lodash
  • 原文地址:https://www.cnblogs.com/dragon16/p/5588931.html
Copyright © 2020-2023  润新知