refactor(php): 重构PHP编译器基础类和预处理器

- 在resetNamespace方法中添加useAliases重置
- 将名称空间比较改为大小写不敏感匹配
- 在preprocessor中添加文件、函数、类和名称空间重置调用
- 将getNamespacedClassName替换为getFullClassName方法
- 添加目录检查防止路径错误
pull/1/head
韩天峰 5 months ago
parent 37b44368eb
commit 537222b1e7
  1. 7
      src/Php/CompilerBase.php
  2. 6
      src/Php/Preprocessor.php
  3. 2
      src/Php/Translator.php

@ -599,8 +599,9 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function resetNamespace(): void protected function resetNamespace(): void
{ {
$this->useNamespaces = []; $this->useNamespaces = [];
$this->useFunctions = []; $this->useAliases = [];
$this->namespace = ''; $this->useFunctions = [];
$this->namespace = '';
} }
protected function getFunctionName(FunctionLike $v): string protected function getFunctionName(FunctionLike $v): string
@ -654,7 +655,7 @@ class CompilerBase extends \PhpAot\Core\Translator
foreach ($this->useNamespaces as $useNamespace) { foreach ($this->useNamespaces as $useNamespace) {
$ns1 = explode('\\', trim($useNamespace, '\\')); $ns1 = explode('\\', trim($useNamespace, '\\'));
if ($ns1[array_key_last($ns1)] === $ns2[0]) { if (strcasecmp($ns1[array_key_last($ns1)], $ns2[0]) === 0) {
$ns = '\\' . implode('\\', $ns1); $ns = '\\' . implode('\\', $ns1);
goto _return; goto _return;
} }

@ -85,6 +85,10 @@ class Preprocessor extends CompilerBase
$phpCode = $this->loadFile($file); $phpCode = $this->loadFile($file);
$this->symbolCallInFile[$this->file] = []; $this->symbolCallInFile[$this->file] = [];
$this->resetFile();
$this->resetFunction();
$this->resetClass();
$this->resetNamespace();
$this->climate->info('prepare: ' . $this->file); $this->climate->info('prepare: ' . $this->file);
try { try {
@ -194,7 +198,7 @@ class Preprocessor extends CompilerBase
protected function prepareClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): string protected function prepareClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): string
{ {
$this->class = $this->parseIdentifier($class->name); $this->class = $this->parseIdentifier($class->name);
$fullClassName = $this->getNamespacedClassName($this->class); $fullClassName = $this->getFullClassName();
$fullClassNameLower = strtolower($fullClassName); $fullClassNameLower = strtolower($fullClassName);
if (!empty($class->extends)) { if (!empty($class->extends)) {

@ -475,7 +475,7 @@ class Translator extends Preprocessor
public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string
{ {
foreach ($this->sourceDirs as $srcDir) { foreach ($this->sourceDirs as $srcDir) {
if (str_starts_with($stubFilenameWithoutExtension, $srcDir)) { if (!is_dir($srcDir) or str_starts_with($stubFilenameWithoutExtension, $srcDir)) {
$filePath = ltrim($this->removeCommonPrefix($srcDir, $stubFilenameWithoutExtension), '/'); $filePath = ltrim($this->removeCommonPrefix($srcDir, $stubFilenameWithoutExtension), '/');
break; break;
} }

Loading…
Cancel
Save