application是所有应用程序类的基类,接下来了解一下它的源码。yii2\base\application.php。
1 php 2 /** 3 * @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 12 /** 13 * application is the base class for all application classes. 14 * 是所有应用程序类的基类 15 * @property \yii\web\assetmanager $assetmanager the asset manager application component. this property is 16 * read-only.资产管理器应用组件,只读 17 * @property \yii\rbac\managerinterface $authmanager the auth manager application component. null is returned 18 * if auth manager is not configured. this property is read-only.认证管理器应用程序组件。未配置返回null,只读 19 * @property string $basepath the root directory of the application. 应用程序的根目录。 20 * @property \yii\caching\cache $cache the cache application component. null if the component is not enabled. 21 * this property is read-only.缓存应用程序组件。 22 * @property \yii\db\connection $db the database connection. this property is read-only.数据库连接。 23 * @property \yii\web\errorhandler|\yii\console\errorhandler $errorhandler the error handler application 24 * component. this property is read-only.错误处理程序应用程序组件 25 * @property \yii\i18n\formatter $formatter the formatter application component. this property is read-only. 26 * 格式化程序的应用程序组件。 27 * @property \yii\i18n\i18n $i18n the internationalization application component. this property is read-only. 28 * 国际化应用组件。 29 * @property \yii\log\dispatcher $log the log dispatcher application component. this property is read-only. 30 * 日志调度程序组件。 31 * @property \yii\mail\mailerinterface $mailer the mailer application component. this property is read-only. 32 * 邮件应用程序组件。 33 * @property \yii\web\request|\yii\console\request $request the request component. this property is read-only. 34 * 请求组件。 35 * @property \yii\web\response|\yii\console\response $response the response component. this property is 36 * read-only.反应元件。 37 * @property string $runtimepath the directory that stores runtime files. defaults to the runtime 38 * subdirectory under [[basepath]].存储运行时文件的目录。 39 * @property \yii\base\security $security the security application component. this property is read-only. 40 * 安全应用组件。 41 * @property string $timezone the time zone used by this application.该应用程序使用的时区。 42 * @property string $uniqueid the unique id of the module. this property is read-only.模块的唯一标识。 43 * @property \yii\web\urlmanager $urlmanager the url manager for this application. this property is read-only. 44 * 此应用程序的网址管理器。 45 * @property string $vendorpath the directory that stores vendor files. defaults to vendor directory under 46 * [[basepath]].存储供应商文件的目录。 47 * @property view|\yii\web\view $view the view application component that is used to render various view 48 * files. this property is read-only.用于呈现各种视图文件的视图应用程序组件 49 * 50 * @author qiang xue 51 * @since 2.0 52 */ 53 abstract class application extends module 54 { 55 /** 56 * @event event an event raised before the application starts to handle a request. 57 * 在应用程序开始处理请求之前提出的事件。 58 */ 59 const event_before_request = 'beforerequest'; 60 /** 61 * @event event an event raised after the application successfully handles a request (before the response is sent out). 62 * 该应用程序成功处理请求后提出的事件 63 */ 64 const event_after_request = 'afterrequest'; 65 /** 66 * application state used by [[state]]: application just started. 67 * [[state]]适用状态:刚开始应用 68 */ 69 const state_begin = 0; 70 /** 71 * application state used by [[state]]: application is initializing. 72 * [[state]]应用程序状态:应用程序初始化。 73 */ 74 const state_init = 1; 75 /** 76 * application state used by [[state]]: application is triggering [[event_before_request]]. 77 * [[state]]应用程序状态:应用触发[[event_before_request]] 78 */ 79 const state_before_request = 2; 80 /** 81 * application state used by [[state]]: application is handling the request. 82 * [[state]]应用程序状态:应用程序处理请求。 83 */ 84 const state_handling_request = 3; 85 /** 86 * application state used by [[state]]: application is triggering [[event_after_request]].. 87 * [[state]]应用程序状态:应用触发[[event_after_request]] 88 */ 89 const state_after_request = 4; 90 /** 91 * application state used by [[state]]: application is about to send response. 92 * [[state]]应用程序状态:应用程序即将发送响应。 93 */ 94 const state_sending_response = 5; 95 /** 96 * application state used by [[state]]: application has ended. 97 * [[state]]应用程序状态:应用程序结束。 98 */ 99 const state_end = 6;100 101 /**102 * @var string the namespace that controller classes are located in.控制器类的命名空间位置。103 * this namespace will be used to load controller classes by prepending it to the controller class name.104 * the default namespace is `app\controllers`.105 * 此命名空间将用于负载控制器类重写它的控制器类的名字。 默认命名空间是`app\controllers`。106 * please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details.107 */108 public $controllernamespace = 'app\\controllers';109 /**110 * @var string the application name.应用程序名称。111 */112 public $name = 'my application';113 /**114 * @var string the version of this application.此应用程序的版本。115 */116 public $version = '1.0';117 /**118 * @var string the charset currently used for the application.目前使用的字符集。119 */120 public $charset = 'utf-8';121 /**122 * @var string the language that is meant to be used for end users. it is recommended that you123 * use [ietf language tags](http://en.wikipedia.org/wiki/ietf_language_tag). for example, `en` stands124 * for english, while `en-us` stands for english (united states).125 * 用来作为终端用户使用的语言126 * @see sourcelanguage127 */128 public $language = 'en-us';129 /**130 * @var string the language that the application is written in. this mainly refers to131 * the language that the messages and view files are written in.132 * 应用程序编写的语言。133 * @see language134 */135 public $sourcelanguage = 'en-us';136 /**137 * @var controller the currently active controller instance当前活动控制器实例138 */139 public $controller;140 /**141 * @var string|boolean the layout that should be applied for views in this application. defaults to 'main'.142 * if this is false, layout will be disabled.143 * 该应用程序中应用的布局。144 */145 public $layout = 'main';146 /**147 * @var string the requested route请求的路径 请求的路径148 */149 public $requestedroute;150 /**151 * @var action the requested action. if null, it means the request cannot be resolved into an action.152 * 操作所要求的行动153 */154 public $requestedaction;155 /**156 * @var array the parameters supplied to the requested action.157 * 所请求的动作提供的参数。158 */159 public $requestedparams;160 /**161 * @var array list of installed yii extensions. each array element represents a single extension162 * with the following structure:163 * 安装yii扩展名列表。每个数组元素代表一个扩展164 *165 * ~~~166 * [167 * 'name' => 'extension name',168 * 'version' => 'version number',169 * 'bootstrap' => 'bootstrapclassname', // optional, may also be a configuration array170 * 'alias' => [171 * '@alias1' => 'to/path1',172 * '@alias2' => 'to/path2',173 * ],174 * ]175 * ~~~176 *177 * the bootstrap class listed above will be instantiated during the application178 * [[bootstrap()|bootstrapping process]]. if the class implements [[bootstrapinterface]],179 * its [[bootstrapinterface::bootstrap()|bootstrap()]] method will be also be called.180 *181 * if not set explicitly in the application config, this property will be populated with the contents of182 * 如果在应用程序配置中没有设置,该属性将填充到内容183 * @vendor/yiisoft/extensions.php`.184 */185 public $extensions;186 /**187 * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]].188 * 组件的列表,运行在 [[bootstrap()|bootstrapping process]]中的应用189 * each component may be specified in one of the following formats:190 *191 * - an application component id as specified via [[components]].192 * - a module id as specified via [[modules]].193 * - a class name.194 * - a configuration array.195 *196 * during the bootstrapping process, each component will be instantiated. if the component class197 * implements [[bootstrapinterface]], its [[bootstrapinterface::bootstrap()|bootstrap()]] method198 * will be also be called.199 * 在整个启动过程中,每个组件被实例化。如果组件类提到 [[bootstrapinterface]], 200 * [[bootstrapinterface::bootstrap()|bootstrap()]]方法也会调用201 */202 public $bootstrap = [];203 /**204 * @var integer the current application state during a request handling life cycle.205 * this property is managed by the application. do not modify this property. 206 * 在请求处理生命周期中的当前应用程序状态。属性由应用程序管理。不要修改此属性。207 */208 public $state;209 /**210 * @var array list of loaded modules indexed by their class names.211 * 加载模块列表由它们的类名称索引组成。212 */213 public $loadedmodules = [];214 215 216 /**217 * constructor.构造函数218 * @param array $config name-value pairs that will be used to initialize the object properties.219 * note that the configuration must contain both [[id]] and [[basepath]].220 * 用来初始化对象属性的 name-value 注意配置必须包含[[id]] 和[[basepath]].221 * @throws invalidconfigexception if either [[id]] or [[basepath]] configuration is missing.222 * 如果是修改[[id]] 或[[basepath]] 则配置丢失。223 */224 public function __construct($config = [])225 {226 yii::$app = $this;// 将自身的实例绑到yii的$app上227 $this->setinstance($this);// 将自身加入到loadedmodules中228 229 $this->state = self::state_begin;// 设置状态为刚开始230 231 // 做预处理配置232 $this->preinit($config);233 234 $this->registererrorhandler($config);235 236 component::__construct($config);237 }238 239 /**240 * pre-initializes the application. 初始化应用。241 * this method is called at the beginning of the application constructor.242 * it initializes several important application properties.243 * 在构造函数中调用该方法,用于初始化一些重要的属性244 * if you override this method, please make sure you call the parent implementation.245 * @param array $config the application configuration 应用的配置246 * @throws invalidconfigexception if either [[id]] or [[basepath]] configuration is missing.247 */248 public function preinit(&$config)249 {250 // 使用了&符号,表示$config的修改会保留251 if (!isset($config['id'])) {//判断配置中是否有application id ,如果没有,抛出异常252 throw new invalidconfigexception('the id configuration for the application is required.');253 }254 if (isset($config['basepath'])) {255 // 是否配置项目的root路径256 $this->setbasepath($config['basepath']);257 //赋值给模块的_basepath属性,并在设置后删除258 unset($config['basepath']);259 } else {//否则抛出异常260 throw new invalidconfigexception('the basepath configuration for the application is required.');261 }262 //如果配置文件中设置了 vendorpath 使用配置的值,并在设置后删除,否则使用默认的263 if (isset($config['vendorpath'])) {264 $this->setvendorpath($config['vendorpath']);265 unset($config['vendorpath']);266 } else {267 // set @vendor268 $this->getvendorpath();269 }270 //如果配置文件中设置了 runtimepath 使用配置的值,并在设置后删除,否则使用默认的271 if (isset($config['runtimepath'])) {272 $this->setruntimepath($config['runtimepath']);273 unset($config['runtimepath']);274 } else {275 // set @runtime276 $this->getruntimepath();277 }278 //如果配置文件中设置了 timezone 使用配置的值,并在设置后删除,否则使用默认的时区279 if (isset($config['timezone'])) {280 $this->settimezone($config['timezone']);281 unset($config['timezone']);282 } elseif (!ini_get('date.timezone')) {283 $this->settimezone('utc');284 }285 286 // merge core components with custom components287 foreach ($this->corecomponents() as $id => $component) {288 if (!isset($config['components'][$id])) {289 // 如果配置中没有配置相应的核心component,就赋给它290 $config['components'][$id] = $component;291 } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {292 // 如果存在相应的核心component,但没有定义它的class,就直接赋到class的key上293 $config['components'][$id]['class'] = $component['class'];294 }295 }296 }297 298 /**299 * @inheritdoc300 */301 public function init()302 {303 $this->state = self::state_init;304 $this->bootstrap();305 }306 307 /**308 * initializes extensions and executes bootstrap components.初始化扩展并执行初始化程序组件309 * this method is called by [[init()]] after the application has been fully configured.310 * 该方法在应用完全配置后被[[init()]]调用311 * if you override this method, make sure you also call the parent implementation.312 */313 protected function bootstrap()314 {315 if ($this->extensions === null) {//如果没有配置,则调用yii的默认扩展组件316 $file = yii::getalias('@vendor/yiisoft/extensions.php');317 $this->extensions = is_file($file) ? include($file) : [];318 }319 foreach ($this->extensions as $extension) {320 if (!empty($extension['alias'])) {//如果扩展组件有设置别名321 foreach ($extension['alias'] as $name => $path) {322 yii::setalias($name, $path);//将给扩展的别名注册到别名数组中323 }324 }325 if (isset($extension['bootstrap'])) {//如果扩展组件有[[bootstrap]]配置 则初始化给扩展组件326 $component = yii::createobject($extension['bootstrap']);327 if ($component instanceof bootstrapinterface) {328 yii::trace(bootstrap with . get_class($component) . '::bootstrap()', __method__);329 $component->bootstrap($this);330 } else {331 yii::trace(bootstrap with . get_class($component), __method__);332 }333 }334 }335 336 foreach ($this->bootstrap as $class) {337 $component = null;338 if (is_string($class)) {339 if ($this->has($class)) {340 $component = $this->get($class);341 } elseif ($this->hasmodule($class)) {342 $component = $this->getmodule($class);343 } elseif (strpos($class, '\\') === false) {344 throw new invalidconfigexception(unknown bootstrapping component id: $class);345 }346 }347 if (!isset($component)) {//如果不存在,则调用yii创建对象348 $component = yii::createobject($class);349 }350 351 if ($component instanceof bootstrapinterface) {352 yii::trace(bootstrap with . get_class($component) . '::bootstrap()', __method__);353 $component->bootstrap($this);354 } else {355 yii::trace(bootstrap with . get_class($component), __method__);356 }357 }358 }
未完待续。