feat(php): 添加类定义文件映射功能

- 在 CompilerBase 中新增 classesDefineInFile 属性用于存储文件中的类定义
- 初始化时清空 classesDefineInFile 数组
- 在 Translator 中为每个类定义生成注册函数
- 将当前解析的类同时添加到 classes 和 classesDefineInFile 数组中
- 优化类定义创建逻辑,移除重复检查代码
pull/1/head
韩天峰 8 months ago
parent eaf2418313
commit 29bd55ccd3
  1. 7
      src/Php/CompilerBase.php
  2. 36
      src/Php/Translator.php

@ -86,9 +86,13 @@ class CompilerBase extends \PhpAot\Core\Translator
protected array $useFunctions = [];
protected string $class = '';
/**
* @var array<ClassDef>
* @var array<string, ClassDef>
*/
protected array $classes = [];
/**
* @var array<string, ClassDef>
*/
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

@ -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 = [];

Loading…
Cancel
Save