yii2源码学习笔记(十)
继续了解Application.
/**
* Registers the errorHandler component as a PHP error handler.
* 注册errorHandler组件作为PHP错误处理函数
* @param array $config application config 应用程序配置
*/
protected function registerErrorHandler(&$config)
{
if (YII_ENABLE_ERROR_HANDLER) {// YII_ENABLE_ERROR_HANDLER在BaseYii中被定义为true
if (!isset($config['components']['errorHandler']['class'])) {
//$config['components']['errorHandler']['class']不存在结束运行
echo "Error: no errorHandler component is configured.\n";
exit();
}
//将$config['components']['errorHandler']的内容设置到了$this->_definitions['errorHandler']中
$this->set('errorHandler', $config['components']['errorHandler']);
unset($config['components']['errorHandler']);// 删除掉配置内容
$this->getErrorHandler()->register();
}
} /**
* Returns an ID that uniquely identifies this module among all modules within the current application.
* Since this is an application instance, it will always return an empty string.
* 返回在当前应用程序中该模块的唯一标识。这是一个应用实例,它将返回一个空字符串。
* @return string the unique ID of the module.模块的唯一标识。
*/
public function getUniqueId()
{
return '';
} /**
* Sets the root directory of the application and the @app alias.设置应用程序的根目录 @ 加应用程序别名。
* This method can only be invoked at the beginning of the constructor.只能在构造函数开始时调用该方法
* @param string $path the root directory of the application.应用程序的根目录。
* @property string the root directory of the application. 应用程序的根目录。
* @throws InvalidParamException if the directory does not exist. 如果目录不存在。抛出异常
*/
public function setBasePath($path)
{
parent::setBasePath($path);
// 使用@app来记录basePath
Yii::setAlias('@app', $this->getBasePath());
} /**
* Runs the application. 运行应用程序。
* This is the main entrance of an application. 应用程序的主要入口。
* @return integer the exit status (0 means normal, non-zero values mean abnormal) 状态(0正常,非0为不正常)
*/
public function run()
{
try { $this->state = self::STATE_BEFORE_REQUEST;
$this->trigger(self::EVENT_BEFORE_REQUEST);//加载事件函数beforRequest函数 $this->state = self::STATE_HANDLING_REQUEST;
$response = $this->handleRequest($this->getRequest());//加载控制器 获取Request对象 $this->state = self::STATE_AFTER_REQUEST;
$this->trigger(self::EVENT_AFTER_REQUEST);//加载afterRequest事件函数 $this->state = self::STATE_SENDING_RESPONSE;
$response->send();//将页面内容输入缓冲,然后输出 $this->state = self::STATE_END; return $response->exitStatus; } catch (ExitException $e) { $this->end($e->statusCode, isset($response) ? $response : null);
return $e->statusCode; }
} /**
* Handles the specified request.
* 处理指定的请求
* This method should return an instance of [[Response]] or its child class
* which represents the handling result of the request.
* 该方法应该返回一个[[Response]]实例,或者它的子类代表处理请求的结果
* @param Request $request the request to be handled 被处理的请求
* @return Response the resulting response 得到的响应
*/
abstract public function handleRequest($request); private $_runtimePath; /**
* Returns the directory that stores runtime files.返回存储运行时文件的路径
* @return string the directory that stores runtime files.存储运行时文件的目录。
* Defaults to the "runtime" subdirectory under [[basePath]].默认返回[[basePath]]下的 "runtime"目录
*/
public function getRuntimePath()
{
if ($this->_runtimePath === null) {//设置临时文件存储路径
$this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
} return $this->_runtimePath;
} /**
* Sets the directory that stores runtime files.设置存储运行时文件的路径
* @param string $path the directory that stores runtime files.存储运行时文件的目录。
*/
public function setRuntimePath($path)
{
// 获取runtimePath的路径,并存到_runtimePath中
$this->_runtimePath = Yii::getAlias($path);
// 使用@runtime来记录 runtimePath
Yii::setAlias('@runtime', $this->_runtimePath);
} private $_vendorPath; /**
* Returns the directory that stores vendor files.返回插件文件的目录。
* @return string the directory that stores vendor files.
* Defaults to "vendor" directory under [[basePath]].
* 默认返回[[basePath]]下的 "vendor" 目录
*/
public function getVendorPath()
{
if ($this->_vendorPath === null) {
// 不存在,就将其设置为basePath/vendor
$this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
} return $this->_vendorPath;
} /**
* Sets the directory that stores vendor files.设置插件目录路径,并设置别名
* @param string $path the directory that stores vendor files.
*/
public function setVendorPath($path)
{
$this->_vendorPath = Yii::getAlias($path);// 获取vendor的路径,并存到_vendorPath中
Yii::setAlias('@vendor', $this->_vendorPath);// 设置@vendor的alias
Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
} /**
* Returns the time zone used by this application.取得时区
* This is a simple wrapper of PHP function date_default_timezone_get().
* If time zone is not configured in php.ini or application config,
* it will be set to UTC by default.
* @return string the time zone used by this application.
* @see http://php.net/manual/en/function.date-default-timezone-get.php
*/
public function getTimeZone()
{
return date_default_timezone_get();
} /**
* Sets the time zone used by this application.设置时区
* This is a simple wrapper of PHP function date_default_timezone_set().
* Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
* @param string $value the time zone used by this application.
* @see http://php.net/manual/en/function.date-default-timezone-set.php
*/
public function setTimeZone($value)
{
date_default_timezone_set($value);
} /**
* Returns the database connection component.返回数据库连接组件
* @return \yii\db\Connection the database connection.
*/
public function getDb()
{
return $this->get('db');
} /**
* Returns the log dispatcher component.返回日志调度组件
* @return \yii\log\Dispatcher the log dispatcher application component.
*/
public function getLog()
{
return $this->get('log');
} /**
* Returns the error handler component.返回错误处理组件
* @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component.
*/
public function getErrorHandler()
{
return $this->get('errorHandler');
} /**
* Returns the cache component.返回缓存组件
* @return \yii\caching\Cache the cache application component. Null if the component is not enabled.
*/
public function getCache()
{
return $this->get('cache', false);
} /**
* Returns the formatter component.返回格式化程序组件
* @return \yii\i18n\Formatter the formatter application component.
*/
public function getFormatter()
{
return $this->get('formatter');
} /**
* Returns the request component.返回请求的组件对象
* @return \yii\web\Request|\yii\console\Request the request component.
*/
public function getRequest()
{
return $this->get('request');
} /**
* Returns the response component.返回响应组件
* @return \yii\web\Response|\yii\console\Response the response component.
*/
public function getResponse()
{
return $this->get('response');
} /**
* Returns the view object.返回视图对象
* @return View|\yii\web\View the view application component that is used to render various view files.
*/
public function getView()
{
return $this->get('view');
} /**
* Returns the URL manager for this application.返回当前应用的URL管理组件
* @return \yii\web\UrlManager the URL manager for this application.
*/
public function getUrlManager()
{
return $this->get('urlManager');
} /**
* Returns the internationalization (i18n) component返回国际化组件
* @return \yii\i18n\I18N the internationalization application component.
*/
public function getI18n()
{
return $this->get('i18n');
} /**
* Returns the mailer component.返回邮件组件
* @return \yii\mail\MailerInterface the mailer application component.
*/
public function getMailer()
{
return $this->get('mailer');
} /**
* Returns the auth manager for this application.返回该应用的权限管理组件
* @return \yii\rbac\ManagerInterface the auth manager application component.
* Null is returned if auth manager is not configured. 管理权限没有配置返回null
*/
public function getAuthManager()
{
return $this->get('authManager', false);
} /**
* Returns the asset manager.返回资源管理组件
* @return \yii\web\AssetManager the asset manager application component.
*/
public function getAssetManager()
{
return $this->get('assetManager');
} /**
* Returns the security component.返回安全组件
* @return \yii\base\Security the security application component.
*/
public function getSecurity()
{
return $this->get('security');
} /**
* Returns the configuration of core application components.返回核心组件的配置
* @see set()
*/
public function coreComponents()
{
return [
'log' => ['class' => 'yii\log\Dispatcher'],
'view' => ['class' => 'yii\web\View'],
'formatter' => ['class' => 'yii\i18n\Formatter'],
'i18n' => ['class' => 'yii\i18n\I18N'],
'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
'urlManager' => ['class' => 'yii\web\UrlManager'],
'assetManager' => ['class' => 'yii\web\AssetManager'],
'security' => ['class' => 'yii\base\Security'],
];
} /**
* Terminates the application.终止应用程序
* This method replaces the `exit()` function by ensuring the application life cycle is completed
* before terminating the application.该方法代替`exit()` 确认一个应用的生命周期已经结束
* @param integer $status the exit status (value 0 means normal exit while other values mean abnormal exit).
* @param Response $response the response to be sent. If not set, the default application [[response]] component will be used.
* @throws ExitException if the application is in testing mode
*/
public function end($status = , $response = null)
{
if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
//判断当前状态为请求前或者处理请求
$this->state = self::STATE_AFTER_REQUEST;//设置应用状态为请求完成后
$this->trigger(self::EVENT_AFTER_REQUEST);
} if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
//如果应用状态不是发送应答和应用结束
$this->state = self::STATE_END;//设置状态为应用结束
$response = $response ? : $this->getResponse();
$response->send();//向客户端发送应答
} if (YII_ENV_TEST) {
throw new ExitException($status);
} else {
exit($status);
}
}
php
yii2源码学习笔记(十)的更多相关文章
- yii2源码学习笔记(十四)
Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...
- yii2源码学习笔记(十九)
view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...
- yii2源码学习笔记(十二)
继续了解controller基类. /** * Runs a request specified in terms of a route.在路径中指定的请求. * The route can be e ...
- yii2源码学习笔记(十六)
Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...
- yii2源码学习笔记(十五)
这几天有点忙今天好些了,继续上次的module来吧 /** * Returns the directory that contains the controller classes according ...
- yii2源码学习笔记(九)
Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...
- yii2源码学习笔记(八)
Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...
- 老刘 Yii2 源码学习笔记之 Action 类
Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...
- yii2源码学习笔记(二十)
Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...
随机推荐
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- LIMS系统供应商一览表
LIMS系统供应商一览表. 国内自主研发的LIMS供应商的产品质量一般,国外的LIMS产品在本土化方面,北京三维天地的质量最佳. LIMS系统JAVA..Net平台上都有,由于实验室业务数据量等原因, ...
- oracle11 客户端安装及PLSQL和TOAD中文乱码
oracle11 客户端安装及PLSQL和TOAD中文乱码 1.从Oracle官方下载“Instant Client Package”的软件,较好的实现了Oracle客户端绿化的解决方案. 下载地址为 ...
- eclipse mybatis Generator
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 【42】了解typename的双重意义
1.在template声明中,class与typename是等价的,但是使用typename更好. 2.在template实现中,模版形参是从属名称,嵌套在模版形参中的类型是嵌套从属名称,不依赖任何t ...
- android106 C基本数据类型
#JNI java native interface #c的基本数据类型 * int:32位,能表示的数字是2的32次方个 * 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是 ...
- iostat来对linux硬盘IO性能进行了解
http://www.php-oa.com/2009/02/03/iostat.html
- AFNetworking源码分析
来源:zongmumask 链接:http://www.jianshu.com/p/8eac5b1975de 简述 在iOS开发中,与直接使用苹果框架中提供的NSURLConnection或NSURL ...
- iOS之AlertController的使用
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...
- modelsim脚本文件的编写
第一章 ModelSim介 绍 本指南是为 ModelSim5.5f版本编写的,该版本运行于UNIX和Microsoft Windows 95/98/Me/NT/2000的操作系统环境中.本指南覆盖了 ...