运行时模块
kuiper/runtime 是 Kuiper 应用的启动入口,基于 Symfony Runtime 组件扩展。它负责:加载配置与 .env、构建 DI 容器、自动装配框架模块,并按运行环境选择 runner——普通 PHP SAPI、Swoole HTTP 服务器或 FrankenPHP worker。
特性
- 一套入口多端运行: 同一个
public/index.php,通过SERVER_MODE环境变量切换 php / swoole / frankenphp - 自动装配清单: 聚合各 composer 包
extra.kuiper声明的配置类(Spring Boot autoconfigure 式),业务无需手工维护配置类列表 - 配置分层合并: runtime 默认值 →
src/config.php→ 容器配置,占位符统一展开 - 组件扫描缓存: 扫描结果可缓存到内存或文件,加速每次请求的容器构建
- 请求生命周期事件: 每个请求分发
RequestStarted/RequestFinished(详见生命周期) - 协程友好: Swoole worker 内重建容器,避免 fork 复用主进程连接
快速开始
入口文件
入口是一个返回闭包的 PHP 文件。闭包接收 DI 容器,返回应用对象:
php
<?php
declare(strict_types=1);
// public/index.php
use kuiper\runtime\MappedRequestHandler;
use Psr\Container\ContainerInterface;
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (ContainerInterface $container): MappedRequestHandler {
return $container->get(MappedRequestHandler::class);
};返回对象的类型决定 runner:
- 返回
Psr\Http\Server\RequestHandlerInterface→ HTTP 服务(php / swoole / frankenphp 三种模式) - 返回其他对象(如 Symfony Console 应用)→ 走 Symfony Runtime 默认的 console runner(命令行模块)
project_dir 是必需的 runtime 选项,通常由 Symfony Runtime 从入口文件位置推断;缺失时启动直接报错并提示设置 $_SERVER['APP_RUNTIME_OPTIONS']。
启动方式
bash
# PHP 内置服务器(开发)
php -S localhost:8000 -t public vendor/autoload_runtime.php
# Swoole HTTP 服务器(生产,需 ext-swoole)
SERVER_MODE=swoole php public/index.php
# FrankenPHP worker 模式
SERVER_MODE=frankenphp php public/index.php
# 命令行命令
php public/index.php app:hello kuiper多应用入口(HTTP + CLI)
MappedRequestHandler 可以把不同路径前缀(或端口)映射到不同 handler,让一个入口同时服务 Web 与 RPC 等:
php
<?php
declare(strict_types=1);
use kuiper\runtime\MappedRequestHandler;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
return function (ContainerInterface $container): MappedRequestHandler {
return new MappedRequestHandler(
// Web 模块把应用 handler 注册为 RequestHandlerInterface
$container->get(RequestHandlerInterface::class),
[
// /healthz 交给独立的健康检查 handler,其余走 Web
[MappedRequestHandler::byPathPrefix('healthz'), $container->get(HealthCheckHandler::class)],
// 端口 9000 上的请求交给内部管理 handler
[MappedRequestHandler::byPort(9000), $container->get(AdminHandler::class)],
],
);
};匹配规则:byPathPrefix('rpc') 匹配 /rpc 与 /rpc/xxx,但不匹配 /rpcs;空前缀匹配所有请求;byPort() 按端口匹配(URI 无端口时回退 SERVER_PORT)。未命中的请求交给 fallback。
Runner 选择
Runtime::getRunner() 按「应用类型 + SERVER_MODE」选择 runner:
| 应用类型 | SERVER_MODE | Runner | 说明 |
|---|---|---|---|
| PSR-15 handler | 未设置 / php | Psr15Runner | php -S、php-fpm、FrankenPHP classic 模式 |
| PSR-15 handler | swoole | SwooleHttpRunner | Swoole HTTP 服务器(需 ext-swoole) |
| PSR-15 handler | frankenphp | FrankenPhpWorkerRunner | FrankenPHP worker 模式 |
| 其他(console 等) | — | Symfony Runtime 默认 runner | 交给父类处理 |
规则细节:
SERVER_MODE取值大小写不敏感(读入后 trim + 小写),非法值抛InvalidArgumentException,提示合法取值php, swoole, frankenphp- FrankenPHP worker 自动识别:
FRANKENPHP_WORKER=1时即使SERVER_MODE=php(或未设置)也走 worker runner;两种触发方式在frankenphp_handle_request()不可用时以同款错误信息失败,前缀区分来源 - Swoole / FrankenPHP worker 模式下,handler 与事件分发器在 worker 进程内通过
applicationFactory重建(同一容器),避免 fork 复制主进程已建立的连接
配置加载与容器构建
启动时容器构建流程(Runtime::createContainer()):
- 加载配置:
loadConfig()写入 runtime 默认键(application.*)与环境变量快照(ENV.*),再merge()业务配置文件application.config_file(默认src/config.php) - 读取容器配置:
application.container_config(默认src/container.php),或 composer.jsonextra.kuiper.config-file指定的文件 - 聚合自动装配清单:
AutoConfigurationLoader从根 composer.json 与vendor/composer/installed.json收集各包extra.kuiper声明的配置类与扫描命名空间 - 构建容器:
ContainerBuilder按序注册配置类(框架默认在前、应用显式声明在后)、合并组件扫描、执行组件扫描 - 展开占位符:
replacePlaceholder()解析{XXX}引用,ENV.*前缀键跳过
详见配置指南。
Swoole / FrankenPHP 的连接安全
Swoole 通过 fork 启动 worker,主进程里已建立的连接(数据库、Redis 等)会被复制到各 worker。因此:
- handler 必须在
onWorkerStart里重建容器后重新创建(createApplicationFactory逻辑) - 容器重建失败默认直接抛出,不回退 fork 副本
SWOOLE_ALLOW_FORK_FALLBACK=true是显式的逃生舱:复用 fork 副本让服务至少能跑,但可能复用主进程已建立的 DB/Redis 连接,只应在排障时临时开启- 事件分发器必须来自 worker 内重建的同一个容器(容器里的 Telemetry 是单例,跨容器会 flush 到空的 Provider 上)
文档导航
相关资源
- Symfony Runtime 文档
- Web 框架模块 - PSR-15 handler 的实现
- 命令行模块 - console runner 下的命令