From 9de6ca6a41586070385a1e3f9d79feaa5fc46330 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 10 Feb 2026 20:01:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E6=94=AF=E6=8C=81=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=89=93=E5=8D=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 PhpParser\PrettyPrinter 用于代码格式化 - 添加 PrettyPrinter 实例变量用于代码美化输出 - 实现 parseEnum 方法处理枚举语法解析 - 添加 genEmbeddedCode 方法生成嵌入式代码 - 在语法解析器中添加 Stmt_Enum 分支处理 - 修复 parseStatic 方法参数类型声明 - 优化原生静态方法调用逻辑增加空值检查 - 添加枚举功能的测试用例文件 --- src/Php/CompilerBase.php | 35 ++++++++++++++++++++++++++--------- tests/aot/enum2.phpt | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 tests/aot/enum2.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8e796772..21241274 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -27,6 +27,7 @@ use PhpParser\Node\UnionType; use PhpParser\NodeAbstract; use PhpParser\Parser; use PhpParser\ParserFactory; +use PhpParser\PrettyPrinter; class CompilerBase extends \PhpAot\Core\Translator { @@ -200,14 +201,15 @@ class CompilerBase extends \PhpAot\Core\Translator protected bool $stubFile = false; protected bool $enableProfiler = false; protected Parser $parser; + protected PrettyPrinter $printer; public function __construct(string $rootPath) { $this->rootPath = $rootPath; - $this->parser = (new ParserFactory())->createForNewestSupportedVersion(); - // $this->prettyPrinter = new PrettyPrinter\Standard; + $this->parser = (new ParserFactory())->createForNewestSupportedVersion(); + $this->printer = new PrettyPrinter\Standard; $this->setBuildDir($rootPath . '/build'); - $climate = new CLImate(); + $climate = new CLImate(); $this->climate = $climate; // $this->noLiteralStrings = $climate->arguments->get('no-literal-strings'); } @@ -948,6 +950,9 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Stmt_Global': $result = $this->parseGlobal($v); break; + case 'Stmt_Enum': + $result = $this->parseEnum($v); + break; case 'Stmt_Static': $result = $this->parseStatic($v); break; @@ -2797,7 +2802,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $var_def . $code; } - protected function parseStatic(mixed $v): string + protected function parseStatic(Node\Stmt\Static_ $v): string { $list = []; foreach ($v->vars as $var) { @@ -2812,6 +2817,11 @@ class CompilerBase extends \PhpAot\Core\Translator return implode(PHP_EOL . $this->getIndent(), $list); } + protected function parseEnum(Node\Stmt\Enum_ $v): string + { + return 'php::eval("' . $this->escapeString($this->genEmbeddedCode($v)) . '");'; + } + protected function parseEval(mixed $expr): string { return 'php::eval(' . $this->parseIdentifier($expr->expr) . ')'; @@ -3109,11 +3119,13 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isNameExpr($expr->class) and $this->isIdExpr($expr->name)) { $nativeFunc = $this->getNativeStaticMethod($class, $method); - $args = $this->parseNativeCallArgs($expr->args, $nativeFunc); - if ($args) { - return self::PREFIX . $nativeFunc . '(php::null_object, ' . $args . ')'; - } else { - return self::PREFIX . $nativeFunc . '(php::null_object)'; + if ($nativeFunc) { + $args = $this->parseNativeCallArgs($expr->args, $nativeFunc); + if ($args) { + return self::PREFIX . $nativeFunc . '(php::null_object, ' . $args . ')'; + } else { + return self::PREFIX . $nativeFunc . '(php::null_object)'; + } } } $ce = $this->getClassEntryPtr($class); @@ -3553,6 +3565,11 @@ class CompilerBase extends \PhpAot\Core\Translator } } + protected function genEmbeddedCode(NodeAbstract $stmt): string + { + return $this->printer->prettyPrint([$stmt]); + } + protected function parseClosure(Node\Expr\Closure $expr): string { $tmpVar = $this->genTmpVarName(); diff --git a/tests/aot/enum2.phpt b/tests/aot/enum2.phpt new file mode 100644 index 00000000..ce382f0e --- /dev/null +++ b/tests/aot/enum2.phpt @@ -0,0 +1,26 @@ +--TEST-- +enum 2 +--FILE-- + +--EXPECT-- +array(4) { + [0]=> + enum(TestEnum2::Hearts) + [1]=> + enum(TestEnum2::Diamonds) + [2]=> + enum(TestEnum2::Clubs) + [3]=> + enum(TestEnum2::Spades) +}