启动命令 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运行流程的更多相关文章

  1. react-native start 运行流程

    在CMD下键入 C:\Node_JS\MyAwesomeProject>react-native start 运行流程: C:\Users\Grart\AppData\Roaming\npm\r ...

  2. 1、CC2541蓝牙4.0芯片中级教程——基于OSAL操作系统的运行流程了解+定时器和串口例程了解

    本文根据一周CC2541笔记汇总得来—— 适合概览和知识快速索引—— 全部链接: 中级教程-OSAL操作系统\OSAL操作系统-实验01 OSAL初探 [插入]SourceInsight-工程建立方法 ...

  3. java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)

    JAVA里面的程序运行流程分三大类: 1,顺序结构:顺序结构就是依次执行每一行代码 2,分支结构:分支结构就是按不同的条件进行分支 3,循环结构:一段代码依条件进行循环执行. 其中,分支结构有两大类: ...

  4. servlet运行流程

    servlet运行流程  (2013-06-19 19:16:43) 转载▼     首先Servlet被部署到Web容器中,当客户端发送调用这个Servlet的请求到达Web容器时,Web容器会先判 ...

  5. [原创]java WEB学习笔记70:Struts2 学习之路-- struts2拦截器源码分析,运行流程

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Struts2框架的运行流程

    Struts2的运行流程 1.浏览器发送请求到控制器(如Struts2中的核心控制器StrutsPrepareAndExecuteFilter): 2.控制器调用Action的execute方法: 3 ...

  7. 转:[gevent源码分析] 深度分析gevent运行流程

    [gevent源码分析] 深度分析gevent运行流程 http://blog.csdn.net/yueguanghaidao/article/details/24281751 一直对gevent运行 ...

  8. Struts2运行流程分析

    一.Struts2运行流程图: 二.运行流程分析: 1. 请求发送给StrutsPrepareAndExecuteFilter 2.StrutsPrepareAndExecuteFilter询问Act ...

  9. Struts2的运行流程以及关键拦截器介绍

    Struts2的运行流程 1.ActionProxy是Action的一个代理类,也就是说Action的调用是通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方 ...

随机推荐

  1. Python3实现zip分卷压缩

    Python实现zip分卷压缩 使用 zipfile 库 查看 官方中文文档 利用 Python 压缩 ZIP 文件,我们第一反应是使用 zipfile 库,然而,它的官方文档中却明确标注" ...

  2. [Leetcode]585. 2016年的投资(MySQL)

    题目 写一个查询语句,将 2016 年 (TIV_2016) 所有成功投资的金额加起来,保留 2 位小数. 对于一个投保人,他在 2016 年成功投资的条件是: 他在 2015 年的投保额 (TIV_ ...

  3. threading之线程的开始,暂停和退出

    目录 背景 实现代码 背景 利用多线程实现一个开关功能,需要对产生的线程进行管理(例如:开启,暂停,关闭等操作). 实现代码 任务脚本: #!/usr/bin/python3 # _*_ coding ...

  4. JAVA MD5加密算法实现与原理解析

    public static String md5Encode(String inputStr) { MessageDigest md5 = null; try { md5 = MessageDiges ...

  5. Hibernate4.3基础知识1

    一.Hibernate 开发环境搭建 4.3 1.导包    2.创建hibernate.cfg.xml配置文件   3.创建实体类   4.创建映射文件 实体类名.hbm.xml  配置文件 二.h ...

  6. Dotnet Core IHttpClientFactory深度研究

    今天,我们深度研究一下IHttpClientFactory.   一.前言 最早,我们是在Dotnet Framework中接触到HttpClient. HttpClient给我们提供了与HTTP交互 ...

  7. mysql-16-variables

    #变量 /* 系统变量: 全局变量 会话变量 自定义变量: 用户变量 局部变量 */ # 一.系统变量 #由系统提供,属于服务器层面 #1.查看所有的系统变量 show global variable ...

  8. JS实现动态显示时间(最简单方法)

    使用JS实现动态显示时间 最简单实现方法 直接在网页适当的位置中插入如下js代码,(id="datetime") 不可省略. <div id="datetime&q ...

  9. 【题解】[CQOI]动态逆序对

    题目链接 题意如题,维护一个动态序列的逆序对总数. 注意题目给的是\([1,n]\)的排列,所以没必要离散化了. 考虑逆序对:二维偏序可以用树状数组做,现在是三维偏序,即加了一个时间维度. 找一个数前 ...

  10. Doug Lea在J.U.C包里面写的BUG又被网友发现了

    这是why的第 69 篇原创文章 BUG描述 一个编号为 8073704 的 JDK BUG,将串联起我的这篇文章. 也就是下面的这个链接. https://bugs.openjdk.java.net ...