Yii源码阅读笔记(二十三)

Module类中的辅助功能方法:

  1     /**
  2      * Returns an ID that uniquely identifies this module among all modules within the current application.
  3      * 返回模块的唯一标识
  4      * Note that if the module is an application, an empty string will be returned.
  5      * @return string the unique ID of the module.
  6      */
  7     public function getUniqueId()
  8     {
  9         //三元运算符,如果当前模块有父模块,则返回[父模块ID/当前模块ID]的格式作为唯一ID,否则只返回当前模块ID
 10         return $this->module ? ltrim($this->module->getUniqueId() . ‘/‘ . $this->id, ‘/‘) : $this->id;
 11     }
 12
 13     /**
 14      * Returns the root directory of the module.
 15      * 返回当前模块的根路径
 16      * It defaults to the directory containing the module class file.
 17      * @return string the root directory of the module.
 18      */
 19     public function getBasePath()
 20     {
 21         if ($this->_basePath === null) {
 22             $class = new \ReflectionClass($this);//生成当前类的反射对象
 23             $this->_basePath = dirname($class->getFileName());//getFileName()取得类定义的路径
 24         }
 25
 26         return $this->_basePath;
 27     }
 28
 29     /**
 30      * Sets the root directory of the module.
 31      * 设置当前模块的根路径
 32      * This method can only be invoked at the beginning of the constructor.
 33      * @param string $path the root directory of the module. This can be either a directory name or a path alias.
 34      * @throws InvalidParamException if the directory does not exist.
 35      */
 36     public function setBasePath($path)
 37     {
 38         $path = Yii::getAlias($path);
 39         $p = realpath($path);
 40         if ($p !== false && is_dir($p)) {
 41             $this->_basePath = $p;
 42         } else {
 43             throw new InvalidParamException("The directory does not exist: $path");
 44         }
 45     }
 46
 47     /**
 48      * Returns the directory that contains the controller classes according to [[controllerNamespace]].
 49      * 根据控制器的命名空间返回控制器的目录路径
 50      * Note that in order for this method to return a value, you must define
 51      * an alias for the root namespace of [[controllerNamespace]].
 52      * 为了使该方法返回正确的值,必须为[[controllerNamespace]]定义一个根别名
 53      * @return string the directory that contains the controller classes.
 54      * @throws InvalidParamException if there is no alias defined for the root namespace of [[controllerNamespace]].
 55      */
 56     public function getControllerPath()
 57     {
 58         //通过将命名空间转换为路径构造别名路径,然后通过getAlias方法取得控制器的绝对路径
 59         return Yii::getAlias(‘@‘ . str_replace(‘\\‘, ‘/‘, $this->controllerNamespace));
 60     }
 61
 62     /**
 63      * Returns the directory that contains the view files for this module.
 64      * 取得当前模块的视图文件目录路径
 65      * @return string the root directory of view files. Defaults to "[[basePath]]/views".
 66      */
 67     public function getViewPath()
 68     {
 69         if ($this->_viewPath === null) {
 70             $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . ‘views‘;//getBasePath()返回当前模块的根路径,然后拼接出视图文件路径
 71         }
 72         return $this->_viewPath;
 73     }
 74
 75     /**
 76      * Sets the directory that contains the view files.
 77      * 设置视图文件目录路径
 78      * @param string $path the root directory of view files.
 79      * @throws InvalidParamException if the directory is invalid
 80      */
 81     public function setViewPath($path)
 82     {
 83         $this->_viewPath = Yii::getAlias($path);
 84     }
 85
 86     /**
 87      * Returns the directory that contains layout view files for this module.
 88      * 取得当前模块的布局文件路径
 89      * @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts".
 90      */
 91     public function getLayoutPath()
 92     {
 93         if ($this->_layoutPath === null) {
 94             $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . ‘layouts‘;//getBasePath()返回当前模块的根路径,然后拼接出布局文件目录路径
 95         }
 96
 97         return $this->_layoutPath;
 98     }
 99
