Yii源码阅读笔记(二十三)
Module类中的辅助功能方法:
/**
* Returns an ID that uniquely identifies this module among all modules within the current application.
* 返回模块的唯一标识
* Note that if the module is an application, an empty string will be returned.
* @return string the unique ID of the module.
*/
public function getUniqueId()
{
//三元运算符,如果当前模块有父模块,则返回[父模块ID/当前模块ID]的格式作为唯一ID,否则只返回当前模块ID
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());//getFileName()取得类定义的路径
}
return $this->_basePath;
}
/**
* Sets the root directory of the module.
* 设置当前模块的根路径
* This method can only be invoked at the beginning of the constructor.
* @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);
$p = realpath($path);
if ($p !== false && is_dir($p)) {
$this->_basePath = $p;
} else {
throw new InvalidParamException("The directory does not exist: $path");
}
}
/**
* Returns the directory that contains the controller classes according to [[controllerNamespace]].
* 根据控制器的命名空间返回控制器的目录路径
* Note that in order for this method to return a value, you must define
* an alias for the root namespace of [[controllerNamespace]].
* 为了使该方法返回正确的值,必须为[[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()
{
//通过将命名空间转换为路径构造别名路径,然后通过getAlias方法取得控制器的绝对路径
return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
}
/**
* Returns the directory that contains the view files for this module.
* 取得当前模块的视图文件目录路径
* @return string the root directory of view files. Defaults to "[[basePath]]/views".
*/
public function getViewPath()
{
if ($this->_viewPath === null) {
$this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';//getBasePath()返回当前模块的根路径,然后拼接出视图文件路径
}
return $this->_viewPath;
}
/**
* Sets the directory that contains the view files.
* 设置视图文件目录路径
* @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);
}
/**
* Returns the directory that contains layout view files for this module.
* 取得当前模块的布局文件路径
* @return string the root directory of layout files. Defaults to "[[viewPath]]/layouts".
*/
public function getLayoutPath()
{
if ($this->_layoutPath === null) {
$this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';//getBasePath()返回当前模块的根路径,然后拼接出布局文件目录路径
}
return $this->_layoutPath;
}
/**
* Sets the directory that contains the layout files.
* 设置当前模块的布局文件路径
* @param string $path the root directory or path alias of layout files.
* @throws InvalidParamException if the directory is invalid
*/
public function setLayoutPath($path)
{
$this->_layoutPath = Yii::getAlias($path);
}
/**
* Defines path aliases.
* 定义路径别名
* This method calls [[Yii::setAlias()]] to register the path aliases.
* This method is provided so that you can define path aliases when configuring a module.
* 通过调用[[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,
* 传入测参数的格式,键名为别名名称,以@开始,键值为对应的路径
* ```php
* [
* '@models' => '@app/models', // an existing alias
* '@backend' => __DIR__ . '/../backend', // a directory
* ]
* ```
*/
public function setAliases($aliases)
{
foreach ($aliases as $name => $alias) {
Yii::setAlias($name, $alias);//调用[[Yii::setAlias()]]注册路径别名
}
}
/**
* 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格式为 `admin/content`
// sub-module
$module = $this->getModule(substr($id, 0, $pos));//取出当前模块的子模块
return $module === null ? false : $module->hasModule(substr($id, $pos + 1));//如果没有取到,返回false,否则判断子模块的子模块
} else {//模块ID没有父模块的情况,直接判断_modules数组中是否有值
return isset($this->_modules[$id]);
}
}
Yii源码阅读笔记(二十三)的更多相关文章
- Yii源码阅读笔记(十三)
Model类,集中整个应用的数据和业务逻辑: namespace yii\base; use Yii; use ArrayAccess; use ArrayObject; use ArrayItera ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- werkzeug源码阅读笔记(二) 下
wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...
- werkzeug源码阅读笔记(二) 上
因为第一部分是关于初始化的部分的,我就没有发布出来~ wsgi.py----第一部分 在分析这个模块之前, 需要了解一下WSGI, 大致了解了之后再继续~ get_current_url()函数 很明 ...
- Detectron2源码阅读笔记-(二)Registry&build_*方法
Trainer解析 我们继续Detectron2代码阅读笔记-(一)中的内容. 上图画出了detectron2文件夹中的三个子文件夹(tools,config,engine)之间的关系.那么剩下的 ...
- Yii源码阅读笔记(二)
接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...
- Yii源码阅读笔记(三十三)
ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: namespace yii\di; use Yii; use Cl ...
- Yii源码阅读笔记(三十二)
web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...
- Yii源码阅读笔记(二十九)
动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...
随机推荐
- Redis执行Lua脚本示例
Redis在2.6推出了脚本功能,允许开发者使用Lua语言编写脚本传到Redis中执行.使用脚本的好处如下: 1.减少网络开销:本来5次网络请求的操作,可以用一个请求完成,原先5次请求的逻辑放在red ...
- static方法中为什么使用的都是静态变量
static方法中需要使用静态变量.假设其他类要使用该方法,该方法里面所使用的变量是非静态的,如果该方法所在的类没有实例化,会导致该方法里面的变量不能实例化,自然该static 方法不能使用
- 【原】iOS学习18之OC内存管理高级
1.属性的内存管理 1> 属性的语义特性 2> assign下的属性内部实现 @property (nonatomic, assign) NSString *name; @synthesi ...
- BZOJ3424 : Poi2013 Multidrink
详细做法以及证明请看论文<Hamiltonian paths in the square of a tree>. 首先将1到n的路径提取出来,作为主干. 定义毛毛虫为去掉叶子之后只有一条单 ...
- 争夺 & KM思想
题意: 给一张二分图,每个点与两个特定点又一条边相连,边权非负,让你给这个二分图每个点一个顶标,让每一条边两端顶标和大于等于这条边.求出最小顶标和. 这当然是翻译过的题目... 原题: 小Y和小P无聊 ...
- android NDK入门 windows下安装cygwin
一.Android NDK环境简介 Android NDK 是运行于Android 平台上的Native Development Kit 的缩写. Android 应用开发者可以通过NDK 调用C 或 ...
- [题解+总结]NOIP2015模拟题2
// 此博文为迁移而来,写于2015年7月22日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w72i.html 1.总 ...
- [WP8.1UI控件编程]Windows Phone理解和运用ItemTemplate、ContentTemplate和DataTemplate
2.2.5 ItemTemplate.ContentTemplate和DataTemplate 在理解ItemTemplate.ContentTemplate和DataTemplate的关系的之前,我 ...
- 4分钟了解nano编辑器和简单命令 2015.10.6
nano感觉并不常用,但是偶尔遇到过几次. nano命令是一个类似VI的编辑器,但是更简单,其中的i,a等命令似乎可以用,但是有些命令不可以用.保存和退出最大的区别在于退出方式,如果你要保存所做的修改 ...
- BZOJ 3211 题解
3211: 花神游历各国 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2549 Solved: 946[Submit][Status][Discus ...