From 6c74f1c8b59e9d1f4aef2ff38473d0e1499db686 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 23 May 2026 14:57:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=B8=B8=E9=87=8F=E5=92=8C=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除多余的类型常量间的空行,统一代码风格 - 添加构建模式相关常量 BUILD_MODE_BIN、BUILD_MODE_EXT、ENTRY_FUNCTION 等 - 将硬编码的字符串替换为对应的常量引用 - 移除不再使用的 phpx_func.h 头文件引用 - 重命名 cxxflags 为 cxxFlags 以符合命名规范 - 将构建模式判断逻辑封装为 isBuildModeBin 和 isBuildModeExt 方法 - 更新配置文件中的默认值为常量引用 - 移除不再需要的 trait 解析方法 --- src/Php/CompilerBase.php | 45 +++++++++++++++++++----------------- src/Php/Constants.php | 2 +- src/Php/Preprocessor.php | 2 +- src/Php/Translator.php | 49 ++++++++++++++++++---------------------- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 58f52227..743ea120 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -62,11 +62,8 @@ class CompilerBase extends \PhpAot\Core\Translator use Utils; public const string TYPE_VAR = 'php::Var'; - public const string TYPE_BOOL = 'php::Bool'; - public const string TYPE_INT = 'php::Int'; - public const string TYPE_FLOAT = 'php::Float'; public const string TYPE_OBJECT = 'php::Object'; public const string TYPE_ARRAY = 'php::Array'; @@ -78,10 +75,12 @@ class CompilerBase extends \PhpAot\Core\Translator public const string TYPE_STR = 'php::Str'; public const string TYPE_REF = 'php::Ref'; public const string TYPE_VOID = 'void'; + public const int DECL_TYPE_OF_RETURN = 1; public const int DECL_TYPE_OF_PROPERTY = 2; public const int DECL_TYPE_OF_CONST = 3; public const int DECL_TYPE_OF_PARAM = 4; + public const string VALUE_NAN = 'std::numeric_limits::quiet_NaN()'; public const string VALUE_INF = 'std::numeric_limits::infinity()'; public const string VALUE_NULL = 'php::null'; @@ -107,6 +106,11 @@ class CompilerBase extends \PhpAot\Core\Translator public const string OP_NOP = "if (0) {}\n"; // 超过65536字节的数组,将从栈上转移到堆 public const int MAX_BYTES_IN_STACK = 65536; + public const string BUILD_MODE_BIN = 'bin'; + public const string BUILD_MODE_EXT = 'ext'; + public const string ENTRY_FUNCTION = 'main'; + public const string PHPX_VENDOR_DIR = '/vendor/swoole/phpx'; + protected string $lang = 'PHP'; protected string $cppCompiler = ''; protected array $literalStrings = []; @@ -154,7 +158,6 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $globalHeaders = [ 'phpx.h', 'phpx_helper.h', - 'phpx_func.h', 'php_aot_helper.h', ]; protected array $localHeaders = []; @@ -176,8 +179,8 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $constData = []; protected int $optimizeLevel = 0; protected int $maxJob = 4; - protected string $buildMode = 'bin'; - protected string $cxxflags = ''; + protected string $buildMode = self::BUILD_MODE_BIN; + protected string $cxxFlags = ''; protected string $cxxStd = 'c++17'; protected string $ldflags = ''; protected int $floatPrecision = 17; @@ -397,26 +400,16 @@ class CompilerBase extends \PhpAot\Core\Translator } // 尝试使用 Composer 安装的 phpx - $composerPhpxDir = $this->rootPath . '/vendor/swoole/phpx'; + $composerPhpxDir = $this->rootPath . self::PHPX_VENDOR_DIR; if (is_dir($composerPhpxDir)) { return $composerPhpxDir; } - $composerPhpxVendorDir = $this->rootPath . '/vendor/swoole/phpx-vendor'; - if (is_dir($composerPhpxVendorDir)) { - return $composerPhpxVendorDir; - } - if (defined('ROOT_PATH')) { - $rootPhpxDir = ROOT_PATH . '/vendor/swoole/phpx'; + $rootPhpxDir = ROOT_PATH . self::PHPX_VENDOR_DIR; if (is_dir($rootPhpxDir)) { return $rootPhpxDir; } - - $rootPhpxVendorDir = ROOT_PATH . '/vendor/swoole/phpx-vendor'; - if (is_dir($rootPhpxVendorDir)) { - return $rootPhpxVendorDir; - } } // 两个路径都不存在,报错 @@ -461,6 +454,16 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->getPlatform() instanceof Macos; } + public function isBuildModeBin(): bool + { + return $this->buildMode === self::BUILD_MODE_BIN; + } + + public function isBuildModeExt(): bool + { + return $this->buildMode === self::BUILD_MODE_EXT; + } + public function getPhpDir(): string { try { @@ -2477,7 +2480,7 @@ class CompilerBase extends \PhpAot\Core\Translator // extension 和 bin 模式都需要链接 PHP 库 if ($platform instanceof Windows) { // Windows: 根据构建模式选择不同的库 - if ($this->buildMode === 'bin') { + if ($this->isBuildModeBin()) { // bin 模式:需要同时链接 php8ts.lib 和 php8embed.lib // 注意:php8ts.lib 必须在 php8embed.lib 之前,因为 embed 依赖 core // php8ts.lib 提供 PHP 核心全局符号(executor_globals, compiler_globals, sapi_globals) @@ -2537,7 +2540,7 @@ class CompilerBase extends \PhpAot\Core\Translator 'build_mode' => $this->buildMode, 'enable_profiler' => $this->enableProfiler, 'suppressed_warnings' => Constants::MSVC_SUPPRESSED_WARNINGS ?? [], - 'cxxflags' => $this->cxxflags, + 'cxxflags' => $this->cxxFlags, ]; $cmd .= $this->getCompilerBackend()->buildCompileOptions($config); @@ -2583,7 +2586,7 @@ class CompilerBase extends \PhpAot\Core\Translator 'build_mode' => $this->buildMode, 'enable_profiler' => $this->enableProfiler, 'suppressed_warnings' => Constants::MSVC_SUPPRESSED_WARNINGS ?? [], - 'cxxflags' => $this->cxxflags, + 'cxxflags' => $this->cxxFlags, ]; } diff --git a/src/Php/Constants.php b/src/Php/Constants.php index 841d60fe..5e34011a 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -116,7 +116,7 @@ class Constants 'prefix' => 'm', 'description' => 'Build mode, -m bin(binary) or -m ext(extension), default: bin', 'required' => false, - 'defaultValue' => 'bin', + 'defaultValue' => CompilerBase::BUILD_MODE_BIN, ], 'debug-line' => [ 'longPrefix' => 'debug-line', diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 2abd85a8..c6cd36a6 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -354,7 +354,7 @@ class Preprocessor extends CompilerBase $this->parseParams($v->params, $functionDef); // main 函数,返回值必须为 void 类型,参数必须为空或者 argc, argv 两个参数 - if (!$this->class and !$this->namespace and $fnName === 'main') { + if (!$this->class and !$this->namespace and $fnName === self::ENTRY_FUNCTION) { if (count($v->params) > 0) { if (count($v->params) != 2) { $this->fatalError($v, 'The parameters of the main function must be `(int $argc, array $argv)`.'); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 12ed97a5..8e215eae 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -62,7 +62,7 @@ class Translator extends Preprocessor // 只读取命令行参数,不立即应用(等待 YAML 解析后再应用) // 这样可以确保优先级:命令行 > YAML > 默认值 $this->internalFunctions = array_flip(get_defined_functions()['internal']); - unset($this->internalFunctions['main']); + unset($this->internalFunctions[self::ENTRY_FUNCTION]); $this->internalConstants = get_defined_constants(); if ($this->climate->arguments->defined('help')) { $this->showUsage(); @@ -286,7 +286,7 @@ class Translator extends Preprocessor public function prepare(string $path): array { // 根据平台检查库文件(仅在构建二进制文件时需要) - if ($this->buildMode === 'bin') { + if ($this->isBuildModeBin()) { foreach ($this->getPlatform()->getBuildLibraryWarnings($this->getPhpDir(), $this->getPhpxDir(), $this->buildMode) as $message) { $this->climate->warning($message['warning']); if (!empty($message['info'])) { @@ -420,8 +420,8 @@ class Translator extends Preprocessor public function genExtension(): string { - if ($this->buildMode == 'bin') { - if (!$this->hasFunction('main')) { + if ($this->isBuildModeBin()) { + if (!$this->hasFunction(self::ENTRY_FUNCTION)) { $this->climate->red('When the build mode is a binary executable file, the `main()` function must be defined'); exit(1); } @@ -433,7 +433,7 @@ class Translator extends Preprocessor $code = $this->genIncludeHeaderFiles(); - if ($this->buildMode === 'bin') { + if ($this->isBuildModeBin()) { $cliHeaders = [ '#include "php_cli_process_title.h"', '#include "php_cli_process_title_arginfo.h"', @@ -522,13 +522,13 @@ CODE; $code .= "// clang-format off\n"; $code .= "static const zend_function_entry ext_functions[] = {\n"; - if ($this->buildMode === 'bin') { + if ($this->isBuildModeBin()) { $code .= $this->getIndent() . "PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)\n"; $code .= $this->getIndent() . "PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)\n"; } foreach ($this->functions as $functionDef) { - if ($this->buildMode === 'ext' and $functionDef->name === 'main') { + if ($this->isBuildModeExt() and $functionDef->name === self::ENTRY_FUNCTION) { continue; } if ($functionDef->method) { @@ -612,8 +612,8 @@ CODE; $code .= 'php::request_init();' . PHP_EOL; $code .= 'php_app_init();' . PHP_EOL; - if ($this->buildMode === 'bin') { - if (count($this->functions['main']->argInfoList) == 2) { + if ($this->isBuildModeBin()) { + if (count($this->functions[self::ENTRY_FUNCTION]->argInfoList) == 2) { $code .= 'php::eval("global $argc, $argv; main($argc, $argv);");' . PHP_EOL; } else { $code .= 'php::eval("main();");' . PHP_EOL; @@ -646,7 +646,7 @@ zend_module_entry {$moduleName}_module_entry = { CODE; $code .= PHP_EOL . PHP_EOL; - if ($this->buildMode === 'ext') { + if ($this->isBuildModeExt()) { $code .= "ZEND_GET_MODULE({$moduleName});\n"; } else { $code .= 'zend_module_entry *' . self::PREFIX . 'embed_get_module() {' . PHP_EOL; @@ -786,7 +786,7 @@ CODE; $sourceFiles[] = $this->genExtension(); // embed 需要 main 函数,以及 cli 的内置函数定义 - if ($this->getBuildMode() == 'bin') { + if ($this->isBuildModeBin()) { $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'; @@ -1321,13 +1321,13 @@ CODE; $list = $this->getFilesFromDir($projectDir); } - // 读取 cxxflags(支持中横线和下划线) - $cxxflags = $cfg['cxx-flags'] ?? $cfg['cxxflags'] ?? null; - if (!empty($cxxflags)) { - if (is_array($cxxflags)) { - $this->cxxflags = implode(' ', $cxxflags); + // 读取 cxx-flags(支持中横线和下划线) + $cxxFlags = $cfg['cxx-flags'] ?? $cfg['cxxflags'] ?? null; + if (!empty($cxxFlags)) { + if (is_array($cxxFlags)) { + $this->cxxFlags = implode(' ', $cxxFlags); } else { - $this->cxxflags = str_replace("\n", ' ', $cxxflags); + $this->cxxFlags = str_replace("\n", ' ', $cxxFlags); } } @@ -1363,11 +1363,11 @@ CODE; if (!empty($buildMode)) { // 映射常见的类型名称到内部 buildMode $modeMap = [ - 'extension' => 'ext', - 'ext' => 'ext', - 'binary' => 'bin', - 'bin' => 'bin', - 'cli' => 'bin', + 'extension' => self::BUILD_MODE_EXT, + 'ext' => self::BUILD_MODE_EXT, + 'binary' => self::BUILD_MODE_BIN, + 'bin' => self::BUILD_MODE_BIN, + 'cli' => self::BUILD_MODE_BIN, ]; $mappedMode = $modeMap[strtolower($buildMode)] ?? $buildMode; $this->setBuildMode($mappedMode); @@ -2121,11 +2121,6 @@ CODE; return $code; } - protected function parseTrait(Node\Stmt\Trait_ $trait) - { - throw new Unsupported('Unsupported Trait '); - } - private function getRegisterClassFunctionArgDef(ClassDef|InterfaceDef $classDef): string { $depsCeList = $this->getRegisterClassFunctionCeList($classDef);