feat(php): 支持常量别名解析和命名空间处理

- 实现 useAliases 映射来处理常量别名
- 添加命名空间类名解析逻辑到 getNamespacedClassName 方法
- 修复 parseUse 方法的参数类型声明为 Node\Stmt\Use_
- 更新变量命名提高代码可读性
- 添加 use-const.phpt 测试用例验证常量别名功能
pull/1/head
韩天峰 5 months ago
parent 4a5fe8dd6c
commit 0cee9a4154
  1. 16
      src/Php/CompilerBase.php
  2. 28
      tests/aot/use-const.phpt

@ -3131,6 +3131,13 @@ class CompilerBase extends \PhpAot\Core\Translator
$fullName = $this->getNamespacedClassName($ns[0]);
$ce = $this->getClassEntryPtr($fullName);
return 'php::constant(' . $ce . ', ' . $this->getLiteralString($ns[1]) . ')';
} elseif (isset($this->useAliases[$name])) {
$name = $this->useAliases[$name];
} else {
$fullName = $this->getNamespacedClassName($name);
if ($fullName) {
$name = $fullName;
}
}
return 'php::constant(nullptr, ' . $this->getLiteralString($name) . ')';
}
@ -4374,16 +4381,15 @@ class CompilerBase extends \PhpAot\Core\Translator
}
}
protected function parseUse(mixed $v2): string
protected function parseUse(Node\Stmt\Use_ $v2): string
{
$code = '';
foreach ($v2->uses as $use) {
$id = $this->parseIdentifier($use->name);
if ($use->type === Node\Stmt\Use_::TYPE_FUNCTION) {
$rpos = strrpos($id, '\\');
$fn = substr($id, $rpos + 1);
$ns = substr($id, 0, $rpos);
// fn => namespace
$lastIndex = strrpos($id, '\\');
$fn = substr($id, $lastIndex + 1);
$ns = substr($id, 0, $lastIndex);
$this->useFunctions[$fn] = $ns;
} else {
if ($id === 'native_types') {

@ -0,0 +1,28 @@
--TEST--
aliasing imported constants to resolve naming conflicts
--FILE--
<?php
namespace foo {
const baz = 42;
}
namespace bar {
const baz = 43;
}
namespace {
use const foo\baz;
use const bar\baz as bar_baz;
function main() {
var_dump(baz);
var_dump(bar_baz);
echo "Done\n";
}
}
?>
--EXPECT--
int(42)
int(43)
Done
Loading…
Cancel
Save