From f30a29a048713a5266b0c794bbf2780a1130e6c2 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 19:54:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=BC=96=E7=A0=81=E6=A3=80=E6=9F=A5=E5=92=8C=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=B0=83=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了UTF-8编码检查以确保文件正确读取 - 更新了composer.json中的swoole/phpx依赖版本 - 修复了Translator中类名获取的方法调用 - 移除了UniversalMethodCall中不需要的返回类型参数 --- composer.json | 2 +- src/Php/CompilerBase.php | 3 +++ src/Php/Translator.php | 2 +- src/Php/UniversalMethodCall.php | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index f9f100e2..8ef254f7 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "marcj/topsort": "^2.0", "symfony/var-dumper": "^8.0", "symfony/yaml": "^8.0", - "swoole/phpx": "~2.1.6" + "swoole/phpx": "~2.3.0" }, "require-dev": { "phpunit/phpunit": "^10.4", diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 40e72cb4..4d1819b5 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -5876,6 +5876,9 @@ class CompilerBase extends \PhpAot\Core\Translator if (!$phpCode) { throw new \Exception('Can not read file: ' . $file); } + if (!mb_check_encoding($phpCode, 'UTF-8')) { + throw new \Exception('File encoding must be UTF-8, got: ' . mb_detect_encoding($phpCode, ['UTF-8', 'ISO-8859-1', 'GBK', 'Shift_JIS'], true) . ' in ' . $file); + } $this->file = realpath($file); $this->dir = dirname($this->file); $this->stubFile = $this->isStubFile($file); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 7a0e1c86..d162f0b6 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1744,7 +1744,7 @@ CODE; $traitMethods = []; $traitConstants = []; $traitProperties = []; - $classDef = $this->getClass($className); + $classDef = $this->getClass($className->toString()); foreach ($stmt->stmts as $classStmt) { if ($classStmt instanceof Node\Stmt\ClassMethod) { diff --git a/src/Php/UniversalMethodCall.php b/src/Php/UniversalMethodCall.php index 770fa902..c27dfa0d 100644 --- a/src/Php/UniversalMethodCall.php +++ b/src/Php/UniversalMethodCall.php @@ -592,7 +592,7 @@ trait UniversalMethodCall 'calc_dec' => '(' . $receiver . ' - 1)', 'direct_method' => $this->genUniversalDirectMethod($receiver, $def['method'], $expr->args, $def['int_cast_args'] ?? []), 'direct_method_mutate' => $this->genUniversalMutatingMethod($receiver, $def['method'], $expr->args), - 'convert_fn' => $this->genUniversalConvertFn($receiver, $def['fn'], $def['return_type']), + 'convert_fn' => $this->genUniversalConvertFn($receiver, $def['fn']), 'php_fn' => $this->genUniversalPhpFn($receiver, $def['fn'], $expr->args, $def['receiver_pos'] ?? 0, $def['const_args'] ?? []), 'php_fn_ref' => $this->genUniversalPhpFnRef($receiver, $def['fn'], $expr->args, $def['return_type']), 'cpp_fn' => $this->genUniversalCppFn($receiver, $def['fn'], $expr->args, $def['receiver_pos'] ?? 0), @@ -688,7 +688,7 @@ trait UniversalMethodCall return '(' . $call . ', ' . $object . ')'; } - protected function genUniversalConvertFn(string $receiver, string $fn, string $type): string + protected function genUniversalConvertFn(string $receiver, string $fn): string { return 'php::' . $fn . '(' . $receiver . ')'; }