yii2源码学习笔记(十)

继续了解Application.

  1   /**
  2      * Registers the errorHandler component as a PHP error handler.
  3      * 注册errorHandler组件作为PHP错误处理函数
  4      * @param array $config application config  应用程序配置
  5      */
  6     protected function registerErrorHandler(&$config)
  7     {
  8         if (YII_ENABLE_ERROR_HANDLER) {// YII_ENABLE_ERROR_HANDLER在BaseYii中被定义为true
  9             if (!isset($config['components']['errorHandler']['class'])) {
 10                 //$config['components']['errorHandler']['class']不存在结束运行    
 11                 echo "Error: no errorHandler component is configured.
";
 12                 exit(1);
 13             }
 14             //将$config['components']['errorHandler']的内容设置到了$this->_definitions['errorHandler']中
 15             $this->set('errorHandler', $config['components']['errorHandler']);
 16             unset($config['components']['errorHandler']);// 删除掉配置内容
 17             $this->getErrorHandler()->register();
 18         }
 19     }
 20 
 21     /**
 22      * Returns an ID that uniquely identifies this module among all modules within the current application.
 23      * Since this is an application instance, it will always return an empty string.
 24      * 返回在当前应用程序中该模块的唯一标识。这是一个应用实例,它将返回一个空字符串。
 25      * @return string the unique ID of the module.模块的唯一标识。
 26      */
 27     public function getUniqueId()
 28     {
 29         return '';
 30     }
 31 
 32     /**
 33      * Sets the root directory of the application and the @app alias.设置应用程序的根目录 @ 加应用程序别名。
 34      * This method can only be invoked at the beginning of the constructor.只能在构造函数开始时调用该方法
 35      * @param string $path the root directory of the application.应用程序的根目录。
 36      * @property string the root directory of the application. 应用程序的根目录。
 37      * @throws InvalidParamException if the directory does not exist. 如果目录不存在。抛出异常
 38      */
 39     public function setBasePath($path)
 40     {
 41         parent::setBasePath($path);
 42         // 使用@app来记录basePath
 43         Yii::setAlias('@app', $this->getBasePath());
 44     }
 45 
 46     /**
 47      * Runs the application.    运行应用程序。
 48      * This is the main entrance of an application. 应用程序的主要入口。
 49      * @return integer the exit status (0 means normal, non-zero values mean abnormal) 状态(0正常,非0为不正常)
 50      */
 51     public function run()
 52     {
 53         try {
 54 
 55             $this->state = self::STATE_BEFORE_REQUEST; 
 56             $this->trigger(self::EVENT_BEFORE_REQUEST);//加载事件函数beforRequest函数
 57 
 58             $this->state = self::STATE_HANDLING_REQUEST;
 59             $response = $this->handleRequest($this->getRequest());//加载控制器  获取Request对象
 60 
 61             $this->state = self::STATE_AFTER_REQUEST;
 62             $this->trigger(self::EVENT_AFTER_REQUEST);//加载afterRequest事件函数
 63 
 64             $this->state = self::STATE_SENDING_RESPONSE;
 65             $response->send();//将页面内容输入缓冲,然后输出
 66 
 67             $this->state = self::STATE_END;
 68 
 69             return $response->exitStatus;
 70 
 71         } catch (ExitException $e) {
 72             
 73             $this->end($e->statusCode, isset($response) ? $response : null);
 74             return $e->statusCode;
 75 
 76         }
 77     }
 78 
 79     /**
 80      * Handles the specified request.
 81      *  处理指定的请求
 82      * This method should return an instance of [[Response]] or its child class
 83      * which represents the handling result of the request.
 84      *  该方法应该返回一个[[Response]]实例,或者它的子类代表处理请求的结果
 85      * @param Request $request the request to be handled    被处理的请求
 86      * @return Response the resulting response  得到的响应
 87      */
 88     abstract public function handleRequest($request);
 89 
 90     private $_runtimePath;
 91 
 92     /**
 93      * Returns the directory that stores runtime files.返回存储运行时文件的路径
 94      * @return string the directory that stores runtime files.存储运行时文件的目录。
 95      * Defaults to the "runtime" subdirectory under [[basePath]].默认返回[[basePath]]下的 "runtime"目录
 96      */
 97     public function getRuntimePath()
 98     {
 99         if ($this->_runtimePath === null) {//设置临时文件存储路径
100             $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
101         }
102 
103         return $this->_runtimePath;
104     }
105 
106     /**
107      * Sets the directory that stores runtime files.设置存储运行时文件的路径
108      * @param string $path the directory that stores runtime files.存储运行时文件的目录。
109      */
110     public function setRuntimePath($path)
111     {
112         // 获取runtimePath的路径,并存到_runtimePath中
113         $this->_runtimePath = Yii::getAlias($path);
114          // 使用@runtime来记录 runtimePath
115         Yii::setAlias('@runtime', $this->_runtimePath);
116     }
117 
118     private $_vendorPath;
119 
120     /**
121      * Returns the directory that stores vendor files.返回插件文件的目录。
122      * @return string the directory that stores vendor files.
123      * Defaults to "vendor" directory under [[basePath]].
124      * 默认返回[[basePath]]下的 "vendor" 目录
125      */
126     public function getVendorPath()
127     {
128         if ($this->_vendorPath === null) {
129             // 不存在,就将其设置为basePath/vendor
130             $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
131         }
132 
133         return $this->_vendorPath;
134     }
135 
136     /**
137      * Sets the directory that stores vendor files.设置插件目录路径,并设置别名
138      * @param string $path the directory that stores vendor files.
139      */
140     public function setVendorPath($path)
141     {
142         $this->_vendorPath = Yii::getAlias($path);// 获取vendor的路径,并存到_vendorPath中
143         Yii::setAlias('@vendor', $this->_vendorPath);// 设置@vendor的alias
144         Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
145         Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
146     }
147 
148     /**
149      * Returns the time zone used by this application.取得时区
150      * This is a simple wrapper of PHP function date_default_timezone_get().
151      * If time zone is not configured in php.ini or application config,
152      * it will be set to UTC by default.
153      * @return string the time zone used by this application.
154      * @see http://php.net/manual/en/function.date-default-timezone-get.php
155      */
156     public function getTimeZone()
157     {
158         return date_default_timezone_get();
159     }
160 
161     /**
162      * Sets the time zone used by this application.设置时区
163      * This is a simple wrapper of PHP function date_default_timezone_set().
164      * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
165      * @param string $value the time zone used by this application.
166      * @see http://php.net/manual/en/function.date-default-timezone-set.php
167      */
168     public function setTimeZone($value)
169     {
170         date_default_timezone_set($value);
171     }
172 
173     /**
174      * Returns the database connection component.返回数据库连接组件
175      * @return yiidbConnection the database connection.
176      */
177     public function getDb()
178     {
179         return $this->get('db');
180     }
181 
182     /**
183      * Returns the log dispatcher component.返回日志调度组件
184      * @return yiilogDispatcher the log dispatcher application component.
185      */
186     public function getLog()
187     {
188         return $this->get('log');
189     }
190 
191     /**
192      * Returns the error handler component.返回错误处理组件
193      * @return yiiwebErrorHandler|yiiconsoleErrorHandler the error handler application component.
194      */
195     public function getErrorHandler()
196     {
197         return $this->get('errorHandler');
198     }
199 
200     /**
201      * Returns the cache component.返回缓存组件
202      * @return yiicachingCache the cache application component. Null if the component is not enabled.
203      */
204     public function getCache()
205     {
206         return $this->get('cache', false);
207     }
208 
209     /**
210      * Returns the formatter component.返回格式化程序组件
211      * @return yiii18nFormatter the formatter application component.
212      */
213     public function getFormatter()
214     {
215         return $this->get('formatter');
216     }
217 
218     /**
219      * Returns the request component.返回请求的组件对象
220      * @return yiiwebRequest|yiiconsoleRequest the request component.
221      */
222     public function getRequest()
223     {
224         return $this->get('request');
225     }
226 
227     /**
228      * Returns the response component.返回响应组件
229      * @return yiiwebResponse|yiiconsoleResponse the response component.
230      */
231     public function getResponse()
232     {
233         return $this->get('response');
234     }
235 
236     /**
237      * Returns the view object.返回视图对象
238      * @return View|yiiwebView the view application component that is used to render various view files.
239      */
240     public function getView()
241     {
242         return $this->get('view');
243     }
244 
245     /**
246      * Returns the URL manager for this application.返回当前应用的URL管理组件
247      * @return yiiwebUrlManager the URL manager for this application.
248      */
249     public function getUrlManager()
250     {
251         return $this->get('urlManager');
252     }
253 
254     /**
255      * Returns the internationalization (i18n) component返回国际化组件
256      * @return yiii18nI18N the internationalization application component.
257      */
258     public function getI18n()
259     {
260         return $this->get('i18n');
261     }
262 
263     /**
264      * Returns the mailer component.返回邮件组件
265      * @return yiimailMailerInterface the mailer application component.
266      */
267     public function getMailer()
268     {
269         return $this->get('mailer');
270     }
271 
272     /**
273      * Returns the auth manager for this application.返回该应用的权限管理组件
274      * @return yii
bacManagerInterface the auth manager application component.
275      * Null is returned if auth manager is not configured.   管理权限没有配置返回null
276      */
277     public function getAuthManager()
278     {
279         return $this->get('authManager', false);
280     }
281 
282     /**
283      * Returns the asset manager.返回资源管理组件
284      * @return yiiwebAssetManager the asset manager application component.
285      */
286     public function getAssetManager()
287     {
288         return $this->get('assetManager');
289     }
290 
291     /**
292      * Returns the security component.返回安全组件
293      * @return yiiaseSecurity the security application component.
294      */
295     public function getSecurity()
296     {
297         return $this->get('security');
298     }
299 
300     /**
301      * Returns the configuration of core application components.返回核心组件的配置
302      * @see set()
303      */
304     public function coreComponents()
305     {
306         return [
307             'log' => ['class' => 'yiilogDispatcher'],
308             'view' => ['class' => 'yiiwebView'],
309             'formatter' => ['class' => 'yiii18nFormatter'],
310             'i18n' => ['class' => 'yiii18nI18N'],
311             'mailer' => ['class' => 'yiiswiftmailerMailer'],
312             'urlManager' => ['class' => 'yiiwebUrlManager'],
313             'assetManager' => ['class' => 'yiiwebAssetManager'],
314             'security' => ['class' => 'yiiaseSecurity'],
315         ];
316     }
317 
318     /**
319      * Terminates the application.终止应用程序
320      * This method replaces the `exit()` function by ensuring the application life cycle is completed
321      * before terminating the application.该方法代替`exit()`  确认一个应用的生命周期已经结束
322      * @param integer $status the exit status (value 0 means normal exit while other values mean abnormal exit).
323      * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used.
324      * @throws ExitException if the application is in testing mode
325      */
326     public function end($status = 0, $response = null)
327     {
328         if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
329             //判断当前状态为请求前或者处理请求
330             $this->state = self::STATE_AFTER_REQUEST;//设置应用状态为请求完成后
331             $this->trigger(self::EVENT_AFTER_REQUEST);
332         }
333 
334         if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
335             //如果应用状态不是发送应答和应用结束
336             $this->state = self::STATE_END;//设置状态为应用结束
337             $response = $response ? : $this->getResponse();
338             $response->send();//向客户端发送应答
339         }
340 
341         if (YII_ENV_TEST) {
342             throw new ExitException($status);
343         } else {
344             exit($status);
345         }
346     }

php

原文地址:https://www.cnblogs.com/dragon16/p/5554284.html