From 29bd55ccd31493faa487d281dec4d121fab4c804 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 12 Jan 2026 17:30:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E7=B1=BB?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=96=87=E4=BB=B6=E6=98=A0=E5=B0=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CompilerBase 中新增 classesDefineInFile 属性用于存储文件中的类定义 - 初始化时清空 classesDefineInFile 数组 - 在 Translator 中为每个类定义生成注册函数 - 将当前解析的类同时添加到 classes 和 classesDefineInFile 数组中 - 优化类定义创建逻辑,移除重复检查代码 --- src/Php/CompilerBase.php | 7 ++++++- src/Php/Translator.php | 36 ++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 6ab3bcc1..af399649 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -86,9 +86,13 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $useFunctions = []; protected string $class = ''; /** - * @var array + * @var array */ protected array $classes = []; + /** + * @var array + */ + protected array $classesDefineInFile = []; protected FunctionDef $functionDef; protected ClassDef $classDef; protected array $globalVars = [ @@ -201,6 +205,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $this->indentLevel = 0; $this->strictTypes = false; + $this->classesDefineInFile = []; } protected function resetNamespace(): void diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 7308ea7d..726f50a0 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -145,6 +145,23 @@ class Translator extends Preprocessor abort($v); } } + + foreach ($this->classesDefineInFile as $classDef) { + $name = $classDef->getNamespacedName(); + + if ($classDef->extends) { + $parentName = 'zend_class_entry *parent_ce'; + $param = 'parent_ce'; + } else { + $parentName = ''; + $param = ''; + } + $cppCode .= 'zend_class_entry *' . self::PREFIX . 'register_class_' . $name . '(' . $parentName . ') {' . PHP_EOL; + $cppCode .= $this->getIndent() . 'return register_class_' . $name . '(' . $param . ');' . PHP_EOL; + $cppCode .= '}' . PHP_EOL . PHP_EOL; + } + $cppCode .= PHP_EOL; + // include + extern global vars + function impl return $this->genIncludeHeaderFiles() . $cppCode; } @@ -390,17 +407,16 @@ class Translator extends Preprocessor $this->stubFileIncluded = true; } - if (!isset($this->classes[$this->class])) { - $this->classDef = new ClassDef(); - $this->classDef->name = $this->class; - if ($class->extends) { - $this->classDef->extends = $this->parseIdentifier($class->extends); - } - $this->classDef->implements = $this->parseIdentifierList($class->implements); - $this->classDef->namespace = $this->namespace; - - $this->classes[$this->class] = $this->classDef; + $this->classDef = new ClassDef(); + $this->classDef->name = $this->class; + if ($class->extends) { + $this->classDef->extends = $this->parseIdentifier($class->extends); } + $this->classDef->implements = $this->parseIdentifierList($class->implements); + $this->classDef->namespace = $this->namespace; + + $this->classes[$this->class] = $this->classDef; + $this->classesDefineInFile[$this->class] = $this->classDef; $methodCodes = [];