From 7519c3c52356f499fc81aefda752e9ee11bd7154 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 27 Apr 2026 12:43:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0PHP=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=85=BC=E5=AE=B9=E6=80=A7=E6=A3=80=E6=9F=A5=E5=92=8C?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加PHP版本检查确保8.2.0以上且低于8.6.0 - 使用指定PHP版本创建解析器而不是最新支持版本 - 新增getNamespacedFuncName方法处理函数名的命名空间补齐 - 为动态函数调用添加命名空间全限定名称转换 - 导入PhpVersion类用于版本处理 --- src/Php/CompilerBase.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 2ff0d9ca..cd9012e5 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -39,6 +39,7 @@ use PhpParser\NodeAbstract; use PhpParser\NodeFinder; use PhpParser\Parser; use PhpParser\ParserFactory; +use PhpParser\PhpVersion; use PhpParser\PrettyPrinter; class CompilerBase extends \PhpAot\Core\Translator @@ -267,8 +268,14 @@ class CompilerBase extends \PhpAot\Core\Translator public function __construct(string $rootPath) { + if (version_compare(PHP_VERSION, '8.2.0', '<')) { + $this->error('PHP 8.2.0 or later is required'); + } + if (version_compare(PHP_VERSION, '8.6.0', '>=')) { + $this->error('PHP 8.6.0 or later is not supported'); + } $this->rootPath = $rootPath; - $this->parser = (new ParserFactory())->createForNewestSupportedVersion(); + $this->parser = (new ParserFactory())->createForVersion(PhpVersion::fromString(PHP_VERSION)); $this->printer = new PrettyPrinter\Standard(); $this->setBuildDir($rootPath . '/build'); $climate = new CLImate(); @@ -687,6 +694,22 @@ class CompilerBase extends \PhpAot\Core\Translator return $class; } + /** + * 函数名称处理,补齐 namespace + * @param string $funcName + * @return string + */ + public function getNamespacedFuncName(string $funcName): string + { + if ($funcName[0] == '\\') { + return ltrim($funcName, '\\'); + } + if (isset($this->useFunctions[$funcName])) { + return $this->useFunctions[$funcName]; + } + return $funcName; + } + protected function getObjectPropVarName(string $object, string $prop): string { return self::OBJECT_PROP . $object . self::NAMESPACE_SEPARATOR . $prop; @@ -2470,6 +2493,8 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->genPlaceHolder($this->identifierToStr($expr->name)); } } + // 动态调用的函数,转换函数名为带有命名空间的全限定名称 + $name = $this->getNamespacedFuncName($name); $code = $this->parseFuncCallWithOptimizer($name, $expr); if ($code) { return $code;