refactor(template): 重构类常量生成逻辑

- 将类常量生成代码从模板文件移动到 Translator 类中
- 创建 genUpdateClassConstants 方法统一处理数组类型常量生成
- 使用新方法替换原有的 PHP 模板循环结构
- 简化 extension.cc.php 模板中的常量定义代码
- 提高代码可维护性和复用性
pull/1/head
韩天峰 5 months ago
parent 68a9a782e8
commit ad3bd659b0
  1. 21
      src/Php/Translator.php
  2. 11
      src/template/extension.cc.php

@ -523,6 +523,27 @@ class Translator extends Preprocessor
return $scanner->scan();
}
protected function genUpdateClassConstants(): string
{
$code = '';
foreach ($this->classes as $classDef) :
foreach ($classDef->constants as $constant) :
if ($constant->type === self::TYPE_ARRAY) :
$constName = self::PREFIX . $this->getNativeName($constant->name, $classDef->namespace, $classDef->name);
$code .= $constName . " = " . $constant->value . ";\n";
if (false) {
$code .= "do {\n";
$code .= "auto c = (zend_class_constant *) zend_hash_str_find_ptr(&" . $this->getClassCe($classDef) . "->constants_table, ZEND_STRL(\"" . $constant->name . "\"));";
$code .= "c->type = (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY);\n";
$code .= "ZVAL_COPY(&c->value, $constName.ptr());\n";
$code .= " } while (0); \n";
}
endif;
endforeach;
endforeach;
return $code;
}
protected function parseProjectYaml(string $path): array
{
$cfg = Yaml::parseFile($path);

@ -178,16 +178,7 @@ endforeach;
?>
// class array constants
<?php
foreach ($this->classes as $classDef) :
foreach ($classDef->constants as $constant) :
if ($constant->type === Translator::TYPE_ARRAY) :
$constName = Translator::PREFIX . $this->getNativeName($constant->name, $classDef->namespace, $classDef->name);
echo $constName . " = " . $constant->value . ";\n";
endif;
endforeach;
endforeach;
?>
<?=$this->genUpdateClassConstants();?>
}
void php_app_clean() {

Loading…
Cancel
Save