Application的run方法分析
来源:互联网 发布:mysql 缓存命中率 编辑:程序博客网 时间:2024/05/17 12:51
(new yii\web\Application($config))->run();
$config 是配置文件获取的参数
__construct方法调用的是Application的父类的__construct
public function __construct($config = []){ /**设置成单例**/ Yii::$app = $this; static::setInstance($this); /**APP应用状态 说明是刚开始 STATE_BEGIN = 0**/ $this->state = self::STATE_BEGIN; /**应用的预先初始化**/ $this->preInit($config); /**注册错误处理**/ $this->registerErrorHandler($config); /**组件初始化**/ Component::__construct($config);}
run方法也是 调用的是Application的父类的__run
public function run(){ try { //请求前过滤器 $this->state = self::STATE_BEFORE_REQUEST; $this->trigger(self::EVENT_BEFORE_REQUEST); //处理请求 $this->state = self::STATE_HANDLING_REQUEST; $response = $this->handleRequest($this->getRequest()); //请求后过滤器 $this->state = self::STATE_AFTER_REQUEST; $this->trigger(self::EVENT_AFTER_REQUEST); //发送请求到浏览器 $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; }}
首先分析下 __construct 用到的函数preInit __construct 的config就是用到这里
public function preInit(&$config){ /**应用ID 必须得有 不能为空**/ if (!isset($config['id'])) { throw new InvalidConfigException('The "id" configuration for the Application is required.'); } /**APP路径必须得设置好**/ if (isset($config['basePath'])) { $this->setBasePath($config['basePath']); unset($config['basePath']); } else { throw new InvalidConfigException('The "basePath" configuration for the Application is required.'); } /**检查组件路径**/ if (isset($config['vendorPath'])) { $this->setVendorPath($config['vendorPath']); unset($config['vendorPath']); } else { // set "@vendor" $this->getVendorPath(); } /**检查runtimePath debug和log文件位置**/ if (isset($config['runtimePath'])) { $this->setRuntimePath($config['runtimePath']); unset($config['runtimePath']); } else { // set "@runtime" $this->getRuntimePath(); } /**检查时区设置**/ if (isset($config['timeZone'])) { $this->setTimeZone($config['timeZone']); unset($config['timeZone']); } elseif (!ini_get('date.timezone')) { $this->setTimeZone('UTC'); } /**检查setContainer**/ if (isset($config['container'])) { /**设置容器**/ $this->setContainer($config['container']); unset($config['container']); } // merge core components with custom components /**coreComponents 是获取设置应用的核心组件**/ foreach ($this->coreComponents() as $id => $component) { if (!isset($config['components'][$id])) { $config['components'][$id] = $component; } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) { $config['components'][$id]['class'] = $component['class']; } }}
Component::__construct 调用的是父类Object的 __construct
public function __construct($config = []){ if (!empty($config)) { Yii::configure($this, $config); } $this->init();}/**这里调用的是yii\BaseYii里的configure,注册组件名和组件到APP**/public static function configure($object, $properties){ foreach ($properties as $name => $value) { $object->$name = $value; } return $object;}
run方法里的trigger方法是调用的其父类 Module-》ServiceLocator-》Component的方法
public function trigger($name, Event $event = null){ $this->ensureBehaviors(); if (!empty($this->_events[$name])) { if ($event === null) { $event = new Event; } if ($event->sender === null) { $event->sender = $this; } $event->handled = false; $event->name = $name; foreach ($this->_events[$name] as $handler) { $event->data = $handler[1]; call_user_func($handler[0], $event); // stop further handling if the event is handled if ($event->handled) { return; } } } // invoke class-level attached handlers Event::trigger($this, $name, $event);}
阅读全文
0 0
- Application的run方法分析
- Application.Run 方法
- Run application
- Application.Run() 和 System.Windows.Forms.Form.Show() 方法的区别
- spark java api通过run as java application运行的方法
- spark java api通过run as java application运行的方法
- eclipse 的项目存在main()方法 但是没有 run as java application
- spark java api通过run as java application运行的方法
- VCL中Application.Run()的过程
- application.run 某文件夹下的窗体
- vbs的Run方法
- angular的run方法
- POCO的Application分析
- node-sqlite3源码分析 - Database::run方法
- linux run java application
- Delphi Application.Run语句
- Run a simple application
- frm.Show();Application.Run
- 编写优质嵌入式C程序
- Dijkstra算法JAVA实现
- 【02】vue.js — 简易留言板
- centos上 java使用Tesseract进行ocr识别
- 跟我学NodeJS(三)之回调函数
- Application的run方法分析
- 数据量很大的排序问题 大量数据如何排序,没有做测试
- JAVA学习
- java中的枚举(enum)
- Dubbo服务框架详解
- AsyncTask原理详解!
- Ubuntu14.04安装visual studio code
- vue饿了么学习-第三篇(需求分析)
- Android OpenGL ES 绘图 -- 热力图