学习yii2.0框架阅读代码(十)

vendor/yiisoft/yii2/base/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;

/**
 * 模块和应用程序类的基类.
 *
 * 一个模块代表一个子应用程序包含MVC元素本身
 * 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 <[email protected]>
 * @since 2.0
 */
class Module extends ServiceLocator
{
    /**
     * @event 在执行ActionEvent方法时触发事件
     * 你可以设置[[ActionEvent:isValid]]是错误的取消操作执行。.
     */
    const EVENT_BEFORE_ACTION = ‘beforeAction‘;

    const EVENT_AFTER_ACTION = ‘afterAction‘;

    /**
     * @var array custom module parameters (name => value).
     */
    public $params = [];
    /**
     * @var 控制器ID
     */
    public $id;
    /**
     * @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.
     */
    public $layout;
    /**
     *  @var 数组从控制器ID映射到控制器配置
     * 每一个名称-值对将指定一个控制器的配置.
     * 一个控制器配置可以是一个字符串或一个数组
     * 如果是前者,字符串应该是控制器的完全限定类名.
     * 如果是后者,数组元素必须包含一个“类”
     * the controller‘s fully qualified class name, and the rest of the name-value pairs
     * 例子,
     *
     * ~~~
     * [
     *   ‘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".
     *
     * 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]].
     */
    public $defaultRoute = ‘default‘;

    /**
     * @var 字符串模块的根目录.
     */
    private $_basePath;
    /**
     * @var 字符串模块的根目录
     */
    private $_viewPath;
    /**
     * @var 字符串模块的根目录.
     */
    private $_layoutPath;
    /**
     * @var 这个模块所属的子模块
     */
    private $_modules = [];

    /**
     * 定义一个构造函数.
     * @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);
    }

    /**
     * 返回当前请求的这个模块类的实例
     * 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.
     * @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)
    {
        // get_called_class和get_class获取的值都是带namespace的
        if ($instance === null) {
            // 如果实例不存在,就将其从$app的loadedModules移出掉
            unset(Yii::$app->loadedModules[get_called_class()]);
        } else {
            // 如果实例存在,就将其加入到$app的loadedModules里
            Yii::$app->loadedModules[get_class($instance)] = $instance;
        }
    }

    /**
     * 初始化模块.
     *
     * 调用此方法后,模块与属性值创建并初始化
     * 在配置。的默认实现将初始化[[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) {
            $class = get_class($this);
            if (($pos = strrpos($class, ‘\\‘)) !== false) {
                $this->controllerNamespace = substr($class, 0, $pos) . ‘\\controllers‘;
            }
        }
    }

php

时间: 2024-08-03 07:00:38

学习yii2.0框架阅读代码(十)的相关文章

学习yii2.0框架阅读代码(十五)

行为是 yii\base\Behavior 或其子类的实例.行为,也称为mixins,可以无须改变类继承关系即可增强一个已有的 yii\base\Component 类功能.当行为附加到组件后,它将“注入”它的方法和属性到组件,然后可以像访问组件内定义的方法和属性一样访问它们.此外,行为通过组件能响应被触发的事件,从而自定义或调整组件正常执行的代码. <?php namespace yii\base; /** * 行为是所有行为类的基类. * * 一个行为可以用来增强现有的功能组件,无需修改其代

学习yii2.0框架阅读代码(十九)

vendor/yiisoft/yii2/base/Module. php(续) /** * 检索指定的子模块ID. * 这种方法支持检索两个子模块和子模块. * @param string $id module ID (case-sensitive). To retrieve grand child modules, * use ID path relative to this module (e.g. `admin/content`). * @param boolean $load wheth

学习yii2.0框架阅读代码(十八)

vendor/yiisoft/yii2/base/Module. php /** * 返回一个ID,惟一标识此模块在所有模块在当前应用程序. * @return string the unique ID of the module. */ public function getUniqueId() { //如果该模块是一个应用程序,将返回一个空字符串. return $this->module ? ltrim($this->module->getUniqueId() . '/' . $t

学习yii2.0框架阅读代码(十二)

先把Object.Component.Module三个核心搞清楚了在写实例 下面介绍一下Object -- Yii最基础的类,大多数类都继承了该类.常用的12个公共方法,有点类似于ThinkPHP里面的魔术方法. <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/

学习yii2.0框架阅读代码(十六)

yii\base\InlineAction 追踪一个命令行请求的过程 namespace yii\base; use Yii; /** * InlineAction表示一个动作被定义为一个控制器的方法. * * The name of the controller method is available via [[actionMethod]] which * is set by the [[controller]] who creates this action. * * @author Qi

学习yii2.0框架阅读代码(十四)

组件(Component) 事件Event(温习) <?php namespace yii\base; //事件是所有事件类的基类.它封装了参数与事件相关联. //如果一个事件处理程序集[[进行]]是真的,其余的,uninvoked处理程序将不再被称为处理事件. //另外,添加一个事件处理程序时,额外的数据可能被传递和可以通过[[数据]]属性调用事件处理程序时. class Event extends Object { /** * @var string the event name. This

学习yii2.0框架阅读代码(二十)

vendor/yiisoft/yii2/base/Module. php(续) /** * 新建一个控制器实例基于给定的路线. * * 路线应该是相对于这个模块.该方法实现了以下算法 * to resolve the given route: * * 1. If the route is empty, use [[defaultRoute]]; * 2. If the first segment of the route is a valid module ID as declared in [

学习yii2.0框架阅读代码(四)

阅读 BaseYii Yii的辅助类核心框架 别名相关(续) //用一个真实的路径注册一个别名 public static function setAlias($alias, $path) { if (strncmp($alias, '@', 1)) { // 如果不是以 @ 开头,就将 @ 拼到开头 $alias = '@' . $alias; } // 获取 / 在 $alias 中首次出现的位置 $pos = strpos($alias, '/'); // 如果 / 不存在,$root 就

学习yii2.0框架阅读代码(十七)

vendor/yiisoft/yii2/base/Module. 模块类 每个模块都有一个继承yii\base\Module的模块类,该类文件直接放在模块的yii\base\Module::basePath目录下, 并且能被自动加载.当一个模块被访问,和应用主题实例类似会创建该模块类唯一实例,模块实例用来帮模块内代码共享数据和组件 class Module extends ServiceLocator { /** * @event 在执行ActionEvent方法时触发事件 * 你可以设置[[A