swoft运行流程
启动命令 php bin/swoft http:start
或者 swoftctl run -c http:start
1 入口文件 bin/swoft.php
#!/usr/bin/env php
<?php // Bootstrap
require_once __DIR__ . '/bootstrap.php'; Swoole\Coroutine::set([
'max_coroutine' => 300000,
]); // Run application
(new \App\Application())->run();
new Application 进入文件 app/Application
<?php declare(strict_types=1);
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://swoft.org/docs
* @contact group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/ namespace App; use Swoft\SwoftApplication;
use function date_default_timezone_set; /**
* Class Application
*
* @since 2.0
*/
class Application extends SwoftApplication
{
protected function beforeInit(): void
{
parent::beforeInit(); // you can init php setting.
date_default_timezone_set('Asia/Shanghai'); // 设置时区
} /**
* @return array
*/
public function getCLoggerConfig(): array
{
$config = parent::getCLoggerConfig(); // False: Dont print log to terminal
$config['enable'] = true; return $config;
}
}
发现他继承 SwoftApplication 要执行父类的construct方法
进入 SwoftApplication 发现初始化方法
/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
// Check runtime env 检查运行环境是否符合 php版本 swoole版本
SwoftHelper::checkRuntime(); // Storage as global static property. Swoft::$app = $this; // 当前对象赋值给 // Before init
$this->beforeInit(); // Init console logger
$this->initCLogger(); //初始化 打印到console界面的日志系统 // Can setting properties by array if ($config) {
ObjectHelper::init($this, $config); // 初始化配置
} // Init application
$this->init(); // 真真的初始化 // After init
$this->afterInit(); // 初始化完执行
}
进入 $this->init()
protected function init(): void
{
// Init system path aliases
$this->findBasePath(); // 找到当前基本路径
$this->setSystemAlias(); // 设置系统别名 $processors = $this->processors(); //重要 实例化几个进程 返回 $this->processor = new ApplicationProcessor($this); // 实例化$this->processor 也就是当前application运用的属性 后面会执行他的handel方法
$this->processor->addFirstProcessor(...$processors); // 把第一个processor添加到 $this->processors
}
/**
* @return ProcessorInterface[]
*/
protected function processors(): array
{
return [
new EnvProcessor($this), // 环境
new ConfigProcessor($this), // 配置
new AnnotationProcessor($this), // 注解
new BeanProcessor($this), // bean
new EventProcessor($this), // 事件
new ConsoleProcessor($this),
];
}
$this->processor = new ApplicationProcessor($this);
调用每个process的handel方法
/**
* Handle application processors
*/
public function handle(): bool
{
$disabled = $this->application->getDisabledProcessors();
foreach ($this->processors as $processor) {
$class = get_class($processor);
// If is disabled, skip handle.
if (isset($disabled[$class])) {
continue;
}
$processor->handle();
}
return true;
}
$this->processor->addFirstProcessor(...$processors); // 把这些对象都丢到$this->processors里面
public function addFirstProcessor(Processor ...$processor): bool
{
array_unshift($this->processors, ... $processor);
return true;
}
实例化 基本执行完成,开始run
// Run application
(new \App\Application())->run();
/**
* Run application
*/
public function run(): void
{
try {
if (!$this->beforeRun()) {
return;
}
$this->processor->handle();
} catch (Throwable $e) {
Console::colored(sprintf('%s(code:%d) %s', get_class($e), $e->getCode(), $e->getMessage()), 'red');
Console::colored('Code Trace:', 'comment');
echo $e->getTraceAsString(), "\n";
}
}
调用当前对象的 handle方法
public function handle(): bool
{
$disabled = $this->application->getDisabledProcessors();
foreach ($this->processors as $processor) {
$class = get_class($processor);
// If is disabled, skip handle.
if (isset($disabled[$class])) {
continue;
}
$processor->handle();
}
return true;
}
这个时候 将之前保存到$this->pkrocesssors里面的对象的handle方法执行一遍
初始化完成
p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
string(29) "Swoft\Processor\BeanProcessor"
p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
string(31) "Swoft\Processor\ConfigProcessor"
string(35) "Swoft\Processor\AnnotationProcessor"
p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
string(28) "Swoft\Processor\EnvProcessor"
p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
string(32) "Swoft\Processor\ConsoleProcessor"
代码位于framwork/processor
比如 执行beanprocessor 下面的handle方法 做了一大堆事情,好像是初始bean化容器 把注解 定义 解析等都挂到container的属性上去
BeanFactory::addDefinitions($definitions);
BeanFactory::addAnnotations($annotations);
BeanFactory::addParsers($parsers);
BeanFactory::setHandler($handler);
BeanFactory::init();
public static function addAnnotations(array $annotations): void
{
Container::getInstance()->addAnnotations($annotations);
}
public function addAnnotations(array $annotations): void
{
$this->annotations = ArrayHelper::merge($this->annotations, $annotations);
}
<?php declare(strict_types=1); namespace Swoft\Processor; use InvalidArgumentException;
use ReflectionException;
use Swoft\Annotation\AnnotationRegister;
use Swoft\Annotation\Exception\AnnotationException;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\BeanFactory;
use Swoft\Bean\Container;
use Swoft\BeanHandler;
use Swoft\Config\Config;
use Swoft\Contract\DefinitionInterface;
use Swoft\Helper\SwoftHelper;
use Swoft\Log\Helper\CLog;
use Swoft\Stdlib\Helper\ArrayHelper;
use function alias;
use function file_exists;
use function get_class;
use function sprintf; /**
* Class BeanProcessor
*
* @since 2.0
*/
class BeanProcessor extends Processor
{
/**
* Handle bean
*
* @return bool
* @throws ReflectionException
* @throws AnnotationException
*/
public function handle(): bool
{
if (!$this->application->beforeBean()) {
return false;
} $handler = new BeanHandler(); $definitions = $this->getDefinitions();
$parsers = AnnotationRegister::getParsers();
$annotations = AnnotationRegister::getAnnotations(); BeanFactory::addDefinitions($definitions); BeanFactory::addAnnotations($annotations);
BeanFactory::addParsers($parsers);
BeanFactory::setHandler($handler);
BeanFactory::init(); $stats = BeanFactory::getStats();
CLog::info('Bean is initialized(%s)', SwoftHelper::formatStats($stats)); /* @var Config $config */
$config = BeanFactory::getBean('config');
CLog::info('Config path is %s', $config->getPath()); if ($configEnv = $config->getEnv()) {
CLog::info('Config env=%s', $configEnv);
} else {
CLog::info('Config env is not setting');
} return $this->application->afterBean();
} /**
* Get bean definitions
*
* @return array
*/
private function getDefinitions(): array
{
// Core beans
$definitions = [];
$autoLoaders = AnnotationRegister::getAutoLoaders(); // get disabled loaders by application
$disabledLoaders = $this->application->getDisabledAutoLoaders(); foreach ($autoLoaders as $autoLoader) {
if (!$autoLoader instanceof DefinitionInterface) {
continue;
} $loaderClass = get_class($autoLoader); // If the component is disabled by app.
if (isset($disabledLoaders[$loaderClass])) {
CLog::info('Auto loader(%s) is <cyan>DISABLED</cyan>, skip handle it', $loaderClass);
continue;
} // If the component is disabled by self.
if (!$autoLoader->isEnable()) {
CLog::info('Auto loader(%s) is <cyan>DISABLED</cyan>, skip handle it', $loaderClass);
continue;
} $definitions = ArrayHelper::merge($definitions, $autoLoader->beans());
} // Application bean definitions
$beanFile = alias($this->application->getBeanFile()); if (!file_exists($beanFile)) {
throw new InvalidArgumentException(sprintf('The bean config file of %s is not exist!', $beanFile));
} /** @noinspection PhpIncludeInspection */
$beanDefinitions = require $beanFile; return ArrayHelper::merge($definitions, $beanDefinitions);
}
}
swoft运行流程的更多相关文章
- react-native start 运行流程
在CMD下键入 C:\Node_JS\MyAwesomeProject>react-native start 运行流程: C:\Users\Grart\AppData\Roaming\npm\r ...
- 1、CC2541蓝牙4.0芯片中级教程——基于OSAL操作系统的运行流程了解+定时器和串口例程了解
本文根据一周CC2541笔记汇总得来—— 适合概览和知识快速索引—— 全部链接: 中级教程-OSAL操作系统\OSAL操作系统-实验01 OSAL初探 [插入]SourceInsight-工程建立方法 ...
- java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)
JAVA里面的程序运行流程分三大类: 1,顺序结构:顺序结构就是依次执行每一行代码 2,分支结构:分支结构就是按不同的条件进行分支 3,循环结构:一段代码依条件进行循环执行. 其中,分支结构有两大类: ...
- servlet运行流程
servlet运行流程 (2013-06-19 19:16:43) 转载▼ 首先Servlet被部署到Web容器中,当客户端发送调用这个Servlet的请求到达Web容器时,Web容器会先判 ...
- [原创]java WEB学习笔记70:Struts2 学习之路-- struts2拦截器源码分析,运行流程
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Struts2框架的运行流程
Struts2的运行流程 1.浏览器发送请求到控制器(如Struts2中的核心控制器StrutsPrepareAndExecuteFilter): 2.控制器调用Action的execute方法: 3 ...
- 转:[gevent源码分析] 深度分析gevent运行流程
[gevent源码分析] 深度分析gevent运行流程 http://blog.csdn.net/yueguanghaidao/article/details/24281751 一直对gevent运行 ...
- Struts2运行流程分析
一.Struts2运行流程图: 二.运行流程分析: 1. 请求发送给StrutsPrepareAndExecuteFilter 2.StrutsPrepareAndExecuteFilter询问Act ...
- Struts2的运行流程以及关键拦截器介绍
Struts2的运行流程 1.ActionProxy是Action的一个代理类,也就是说Action的调用是通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方 ...
随机推荐
- Face The Right Way(POJ 3276)
原题如下: Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6708 Accepte ...
- Django进入监听端口就自动打开指定页面,无需导航栏手动添加(Django六)
在我们进入监听端口时画面如下:而因为在urls.py中写过如下语句 我们在监听端口后加上/login就会跳转到login.html页面,如下图 那么如何一打开监听端口就可以走动跳转到login.htm ...
- Linux驱动之设备树的基础知识
前期知识 1. 如何编写一个简单的Linux驱动(一)--驱动的基本框架 2. 如何编写一个简单的Linux驱动(二)--设备操作集file_operations 3. 如何编写一个简单的 ...
- C#开发PACS医学影像处理系统(十四):处理Dicom影像窗宽窗位
概念解释(网络资料): 窗宽: 窗宽指CT图像所显示的CT 值范围.在此CT值范围内的组织结构按其密度高低从白到黑分为16 个灰阶以供观察对比.例如,窗宽选定为100 Hu ,则人眼可分辨的CT值为1 ...
- 面向对象技术-原文转载自jingwhale
面向对象技术 --原文转载自jingwhale,文章底部附原文链接 对象是面向对象的程序设计的核心,它由描述状态的属性(变量)和用来实现对象行为的方法(函数)组成,完成了从数据模型到处理模型的结合与统 ...
- MyBatis学习(四)代码生成器MyBatis-Generator
一.简介 前面写过一篇文章介绍了如何使用Mybatis,那么如果我门数据库中有许许多多的表的时候,每张表都手动去写对应的mapper的映射关系,会非常麻烦,那么我们可以使用代码生成器MyBatis-G ...
- 转载:tensorflow slim模块用法
https://www.cnblogs.com/hellcat/p/8058092.html
- Vue 属性渲染
属性渲染 关于标签的属性渲染统一使用v-bind属性指令,比如轮播图的src全部经过后端获得,所以我们需要对src属性做动态渲染. 基本使用 使用v-bind属性指令,动态绑定图片的地址. <b ...
- 微服务实战系列(七)-网关springcloud gateway
1. 场景描述 springcloud刚推出的时候用的是netflix全家桶,路由用的zuul,但是据说zull1.0在大数据量访问的时候存在较大性能问题,2.0就没集成到springcloud中了, ...
- cookie、session和md5加密
cookie Http无状态协议,只能在同一个网站(包括多个页面)下获取,存储在客户端本地的一段信息,帮助我们存储信息获取信息.但是同样有风险:我们自己在浏览器上可以操作或者设置Cookie. con ...