Module类是模块和应用类的基类。  yiisoft\yii2\base\Module.php

 <?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/ namespace yii\base; use Yii;
use yii\di\ServiceLocator; /**
* Module is the base class for module and application classes.
* Module是模块和应用类的基类
* A module represents a sub-application which contains MVC elements by itself, such as
* models, views, controllers, etc.
* 模块是一个由模型、视图、控制器等组成的子应用
* A module may consist of [[modules|sub-modules]].
* 模块内也可以包含模块或子模块
* [[components|Components]] may be registered with the module so that they are globally
* accessible within the module.
* 组件可以注册到模块,以便在模块内全局访问
* @property 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. See [[setAliases()]] for an example.
* This property is write-only. 要定义的别名路径数组 只写
* @property string $basePath The root directory of the module. 模块的根路径
* @property string $controllerPath The directory that contains the controller classes. This property is
* read-only. 控制器类的路径 只读
* @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts".
* 模板路径数组 只读
* @property array $modules The modules (indexed by their IDs). 模块数组
* @property string $uniqueId The unique ID of the module. This property is read-only.模块的唯一标识 只读
* @property string $viewPath The root directory of view files. Defaults to "[[basePath]]/views".
* 模块下视图文件路径
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Module extends ServiceLocator
{
/**
* @event ActionEvent an event raised before executing a controller action. 在执行控制的的action方法前触发
* You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
* 可以设置[[ActionEvent::isValid]]为false取消行动的执行。
*/
const EVENT_BEFORE_ACTION = 'beforeAction';
/**
* @event ActionEvent an event raised after executing a controller action.
* 在执行控制的的action方法后触发
*
*/
const EVENT_AFTER_ACTION = 'afterAction'; /**
* @var array custom module parameters (name => value). 自定义模块参数
*/
public $params = [];
/**
* @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
* 模块的唯一标识,用于区分同一父模块下的模块
*/
public $id;
/**
* @var Module the parent module of this module. Null if this module does not have a parent.
* 当前模块的父模块
*/
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.
* 布局文件 如果没有设置,调用 [[module|parent module]]的值。如果是false,在模块中布局将被禁用。
*/
public $layout;
/**
* @var array mapping from controller ID to controller configurations. 控制器ID到控制器配置的映射
* Each name-value pair specifies the configuration of a single controller.
* A controller configuration can be either a string or an array.
* If the former, the string should be the fully qualified class name of the controller.
* If the latter, the array must contain a 'class' element which specifies
* the controller's fully qualified class name, and the rest of the name-value pairs
* in the array are used to initialize the corresponding controller properties. For example,
* 每个键值对指定单独的控制器,控制器配置可以是字符串或者数组,如果是前者,该字符串是指定控制的的全路径
95 * 如果是后者,则包含一个‘class’元素指定控制器的全路径,其余的参数用于初始化对应的属性
* ~~~
* [
* 'account' => 'app\controllers\UserController',
* 'article' => [
* 'class' => 'app\controllers\PostController',
* 'pageTitle' => 'something new',
* ],
* ]
* ~~~
*/
public $controllerMap = [];
/**
* @var string the namespace that controller classes are in. 控制器的命名空间
* This namespace will be used to load controller classes by prepending it to the controller
* 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 "foo\bar", then the default
* controller namespace would be "foo\bar\controllers".
* 如果没有设置,默认为当前模块的命名空间加上 `controllers`构成的命名空间
119 * 如当前模块的命名空间为"foo\bar",控制器的默认命名空间为"foo\bar\controllers"
* See also the [guide section on autoloading](guide:concept-autoloading) to learn more about
* defining namespaces and how classes are loaded.
*/
public $controllerNamespace;
/**
* @var string the default route of this module. Defaults to 'default'. 当前前模块的默认路由
* The route may consist of child module ID, controller ID, and/or action 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]].
* route 可能包含子模块ID,控制器ID,操作ID,如果action ID未给定,会调用[Controller::defaultAction]指定的action
*/
public $defaultRoute = 'default'; /**
* @var string the root directory of the module. 当前模块的根路径
*/
private $_basePath;
/**
* @var string the root directory that contains view files for this module 当前模块下视图文件的路径
*/
private $_viewPath;
/**
* @var string the root directory that contains layout view files for this module.
* 当前模块下的布局文件路径
*/
private $_layoutPath;
/**
* @var array child modules of this module 当前模块的子模块数组
*/
private $_modules = []; /**
* Constructor. 构造函数
* @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); //调用父类的配置
} /**
* Returns the currently requested instance of this module class. 取得当前类的实例
* If the module class is not currently requested, null will be returned.
* 没有当前请求的模块类,将返回null。
* 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;
} /**
* Sets the currently requested instance of this module class. 设置模块类的当前请求实例。
* @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.
* 当前模块类的实例。如果为null,调用类的实例将被删除
*/
public static function setInstance($instance)
{
if ($instance === null) {//如果没有传入参数,直接unset
unset(Yii::$app->loadedModules[get_called_class()]);
} else {//将该类和类的实例存入loadedModules数组中
Yii::$app->loadedModules[get_class($instance)] = $instance;
}
} /**
* Initializes the module.
* 初始化模块
* This method is called after the module is created and initialized with property values
* given in configuration. The default implementation will initialize [[controllerNamespace]]
* if it is not set.
* 该模块创建和初始化给出的配置 如果没有设置,默认初始化[[controllerNamespace]]
* 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, , $pos) . '\\controllers'; //取得命名空间
}
}
} /**
* 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
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;
} /**
* 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;//是目录名且不为false,返回目录名,否则抛出异常
} else {
throw new InvalidParamException("The directory does not exist: $path");
}
}

yii2源码学习笔记(十四)的更多相关文章

  1. yii2源码学习笔记(十九)

    view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...

  2. yii2源码学习笔记(十二)

    继续了解controller基类. /** * Runs a request specified in terms of a route.在路径中指定的请求. * The route can be e ...

  3. yii2源码学习笔记(十)

    继续了解Application. /** * Registers the errorHandler component as a PHP error handler. * 注册errorHandler ...

  4. yii2源码学习笔记(十六)

    Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...

  5. yii2源码学习笔记(十五)

    这几天有点忙今天好些了,继续上次的module来吧 /** * Returns the directory that contains the controller classes according ...

  6. async-validator 源码学习笔记(四):validator

    系列文章: 1.async-validator 源码学习(一):文档翻译 2.async-validator 源码学习笔记(二):目录结构 3.async-validator 源码学习笔记(三):ru ...

  7. yii2源码学习笔记(九)

    Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...

  8. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  9. 老刘 Yii2 源码学习笔记之 Action 类

    Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...

随机推荐

  1. 【转】C++中继承中的同名成员问题

    C++中,子类若有与父类同名的成员变量和成员函数,则同名的成员变量相互独立,但同名的子类成员函数重载父类的同名成员函数.举例如下: #include <iostream> using na ...

  2. 数字信号处理与音频处理(使用Audition)

    前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...

  3. NoSQL 数据库产品学习总结(一)

    NoSQL 数据库产品学习总结(一) 本篇文章共分为四个章节,会陆续整理下 Memcached.Redis.tair.mongodb.hbase.SequoiaDB. Cassandra的相关知识. ...

  4. [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2

    Aadd watch to the config.json file: { "apps": [{ "name": "App1", " ...

  5. Android(java)学习笔记165:Android的Junit调试

    编写android应用的时候,往往我们需要编写一些业务逻辑实现类,但是我们可能不能明确这个业务逻辑是否可以成功实现,特别是逻辑代码体十分巨大的时候,我们不可能一行一行检查自己的代码,为了解决这样的问题 ...

  6. EasilyUI的一个简单的拖拽功能

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs&quo ...

  7. warning(包括PHP中的notice)必须改吗?

    作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...

  8. 网页设定固定背景图片(抄袭自百度FM)

    这个新技能,我是从百度FM中学习到的. 在网页中,有一个id为"body-bg"的层, html代码: <div id="body-bg" style=& ...

  9. (转)HttpHandler与HttpModule的理解与应用

    神秘的HttpHandler与HttpModule 大学时候我是从拖控件开始学习 asp.net的,对.net的很多类库对象都不是很了解.所以看到大家写一些个性的asp.net名词,就感觉asp.ne ...

  10. android中实现“再按一次退出”功能

    首先,定义两次点击退出按钮的时间间隔:private static final long INTERNAL_TIME=2000; 然后,定义一个当前时间的变量:private long exitTim ...