100     /**
101      * Sets the directory that contains the layout files.
102      * 设置当前模块的布局文件路径
103      * @param string $path the root directory or path alias of layout files.
104      * @throws InvalidParamException if the directory is invalid
105      */
106     public function setLayoutPath($path)
107     {
108         $this->_layoutPath = Yii::getAlias($path);
109     }
110
111     /**
112      * Defines path aliases.
113      * 定义路径别名
114      * This method calls [[Yii::setAlias()]] to register the path aliases.
115      * This method is provided so that you can define path aliases when configuring a module.
116      * 通过调用[[Yii::setAlias()]]注册路径别名,方便在配置模块的时候定义路径别名
117      * @property array list of path aliases to be defined. The array keys are alias names
118      * (must start with ‘@‘) and the array values are the corresponding paths or aliases.
119      * See [[setAliases()]] for an example.
120      * @param array $aliases list of path aliases to be defined. The array keys are alias names
121      * (must start with ‘@‘) and the array values are the corresponding paths or aliases.
122      * For example,
123      * 传入测参数的格式,键名为别名名称,以@开始,键值为对应的路径
124      * ```php
125      * [
126      *     ‘@models‘ => ‘@app/models‘, // an existing alias
127      *     ‘@backend‘ => __DIR__ . ‘/../backend‘,  // a directory
128      * ]
129      * ```
130      */
131     public function setAliases($aliases)
132     {
133         foreach ($aliases as $name => $alias) {
134             Yii::setAlias($name, $alias);//调用[[Yii::setAlias()]]注册路径别名
135         }
136     }
137
138     /**
139      * Checks whether the child module of the specified ID exists.
140      * This method supports checking the existence of both child and grand child modules.
141      * @param string $id module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
142      * @return boolean whether the named module exists. Both loaded and unloaded modules
143      * are considered.
144      */
145     public function hasModule($id)
146     {
147         if (($pos = strpos($id, ‘/‘)) !== false) {//如果模块ID格式为 `admin/content`
148             // sub-module
149             $module = $this->getModule(substr($id, 0, $pos));//取出当前模块的子模块
150
151             return $module === null ? false : $module->hasModule(substr($id, $pos + 1));//如果没有取到,返回false,否则判断子模块的子模块
152         } else {//模块ID没有父模块的情况,直接判断_modules数组中是否有值
153             return isset($this->_modules[$id]);
154         }
155     }
时间: 2024-08-06 21:13:19

Yii源码阅读笔记(二十三)的相关文章

Yii源码阅读笔记(十三)

Model类,集中整个应用的数据和业务逻辑: 1 namespace yii\base; 2 3 use Yii; 4 use ArrayAccess; 5 use ArrayObject; 6 use ArrayIterator; 7 use ReflectionClass; 8 use IteratorAggregate; 9 use yii\helpers\Inflector; 10 use yii\validators\RequiredValidator; 11 use yii\vali

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Java Jdk1.8 HashMap源码阅读笔记二

三.源码阅读 3.元素包含containsKey(Object key) /** * Returns <tt>true</tt> if this map contains a mapping for the * specified key. * * @param key The key whose presence in this map is to be tested * @return <tt>true</tt> if this map contains

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

Yii源码阅读笔记(二)

接下来阅读BaseYii.php vendor/yiisoft/yii2/Yii.php—— 1 namespace yii; 2 3 use yii\base\InvalidConfigException; 4 use yii\base\InvalidParamException; 5 use yii\base\UnknownClassException; 6 use yii\log\Logger; 7 use yii\di\Container; 第1行定义命名空间为yii: 第3到7行使用了

Yii源码阅读笔记(三十三)

ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: 1 namespace yii\di; 2 3 use Yii; 4 use Closure; 5 use yii\base\Component; 6 use yii\base\InvalidConfigException; 7 8 /** 9 * ServiceLocator implements a [service locator](http://en.wi

Yii源码阅读笔记(二十一)——请求处理流程

Yii2请求处理流程: 首先:项目路径/web/index.php (new yii\web\Application($config))->run();//根据配置文件创建App实例,先实例化yii\web\Application(),然后调用run()方法 该语句可分为两步: $application = new yii\web\Application($config);//实例化app $application->run();//调用run()方法 $config 为配置文件,通过 req

Yii源码阅读笔记(二十六)

Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: 1 /** 2 * Handles the specified request. 3 * 处理指定的请求--抽象方法 4 * This method should return an instance of [[Response]] or its child class 5 * which represents the handling result of the reques

Yii源码阅读笔记(三十二)

web/Application类的注释,继承base/Application类,针对web应用的一些处理: 1 namespace yii\web; 2 3 use Yii; 4 use yii\base\InvalidRouteException; 5 6 /** 7 * Application is the base class for all web application classes. 8 * Application 是所有web应用的基类 9 * 10 * @property st