標籤:
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
學習yii2.0架構閱讀代碼(十)