Yii2 source code learning notes (14th) and yii2 source code learning notes
The Module class is the base class of the Module and application classes. Yiisoft \ yii2 \ base \ Module. php
1 <? Php 2/*** @ link http://www.yiiframework.com/4 * @ copyright Copyright (c) 2008 Yii Software LLC 5 * @ license http://www.yiiframework.com/license/ 6 */7 8 namespace yii \ base; 9 10 use Yii; 11 use yii \ di \ ServiceLocator; 12 13/** 14 * Module is the base class for module and application classes. 15 * Module is the base class of the module and application classes 16 * A Module represents a sub-application which contains MVC elements by itself, Such as 17 * models, views, controllers, etc. 18 * module is A sub-application consisting of models, views, and controllers 19 * A module may consist of [[modules | sub-modules]. 20 * the module can also contain modules or submodules 21 * [[components | Components] may be registered with the module so that they are globally 22 * accessible within the module. 23 * The component can be registered with the module to allow global access within the Module 24 * @ property array $ aliases List of path aliases to be defined. the array keys are alias names (Must start 25 * with '@') and the array values are the corresponding paths or aliases. see [[setAliases ()] for an example. 26 * This property is write-only. only 27 * @ property string $ basePath The root directory of the module is required for The alias path array to be defined. module root path 28 * @ property string $ controllerPath The directory that contains the controller classes. this property is 29 * read-only. controller class path read-only 30 * @ property string $ LayoutPath The root directory of layout files. defaults to "[[viewPath]/layouts ". 31 * template path array read-only 32 * @ property array $ modules The modules (indexed by their IDs ). module array 33 * @ property string $ uniqueId The unique ID of the module. this property is read-only. the unique identifier of The module is read-only 34 * @ property string $ viewPath The root directory of view files. defaults to "[[basePath]/views ". 35 * path of View File under module 36 * @ author Qi Ang Xue <qiang.xue@gmail.com> 37 * @ since 2.0 38 */39 class Module extends ServiceLocator 40 {41/** 42 * @ event ActionEvent an event raised before executing a controller action. trigger 43 * You may set [[ActionEvent: isValid] to be false to cancel the action execution before executing the action method. 44 * You can set [[ActionEvent: isValid] to false to cancel the operation. 45 */46 const EVENT_BEFORE_ACTION = 'beforeaction'; 47/*** 48 * @ event ActionEvent an event raised after executing a controller action. 49 * trigger 50*51 */52 const EVENT_AFTER_ACTION = 'afteraction' after the action method being controlled '; 53 54/** 55 * @ var array custom module parameters (name => value ). custom module parameter 56 */57 public $ params = []; 58/** 59 * @ var string an ID that uniquely identifies this module among othe R modules which have the same [[module | parent]. the unique identifier of the 60 * Module, which is used to identify the module 61 */62 public $ id under the same parent module; 63/** 64 * @ var Module the parent module of this module. null if this module does not have a parent. 65 * parent module of the current module 66 */67 public $ module; 68/** 69 * @ var string | boolean the layout that shoshould be applied for views within this module. this refers to a view name 70 * relative to [[layoutPath]. if thi S is not set, it means the layout value of the [[module | parent module] 71 * will be taken. if this is false, layout will be disabled within this module. 72 * If the layout file is not set, call the value of [[module | parent module. If it is false, the layout in the module is disabled. 73 */74 public $ layout; 75/** 76 * @ var array mapping from controller ID to controller configurations. controller ID to controller configuration 77 ing 77 * Each name-value pair specifies the configuration of a single controller. 78 * A controller configuration can be either a string or an array. 79 * If the former, the string shocould be the fully qualified class name of the controller. 80 * If the latter, the array must contain A 'class' element which specifies 81 * the controller's fully qualified class name, and the rest of the name-value pairs 82 * in the array are used to initialize the corresponding controller properties. for example, 83 * each key-Value Pair specifies a separate controller. The controller configuration can be a string or array, this string is the full path 84 95 * if it is the latter, it contains a 'class' element to specify the full path of the controller, other parameters are used to initialize the corresponding property 85 *~~~ 86*[87 * 'account' => 'app \ controllers \ usercontroller ', 88 * 'Article' => [89 * 'class' => 'app \ controllers \ postcontroller', 90 * 'pagetitle' => 'something new', 91 *], 92 *] 93 *~~~ 94 */95 public $ controllerMap = []; 96/** 97 * @ var string the namespace that controller classes are in. the controller's namespace 98 * This namespace will be used to load controller classes by prepending it to the controller 99 * class name.100 * the namespace loads the controller class 101 * If not set, it will use the 'controllers' sub-namespace under the namespace of this module.102 * For example, if the namespace of this module Is "foo \ bar", then the default103 * controller namespace wocould be "foo \ bar \ controllers ". 104 * If this parameter is not set, the namespace created by 'controllers' is added to the namespace of the current module by default. 105 119 * If the namespace of the current module is "foo \ bar ", the default namespace of the controller is "foo \ bar \ controllers" 106 * See also the [guide section on autoloading] (guide: concept-autoloading) to learn more about107 * defining namespaces and how classes are loaded.108 */109 public $ controllerNamespace; 110/** 111 *@ Var string the default route of this module. ults to 'default '. the default route of The current module is 112 * The route may consist of child module ID, controller ID, and/or action ID.113 * For example, 'help', 'Post/create ', 'admin/post/create '. 114 * If action ID is not given, it will take the default value as specified in115 * [[Controller: defaultAction]. 116 * The route may contain the sub-module ID, Controller ID, and Operation ID. If the action ID is not specified, the [Controller: d EfaultAction] the specified action117 */118 public $ defaultRoute = 'default'; 119 120/** 121 * @ var string the root directory of the module. the root path of the current module is 122 */123 private $ _ basePath; 124/** 125 * @ var string the root directory that contains view files for this module the path of the view File under the current module is 126 */127 private $ _ viewPath; 128/** 129 * @ var string the root directory that contains layout view files for this module.130 * layout file path under the current Module Path: 131 */132 private $ _ layoutPath; 133/** 134 * @ var array child modules of this module submodule array 135 */136 private $ _ modules = []; 137 138 139/** 140 * Constructor. constructor 141 * @ param string $ id the ID of this module id of the current Module 142 * @ param module $ parent the parent module (if any) the parent module of the current module is 143 * @ param array $ config name-value pairs that will be used to initialize the object properties144 * the configuration file is used to initialize object property 1 45 */146 public function _ construct ($ id, $ parent = null, $ config = []) 147 {148 $ this-> id = $ id; // uniquely identifies the current module 149 $ this-> module = $ parent; // The parent module 150 parent ::__ construct ($ config) of the current module ); // call the parent class configuration 151} 152 153/** 154 * Returns the currently requested instance of this module class. obtain the instance 155 * If the module class is not currently requested of the current class, null will be returned.156 * does not have the module class of the current request, and null is returned. 157 * This method is provided so that you access the module instance from anywhere within the module.158 * you can renew an instance of the class anywhere in the module 159 * @ return static | null the currently requested instance of this module class, or null if the module class is not requested.160 */161 public static function getInstance () 162 {163 $ class = get_called_class (); 164 return isset (Yii :: $ app-> loadedModules [$ class])? Yii: $ app-> loadedModules [$ class]: null; 165} 166 167/** 168 * Sets the currently requested instance of this module class. sets the current request instance of the module class. 169 * @ param Module | null $ instance the currently requested instance of this module class.170 * If it is null, the instance of the calling class will be removed, if any.171 * instance of the current module class. If it is null, the instance that calls the class will be deleted 172 */173 public static function setInstance ($ instance) 174 {175 if ($ instance = null) {// if no parameter is input, directly unset176 unset (Yii ::$ app-> loadedModules [get_called_class ()]); 177} else {// Save the Class and Class instance to the loadedModules array. 178 Yii ::$ app-> loadedModules [get_class ($ instance)] = $ instance; 179} 180} 181 182/** 183 * Initializes the module.184 * initialization module 185 * This method is called after the module is created And initialized with property values186 * given in configuration. the default implementation will initialize [[controllerNamespace] 187 * if it is not set.188 * if no configuration is set for The module creation and initialization, [[controllerNamespace] 189 * If you override this method, please make sure you call the parent implementation.190 * override ensures that the parent class calls 191 */192 public function init () 193 {194 if ($ this-> controllerNamespace === null) {// check whether No blank 195 $ class = get_class ($ this); // obtain the class name 196 if ($ pos = strrpos ($ class ,'\\'))! = False) {197 $ this-> controllerNamespace = substr ($ class, 0, $ pos ). '\ controllers '; // obtain the namespace 198} 199} 200} 201 202/** 203 * Returns an ID that uniquely identifies this module among all modules within the current application.204 * Note that if the module is an application, an empty string will be returned.205 * unique identifier of the module in the current application. If this module is an empty string returned by the application 206 * @ return string the unique ID of the module. unique identifier of a module 207 */208 public function getUniqueId () 209 {// if the current module has a parent module, return the spliced ID as a unique ID, otherwise, only the current module ID210 return $ this-> module? Ltrim ($ this-> module-> getUniqueId (). '/'. $ this-> id, '/'): $ this-> id; 211} 212 213/** 214 * Returns the root directory of the module. return the root path of the current module 215 * It defaults to the directory containing the module class file. the default path contains the module files. 216 * @ return string the root directory of the module. the root path of the current module is 217 */218 public function getBasePath () 219 {220 if ($ this-> _ basePath = null) {221 $ class = new \ ReflectionClass ($ this); // generate the reflection object of the current class 222 $ this-> _ basePath = dirname ($ class-> getFileName ()); // obtain the class definition path 223} 224 225 return $ this-> _ basePath; 226} 227 228/** 229 * Sets the root directory of the module. set the root path of the current module to 230 * This method can only be in Voked at the beginning of the constructor. It is called only at the beginning of the constructor. 231 * @ param string $ path the root directory of the module. This can be the root directory of the either a directory name or a path alias.232 * module. It can be a directory name or path alias 233 * @ throws InvalidParamException if the directory does not exist. if the path does not exist. Throw an exception 234 */235 public function setBasePath ($ path) 236 {237 $ path = Yii: getAlias ($ path); // convert the path alias to the actual path. 238 $ p = realpath ($ path); // returns the absolute path 239 if ($ p! = False & is_dir ($ p) {240 $ this-> _ basePath = $ p; // If the directory name is not false, the returned directory name is returned, otherwise, an exception 241} else {242 throw new InvalidParamException ("The directory does not exist: $ path"); 243} 244} is thrown}