• 学习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 yiiase;
    
    use Yii;
    use yiidiServiceLocator;
    
    /**
     * 模块和应用程序类的基类.
     *
     * 一个模块代表一个子应用程序包含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 <qiang.xue@gmail.com>
     * @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' => 'appcontrollersUserController',
         *   'article' => [
         *      'class' => 'appcontrollersPostController',
         *      '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 "fooar", then the default
         * controller namespace would be "fooarcontrollers".
         *
         * 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

  • 相关阅读:
    Activiti流程实例
    Activiti使用步骤 (IDEA)
    工作流简介
    并发编程常见面试题
    navicat自动备份数据,可实现每天定时备份
    MySQL, SQLite 和 PostgreSQL 关于information_schema 的一些查询(表结构,表信息,库信息....)
    GIS相关网站下载全国的GeoJSON格式数据可以到区县级数据
    GIS当中使用uDig打开shp图层,并查看数据结果
    解决Failed to execute goal org.apache.maven.plugins 错误,JDK版本不一致
    Maven插件maven-assembly-plugin进行打包部署
  • 原文地址:https://www.cnblogs.com/xwzj/p/5410972.html
Copyright © 2020-2023  润新知