zanphp源码解读 - 应用的启动

获取应用并启动

php bin/httpd

createHttpServer();
$server->start();

init/app.php

分析 Application

vendor/zanphp/framework/src/Foundation/Application.php

    public function __construct($appName, $basePath)
    {
        // 设置应用名称
        $this->appName = $appName;

        // 获取 本身实例
        static::setInstance($this);

        // 想容器注册单例
        ZanPHPContainer::getInstance()->instance(ApplicationContract::class, $this);

        // 设置 应用 基础路径
        $this->setBasePath($basePath);

        // 其他初始化工作
        $this->bootstrap();
    }

    protected function bootstrap()
    {
        // 初始化 容器
        $this->setContainer();
        // 其他初始化工作
        foreach ($this->bootstrapItems as $bootstrap) {
            $this->make($bootstrap)->bootstrap($this);
        }
    }
    
    /**
     * get http server. 创建 httpServer
     *
     * 根据 前面 的知识扫盲 可知道 返回的 真身 是 ZanPHP\HttpServer\Server 
     * 位于 vendor/zanphp/http-server/src/Server.php
     * @return \Zan\Framework\Network\Http\Server
     */
    public function createHttpServer()
    {
        /** @var Factory $factory */
        $factory = make(Factory::class, ["server"]);
        $server = $factory->createHttpServer();

        $this->server = $server;

        return $server;
    }

分析 Server.php

vendor/zanphp/http-server/src/Server.php
vendor/zanphp/server-base/src/ServerBase.php

/*
 * 继承 ZanPHP\ServerBase\ServerBase 
 * 这里 就 把 ServerBase 中的 函数 都放在 Server 分析了
 */
class Server extends ServerBase
{
    // 服务的启动主入口函数
    // 服务的启动主入口函数
    // 服务的启动主入口函数
    public function start();
    
    // 服务 启动初始化 包括 自定义的启动项  配置在  init/ServerStart/.config.php
    function bootServerStartItem();
    
    // worker 启动初始化 包括 自定义的启动项  配置在  init/WorkerStart/.config.php
    function bootServerStartItem();
    
    // 请求处理函数
    public function onRequest(SwooleHttpRequest $httpRequest, SwooleHttpResponse $httpResponse)
    {
        // ...
        /** 请求处理类 后续讲解 */
        (new RequestHandler())->handle($httpRequest, $httpResponse);
    }
}

你可能感兴趣的:(php)