php bin/httpd
php
<?php #!/usr/bin/env php <?php /** @var \Zan\Framework\Foundation\Application $app */ $app = require_once __DIR__.'/../init/app.php'; $server = $app->createHttpServer(); $server->start();
init/app.phpbootstrap
<?php use Zan\Framework\Foundation\Application; require __DIR__ . '/../vendor/autoload.php'; $appName = 'http-demo'; $rootPath = realpath(__DIR__.'/../'); $app = new Application($appName, $rootPath); return $app;
vendor/zanphp/framework/src/Foundation/Application.phpapp
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; }
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); } }