refactor(php): 重构 PHP 编译器基础类以使用 phpx 库

- 移除硬编码的 phpx 目录路径,改用动态获取方法
- 将 phpx 目录设置方法从公共改为受保护,并返回 vendor 中的 phpx 路径
- 更新 include 和 lib 目录路径为从 phpx 库动态获取
- 在 composer.json 中添加 swoole/phpx 依赖
- 添加构建前检查 php-config 和必要库文件的验证逻辑
- 更新源文件路径为从 phpx 库目录中获取
- 为 debug-info 参数添加默认值配置
pull/1/head
韩天峰 4 months ago
parent b4d82a1bad
commit d78a43099d
  1. 3
      composer.json
  2. 46
      composer.lock
  3. 11
      src/Php/CompilerBase.php
  4. 1
      src/Php/Constants.php
  5. 18
      src/Php/Translator.php

@ -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",

46
composer.lock generated

@ -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",

@ -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) {

@ -128,6 +128,7 @@ class Constants
'longPrefix' => 'debug-info',
'description' => 'Enable debug info',
'required' => false,
'defaultValue' => 0,
],
'job' => [
'prefix' => 'j',

@ -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';
}
// 并行编译

Loading…
Cancel
Save