热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

yii2源码学习笔记(十)

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.\n";
 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 \yii\db\Connection 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 \yii\log\Dispatcher 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 \yii\web\ErrorHandler|\yii\console\ErrorHandler 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 \yii\caching\Cache 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 \yii\i18n\Formatter 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 \yii\web\Request|\yii\console\Request the request component.
221      */
222     public function getRequest()
223     {
224         return $this->get('request');
225     }
226 
227     /**
228      * Returns the response component.返回响应组件
229      * @return \yii\web\Response|\yii\console\Response 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|\yii\web\View 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 \yii\web\UrlManager 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 \yii\i18n\I18N 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 \yii\mail\MailerInterface 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\rbac\ManagerInterface 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 \yii\web\AssetManager 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 \yii\base\Security 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' => 'yii\log\Dispatcher'],
308             'view' => ['class' => 'yii\web\View'],
309             'formatter' => ['class' => 'yii\i18n\Formatter'],
310             'i18n' => ['class' => 'yii\i18n\I18N'],
311             'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
312             'urlManager' => ['class' => 'yii\web\UrlManager'],
313             'assetManager' => ['class' => 'yii\web\AssetManager'],
314             'security' => ['class' => 'yii\base\Security'],
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


推荐阅读
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 有没有一种方法可以在不继承UIAlertController的子类或不涉及UIAlertActions的情况下 ... [详细]
  • 【前端工具】nodejs+npm+vue 安装(windows)
    预备先看看这几个是干嘛的,相互的关系是啥。nodejs是语言,类比到php。npm是个包管理,类比到composer。vue是个框架&# ... [详细]
  • 必须先赞下国人npm库作品:node-images(https:github.comzhangyuanweinode-images),封装了跨平台的C++逻辑,形成nodejsAP ... [详细]
  • vuecli创建项目(详情步骤)
    1、安装node环境2、下载vue和vue-cli脚手架命令行输入npm ... [详细]
  • 这篇文章主要讲解了“如何应对Android面试”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何应对 ... [详细]
  • 1、DashAPI文档Dash是一个API文档浏览器,使用户可以使用离线功能即时搜索无数API。程序员使用Dash可访问iOS,MacOS, ... [详细]
  • 1.移除consol.log()的babel插件安装:npmibabel-plugin-transform-remove-console-D配置:babel.config.js:这 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 如何修改路由器密码?路由器登录密码和无线密码的修改方法
    本文介绍了修改路由器密码的两种方法:一是修改路由器登录口令,需要进入路由器后台进行操作;二是修改无线连接密码,通过进入路由器后台的无线设置和无线安全设置进行修改。详细步骤包括复位处理、登录路由器后台、选择系统工具、填入用户名和用户密码、保存修改等。 ... [详细]
  • 本文介绍了2019年上半年内蒙古计算机软考考试的报名通知和考试时间。考试报名时间为3月1日至3月23日,考试时间为2019年5月25日。考试分为高级、中级和初级三个级别,涵盖了多个专业资格。报名采取网上报名和网上缴费的方式进行,报考人员可登录内蒙古人事考试信息网进行报名。详细内容请点击查看。 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
author-avatar
爱上为突然_381
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有