Module类,属性的注释和构造函数的注释:

 <?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 array $aliases 别名数组  只读
  * @property string $basePath The root directory of the module.
  * @property string $basePath  模块的根路径
  * @property string $controllerPath The directory that contains the controller classes. This property is
  * read-only.
  * @property string $controllerPath 控制的路径数组 只读
  * @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts".
  * @property string $layoutPath 模板路径数组 只读
  * @property array $modules The modules (indexed by their IDs).
  * @property array $modules 模块数组
  * @property string $uniqueId The unique ID of the module. This property is read-only.
  * @property string $uniqueId 模块的唯一ID 只读
  * @property string $viewPath The root directory of view files. Defaults to "[[basePath]]/views".
  * @property string $viewPath 模块下的视图文件路径
  *
  * @author Qiang Xue <qiang.xue@gmail.com>
  * @since 2.0
  */
 class Module extends ServiceLocator
 {
     /**
      * @event ActionEvent an event raised before executing a controller action.
      * @event ActionEvent Action事件,在执行控制的的action方法时触发
      * You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
      */
     const EVENT_BEFORE_ACTION = 'beforeAction';
     /**
      * @event ActionEvent an event raised after executing a controller action.
      * @event ActionEvent Action事件,在执行控制的的action方法后触发
      */
     const EVENT_AFTER_ACTION = 'afterAction';

     /**
      * @var array custom module parameters (name => value).
      * @var array 自定义模块参数
      */
     public $params = [];
     /**
      * @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
      * @var string 模块的唯一ID,用于区分同一父模块下的模块
      */
     public $id;
     /**
      * @var Module the parent module of this module. Null if this module does not have a parent.
      * @var 当前模块的父模块
      */
     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.
      * @var string|boolean 布局文件
      */
     public $layout;
     /**
      * @var array mapping from controller ID to controller configurations.
      * @var array 控制器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,
      * 每一个键值对指定一个单独的控制器,控制器配置可以是一个字符串或者数组,如果是前者,该字符串是指定控制的的全路径(即带命名空间的)
      * 如果是后者,则数组中包含一个‘class’元素,该元素指定控制器的全路径,其余的参数用于初始化对应的属性,例子如下:
      *
      * ```php
      * [
      *   'account' => 'app\controllers\UserController',//字符串形式,指定了accout控制器的路径
      *   'article' => [
      *      'class' => 'app\controllers\PostController',//指定article控制器的路径
      *      'pageTitle' => 'something new',//提供了pageTitle参数
      *   ],
      * ]
      * ```
      */
     public $controllerMap = [];
     /**
      * @var string the namespace that controller classes are in.
      * @var string 控制器的命名空间
      * 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`构成的子命名空间
      * 如当前模块的命名空间为"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'.
      * @var string 当前模块的默认路由 默认为'default'
      * The route may consist of child module ID, controller ID, and/or action ID.
      * route 可能包含子模块ID,控制器ID,和/或 action ID,如果action ID未给定,则会调用[Controller::defaultAction]指定的action
      * 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]].
      */
     public $defaultRoute = 'default';

     /**
      * @var string the root directory of the module.
      * @var string 当前模块的根路径
      */
     private $_basePath;
     /**
      * @var string the root directory that contains view files for this module
      * @var string 当前模块下视图文件的路径数组
      */
     private $_viewPath;
     /**
      * @var string the root directory that contains layout view files for this module.
      * @var string 当前模块下的布局文件路径数组
      */
     private $_layoutPath;
     /**
      * @var array child modules of this module
      * @var array 当前模块的子模块数组
      */
     private $_modules = [];

     /**
      * Constructor.
      * 构造函数
      * @param string $id the ID of this module
      * @param string $id 当前模块的ID
      * @param Module $parent the parent module (if any)
      * @param Module $parent 当前模块的父模块(如果有的话)
      * @param array $config name-value pairs that will be used to initialize the object properties
      * @param array $config 配置文件|数组
      */
     public function __construct($id, $parent = null, $config = [])
     {
         $this->id = $id;//给当前模块赋值唯一ID
         $this->module = $parent;//给module属性(即当前模块的父模块)赋值
         parent::__construct($config);//将配置文件赋值到Object的属性中,因为Object是所有类的基类,理论可以整个项目中访问
     }

     /**
      * Returns the currently requested instance of this module class.
      * 从Yii::$app->loadedModules['yii\web\Application']数组中取得当前类的实例
      * If the module class is not currently requested, null will be returned.
      * 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.
      * 将当前类名和存储类的对象变量加入Yii::$app->loadedModules['yii\web\Application']数组中
      * 这样直接通过Yii::$app->loadedModules['yii\web\Application']就可以直接调用这个类
      * @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.
      */
     public static function setInstance($instance)//module模块里会用到,为getInstance提供数据
     {
         if ($instance === null) {
             unset(Yii::$app->loadedModules[get_called_class()]);//如果没有传入参数,说明是删除,直接unset
         } else {
             Yii::$app->loadedModules[get_class($instance)] = $instance;//否则将该类和类的实例存入loadedModules数组中
         }
     }

     /**
      * 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.
      *
      * If you override this method, please make sure you call the parent implementation.
      */
     public function init()
     {
         if ($this->controllerNamespace === null) {//判断controllerNamespace属性是否被赋值,没有赋值才执行
             $class = get_class($this);//获取类名
             if (($pos = strrpos($class, '\\')) !== false) {
                 $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers';//取得命名空间
             }
         }
     }

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

  1. Yii源码阅读笔记(十二)

    Action类,控制器中方法的基类: namespace yii\base; use Yii; /** * Action is the base class for all controller ac ...

  2. Yii源码阅读笔记(十八)

    View中的查找视图文件方法和渲染文件方法 /** * Finds the view file based on the given view name. * 通过view文件名查找view文件 * ...

  3. Yii源码阅读笔记(十五)

    Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...

  4. Yii源码阅读笔记(十六)

    Model类,集中整个应用的数据和业务逻辑—— /** * Generates a user friendly attribute label based on the give attribute ...

  5. Yii源码阅读笔记(十四)

    Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: /** * Returns a list of scenarios and the corresponding active attr ...

  6. Yii源码阅读笔记(十)

    控制器类,所有控制器的基类,用于调用模型和布局,输出到视图 namespace yii\base; use Yii; /** * Controller is the base class for cl ...

  7. Yii源码阅读笔记(十九)

    View中渲染view视图文件的前置和后置方法,以及渲染动态内容的方法: /** * @return string|boolean the view file currently being rend ...

  8. Yii源码阅读笔记(一)

    今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...

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

    web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...

  10. Yii源码阅读笔记(二十九)

    动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...

随机推荐

  1. 微软开放WP开发者回复用户应用评论功能

    1   4月18日,据The NextWeb网站报道,微软今天公布了一项新的开发者试点项目:回复Windows Phone应用评论.该公司表示,它们将在本周推出这项功能,不过目前仅对部分开发者开放. ...

  2. iOS 注意事项

    1.记得在项目中设置项目文件命名的prefix,避免命名冲突. 2.在适当的位置对属性和方法进行注释,建议利用插件(如VVDocument)提供效率.在给企业看文档时,可以利用(如Doxygen)这种 ...

  3. 疯狂java学习笔记之面向对象(九) - 抽象和接口

    一.抽象(abstract): 1.抽象类: 使用abstract修饰的类就是抽象类; 相比于普通类抽象类增加了支持抽象方法的功能,但也丢失了创建实例的功能(抽象类中不能创建实例),其他普通类有的抽象 ...

  4. Java类加载

    类的生命周期是: 在一个类编译完成之后,下一步就需要开始使用类,如果要使用一个类,肯定离不开JVM.在程序执行中JVM通过装载,链接,初始化这3个步骤完成. 类的装载是通过类加载器完成的,加载器将.c ...

  5. java线程详解

    Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程 ...

  6. POJ 1222 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1222 题目大意:一堆开关,或开或关.每个开关按下后,周围4个方向开关反转.问使最后所有开关都关闭的,开关按法.0表示不按,1表示按. ...

  7. ACM 喷水装置(一)

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...

  8. nodejs之async异步编程

    1.什么是异步编程? 异步编程是指由于异步I/O等因素,无法同步获得执行结果时, 在回调函数中进行下一步操作的代码编写风格,常见的如setTimeout函数.ajax请求等等. 示例:  for (v ...

  9. 接口测试之soupui&groovy

    原著地址:http://www.cnblogs.com/wade-xu/p/4236295.html#3334654 需注意下方code的设置

  10. Sortable Observable Collection in C#

    Sorting outside the collection protected override void OnNavigatedTo(NavigationEventArgs e) { if (Se ...