From 537222b1e745053286ad93acdda3a66d0517470f Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 30 Mar 2026 12:24:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84PHP=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=92=8C=E9=A2=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在resetNamespace方法中添加useAliases重置 - 将名称空间比较改为大小写不敏感匹配 - 在preprocessor中添加文件、函数、类和名称空间重置调用 - 将getNamespacedClassName替换为getFullClassName方法 - 添加目录检查防止路径错误 --- src/Php/CompilerBase.php | 7 ++++--- src/Php/Preprocessor.php | 6 +++++- src/Php/Translator.php | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 6a34b1c6..db71194b 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -599,8 +599,9 @@ class CompilerBase extends \PhpAot\Core\Translator protected function resetNamespace(): void { $this->useNamespaces = []; - $this->useFunctions = []; - $this->namespace = ''; + $this->useAliases = []; + $this->useFunctions = []; + $this->namespace = ''; } protected function getFunctionName(FunctionLike $v): string @@ -654,7 +655,7 @@ class CompilerBase extends \PhpAot\Core\Translator foreach ($this->useNamespaces as $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); goto _return; } diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index d3dfd0cd..6beffb06 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -85,6 +85,10 @@ class Preprocessor extends CompilerBase $phpCode = $this->loadFile($file); $this->symbolCallInFile[$this->file] = []; + $this->resetFile(); + $this->resetFunction(); + $this->resetClass(); + $this->resetNamespace(); $this->climate->info('prepare: ' . $this->file); try { @@ -194,7 +198,7 @@ class Preprocessor extends CompilerBase protected function prepareClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): string { $this->class = $this->parseIdentifier($class->name); - $fullClassName = $this->getNamespacedClassName($this->class); + $fullClassName = $this->getFullClassName(); $fullClassNameLower = strtolower($fullClassName); if (!empty($class->extends)) { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 62ba54ab..ac41a099 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -475,7 +475,7 @@ class Translator extends Preprocessor public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string { 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), '/'); break; }