From ad3bd659b016cfbd45157ab8b3b29ab75877cb9c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 24 Mar 2026 18:47:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(template):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=B1=BB=E5=B8=B8=E9=87=8F=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将类常量生成代码从模板文件移动到 Translator 类中 - 创建 genUpdateClassConstants 方法统一处理数组类型常量生成 - 使用新方法替换原有的 PHP 模板循环结构 - 简化 extension.cc.php 模板中的常量定义代码 - 提高代码可维护性和复用性 --- src/Php/Translator.php | 21 +++++++++++++++++++++ src/template/extension.cc.php | 11 +---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Php/Translator.php b/src/Php/Translator.php index ff238b82..1ad3b58f 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.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); diff --git a/src/template/extension.cc.php b/src/template/extension.cc.php index 07e8c0d0..b1ffb468 100644 --- a/src/template/extension.cc.php +++ b/src/template/extension.cc.php @@ -178,16 +178,7 @@ endforeach; ?> // class array constants -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; -?> + genUpdateClassConstants();?> } void php_app_clean() {