feat(php): 添加复杂命名空间规则支持并优化函数名转义

- 实现复杂命名空间组合规则,支持 use foo\bar; bar\fn() 形式的调用
- 新增 escapeZendFnName 方法用于函数名转义处理
- 使用统一的函数名转义机制替换模板中的硬编码逻辑
- 移除函数定义中的静态关键字以提高灵活性
pull/1/head
韩天峰 7 months ago
parent 9118fc4e33
commit 0c02958589
  1. 17
      src/Php/CompilerBase.php
  2. 2
      src/Php/Translator.php
  3. 10
      src/template/extension.cc.php

@ -1804,6 +1804,18 @@ class CompilerBase extends \PhpAot\Core\Translator
if (isset($this->useFunctions[$fname])) {
$possibleFunctionNames[] = $this->escapeNamespace($this->useFunctions[$fname]) . self::NAMESPACE_SEPARATOR . $fname;
}
// 复杂命名空间规则,组合命名空间
// 例子:use foo\bar; bar\fn();
foreach ($this->useNamespaces as $use) {
$ns1 = explode('\\', $use);
$ns2 = explode('\\', $fname);
if ($ns1[array_key_last($ns1)] === $ns2[array_key_first($ns2)]) {
$ns = array_merge($ns1, $ns2);
array_splice($ns, array_key_last($ns1) + 1);
$possibleFunctionNames[] = $this->escapeNamespace(implode('\\', $ns));
break;
}
}
foreach ($possibleFunctionNames as $name) {
// 在预处理阶段检测到函数声明,但是未定义,说明在当前文件,但是顺序错误
// 跳过,稍后再处理
@ -2373,6 +2385,11 @@ class CompilerBase extends \PhpAot\Core\Translator
return str_replace('\\', self::NAMESPACE_SEPARATOR, strtolower($ns));
}
protected function escapeZendFnName(string $fn): string
{
return str_replace('\\', '_', strtolower($fn));
}
protected function escapeName(string $name): string
{
return strtolower($name);

@ -972,7 +972,7 @@ class Translator extends Preprocessor
private function genFunctionWrapper(FunctionDef $functionDef): string
{
$name = $functionDef->namespace ? $functionDef->namespace . '_' . $functionDef->name : $functionDef->name;
$name = $this->escapeZendFnName($functionDef->getNamespacedName());
$cppCode = 'ZEND_FUNCTION(' . $name . '){' . PHP_EOL;
$fn = self::PREFIX . $this->getNativeName($functionDef->name, $functionDef->namespace);
$cppCode .= $this->genWrapperFunctionArgs($fn, $functionDef);

@ -75,19 +75,19 @@ foreach ($this->functions as $functionDef):
if ($this->buildMode === 'ext' and $functionDef->name === 'main') {
continue;
}
$zif_name = $this->escapeZendFnName($functionDef->getNamespacedName());
?>
<?php if ($functionDef->namespace): ?>
<?php $zif_name = $functionDef->namespace . '_' . $functionDef->name; ?>
ZEND_NAMED_FE("<?=$this->escapeString($functionDef->getNamespacedName())?>", ZEND_FN(<?= $zif_name ?>), arginfo_<?=$zif_name?>)
<?php else: ?>
ZEND_FE(<?=$functionDef->name?>, arginfo_<?=$functionDef->name?>)
ZEND_FE(<?=$zif_name?>, arginfo_<?=$zif_name?>)
<?php endif; ?>
<?php endforeach;?>
ZEND_FE_END
};
// clang-format on
static PHP_MINIT_FUNCTION(<?=$this->targetName?>) {
PHP_MINIT_FUNCTION(<?=$this->targetName?>) {
// class/interface class entries
<?php
foreach ($this->classCeList as $ce):
@ -151,7 +151,7 @@ foreach ($this->nativeConstants as $name => $constant):
<?php endforeach; ?>
}
static PHP_RINIT_FUNCTION(<?=$this->targetName?>) {
PHP_RINIT_FUNCTION(<?=$this->targetName?>) {
php::request_init();
php_app_init();
@ -166,7 +166,7 @@ static PHP_RINIT_FUNCTION(<?=$this->targetName?>) {
return SUCCESS;
}
static PHP_RSHUTDOWN_FUNCTION(<?=$this->targetName?>) {
PHP_RSHUTDOWN_FUNCTION(<?=$this->targetName?>) {
php_app_clean();
php::request_shutdown();
return SUCCESS;

Loading…
Cancel
Save