From d78a43099d9abe007a61f62fdc04888a3499149d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 23 Apr 2026 14:38:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=20PHP=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=99=A8=E5=9F=BA=E7=A1=80=E7=B1=BB=E4=BB=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20phpx=20=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除硬编码的 phpx 目录路径,改用动态获取方法 - 将 phpx 目录设置方法从公共改为受保护,并返回 vendor 中的 phpx 路径 - 更新 include 和 lib 目录路径为从 phpx 库动态获取 - 在 composer.json 中添加 swoole/phpx 依赖 - 添加构建前检查 php-config 和必要库文件的验证逻辑 - 更新源文件路径为从 phpx 库目录中获取 - 为 debug-info 参数添加默认值配置 --- composer.json | 3 ++- composer.lock | 46 +++++++++++++++++++++++++++++++++++++++- src/Php/CompilerBase.php | 11 +++++----- src/Php/Constants.php | 1 + src/Php/Translator.php | 18 +++++++++++++--- 5 files changed, 68 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 055c493b..21d51350 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "league/climate": "^3.10", "marcj/topsort": "^2.0", "symfony/var-dumper": "^8.0", - "symfony/yaml": "^8.0" + "symfony/yaml": "^8.0", + "swoole/phpx": "~2.1.3" }, "require-dev": { "phpunit/phpunit": "^10.4", diff --git a/composer.lock b/composer.lock index f4e53f0d..27b186a0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9cc0c6bd977c8959d0cd9596d3962b37", + "content-hash": "b85ec97da999339a1cebcf7664e5465b", "packages": [ { "name": "league/climate", @@ -297,6 +297,50 @@ }, "time": "2020-12-15T21:32:01+00:00" }, + { + "name": "swoole/phpx", + "version": "v2.1.4", + "source": { + "type": "git", + "url": "https://github.com/swoole/phpx.git", + "reference": "2f6d8de5333085c0d8f7975ca9b6b6cdb74057b6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/swoole/phpx/zipball/2f6d8de5333085c0d8f7975ca9b6b6cdb74057b6", + "reference": "2f6d8de5333085c0d8f7975ca9b6b6cdb74057b6", + "shasum": "" + }, + "require": { + "league/climate": "^3.10" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.75", + "phpunit/phpunit": "^10.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Phpx\\": "src/php" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "description": "C++ wrapper for Zend API", + "keywords": [ + "ZendAPI", + "embedded", + "extension", + "php" + ], + "support": { + "issues": "https://github.com/swoole/phpx/issues", + "source": "https://github.com/swoole/phpx/tree/v2.1.4" + }, + "time": "2026-04-23T06:18:21+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.36.0", diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index b0d0fd56..f2f730d7 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -88,7 +88,6 @@ class CompilerBase extends \PhpAot\Core\Translator public const string OP_NOT_EMPTY = 'notEmpty'; public const string OP_REFVAL = 'toReference'; public const string OP_NOP = "if (0) {}\n"; - protected string $phpxDir = '~/workspace/projects/phpx'; protected string $lang = 'PHP'; protected string $cppCompiler = 'g++'; protected array $literalStrings = []; @@ -276,9 +275,9 @@ class CompilerBase extends \PhpAot\Core\Translator // $this->noLiteralStrings = $climate->arguments->get('no-literal-strings'); } - public function setPhpxDir($dir): void + protected function getPhpxDir(): string { - $this->phpxDir = $dir; + return $this->rootPath . '/vendor/swoole/phpx'; } public function save(string $code, string $file): void @@ -2021,9 +2020,9 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseIncludes(): string { $list = [ - $this->phpxDir . '/include', + $this->getPhpxDir() . '/include', $this->getBuildDir() . '/include', - $this->rootPath . '/src/cpp', + $this->getPhpxDir() . '/src/misc', ]; $out = '$(php-config --includes) '; foreach ($list as $li) { @@ -2037,7 +2036,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $list = [ '$(php-config --prefix)/lib', - $this->phpxDir . '/lib', + $this->getPhpxDir() . '/lib', ]; $out = ''; foreach ($list as $li) { diff --git a/src/Php/Constants.php b/src/Php/Constants.php index 3c1706ae..83941af4 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -128,6 +128,7 @@ class Constants 'longPrefix' => 'debug-info', 'description' => 'Enable debug info', 'required' => false, + 'defaultValue' => 0, ], 'job' => [ 'prefix' => 'j', diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 03e02e3f..bc71313e 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -211,6 +211,18 @@ class Translator extends Preprocessor public function prepare(string $path): array { + $phpDir = shell_exec('php-config --prefix'); + if (empty($phpDir)) { + $this->error("The `php-config` is not found"); + } + $phpDir = trim($phpDir); + if (!is_file($phpDir . '/lib/libphp.so')) { + $this->error("The `libphp.so` is not found, please run `make` to build it"); + } + if (!is_file($this->getPhpxDir() . '/lib/libphpx.so')) { + $this->error("The `libphpx.so` is not found, please run `make` to build it"); + } + $files = $this->getFiles($path); // 应用 ignorePaths 过滤 if (!empty($this->ignorePaths)) { @@ -639,9 +651,9 @@ CODE; // embed 需要 main 函数,以及 cli 的内置函数定义 if ($this->getBuildMode() == 'bin') { - $sourceFiles[] = __DIR__ . '/../cpp/main.cc'; - $sourceFiles[] = __DIR__ . '/../cpp/php_cli_process_title.c'; - $sourceFiles[] = __DIR__ . '/../cpp/ps_title.c'; + $sourceFiles[] = $this->getPhpxDir() . '/src/misc/main.cc'; + $sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_cli_process_title.c'; + $sourceFiles[] = $this->getPhpxDir() . '/src/misc/ps_title.c'; } // 并行编译