一个面向 PHP CLI 常驻进程的基础框架包,组合以下组件:
- Framework X:HTTP 路由与中间件
reactphp-x/worker:多进程 HTTP Worker 管理和信号重载vlucas/phpdotenv:.env配置加载
- PHP 8.1+
- Linux/macOS(不支持 Windows worker 模式)
pcntl、posix扩展
composer require reactphp-x/framework当前 reactphp-x/worker 只发布了 dev-master,因此项目需要允许 dev 稳定级别并优先选择稳定版本:
{
"minimum-stability": "dev",
"prefer-stable": true
}复制环境配置:
cp .env.example .env创建启动文件:
<?php
use FrameworkX\App;
use React\Http\Message\Response;
use ReactphpX\Framework\Application;
use ReactphpX\Framework\Environment;
require __DIR__ . '/vendor/autoload.php';
$server = Application::create(__DIR__);
$server->http(static function (Environment $env): App {
$app = new App();
$app->get('/', static fn () => Response::plaintext("Hello\n"));
return $app;
});
if ($server->environment()->string('APP_ENV', 'production') === 'development') {
$server->fileReload([__DIR__ . '/app', __DIR__ . '/config']);
}
$server->run();启动及管理命令由 reactphp-x/worker 提供:
php start.php start
php start.php start -d
php start.php status
php start.php reload
php start.php stop仓库内的完整示例可通过 php examples/start.php start 启动。
fileReload() 参考 game-needle 的 FileMonitor Worker 实现:
- 只在非 daemon 模式启用
- 监控 PHP 文件的新增、修改和删除
- 自动跳过
.git与vendor - 检测到变化后向 master 发送
SIGUSR1 - FileMonitor 自身不可重载,仅重载 HTTP Worker
可用配置及默认值见 .env.example。phpdotenv 使用 immutable 模式加载 .env,不会覆盖操作系统已经提供的环境变量。