feat(php): 添加PHP版本兼容性检查和函数命名空间处理

- 添加PHP版本检查确保8.2.0以上且低于8.6.0
- 使用指定PHP版本创建解析器而不是最新支持版本
- 新增getNamespacedFuncName方法处理函数名的命名空间补齐
- 为动态函数调用添加命名空间全限定名称转换
- 导入PhpVersion类用于版本处理
pull/1/head
韩天峰 4 months ago
parent 608db0e8c2
commit 7519c3c523
  1. 27
      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;

Loading…
Cancel
Save