feat(compiler): 添加对ThinkPHP和Laravel项目的编译支持

- 在.gitignore中添加thinkphp、laravel和tmp项目目录的忽略规则
- 修改错误处理逻辑,使用统一的output方法替代直接echo输出
- 新增getFullClassName方法用于获取完整类名
- 添加Unsupported异常类型并改进语法检查
- 实现自继承类的检测和错误报告功能
- 优化类名解析逻辑,提高编译器兼容性
pull/1/head
韩天峰 5 months ago
parent a5f397efa5
commit 37b44368eb
  1. 3
      .gitignore
  2. 4
      bin/compiler.php
  3. 5
      src/Php/CompilerBase.php
  4. 9
      src/Php/Preprocessor.php
  5. 7
      src/Php/Translator.php

3
.gitignore vendored

@ -8,5 +8,8 @@
/tmp /tmp
/projects/wordpress /projects/wordpress
/projects/workerman /projects/workerman
/projects/thinkphp
/projects/laravel
/projects/tmp
/.php-cs-fixer.cache /.php-cs-fixer.cache
*.o *.o

@ -25,10 +25,10 @@ foreach ($list as $k => $file) {
try { try {
$translator->prepare($file); $translator->prepare($file);
} catch (Unsupported $e) { } catch (Unsupported $e) {
echo " unsupported syntax: " . $e->getMessage() . "\n"; $translator->output(" unsupported syntax: " . $e->getMessage() . "\n" . " skip: " . $file . "\n", 'error');
echo " skip: " . $file . "\n";
unset($list[$k]); unset($list[$k]);
} catch (SyntaxError $e) { } catch (SyntaxError $e) {
$translator->output(" syntax error: " . $e->getMessage() . "\n" . " skip: " . $file . "\n", 'error');
unset($list[$k]); unset($list[$k]);
} }
} }

@ -627,6 +627,11 @@ class CompilerBase extends \PhpAot\Core\Translator
return $this->escapeName($prefix . $name); return $this->escapeName($prefix . $name);
} }
protected function getFullClassName(): string
{
return ltrim($this->namespace . '\\' . $this->class, '\\');
}
protected function getNamespacedClassName(string $class): string protected function getNamespacedClassName(string $class): string
{ {
if ($class === '') { if ($class === '') {

@ -10,6 +10,7 @@ namespace PhpAot\Php;
use MJS\TopSort\Implementations\StringSort; use MJS\TopSort\Implementations\StringSort;
use PhpAot\Php\Exception\SyntaxError; use PhpAot\Php\Exception\SyntaxError;
use PhpAot\Php\Exception\Unsupported;
use PhpParser\Modifiers; use PhpParser\Modifiers;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\NodeAbstract; use PhpParser\NodeAbstract;
@ -165,7 +166,7 @@ class Preprocessor extends CompilerBase
$this->foundStrayCode($v2); $this->foundStrayCode($v2);
// no break // no break
default: default:
abort($v2); throw new Unsupported('Unsupported statement: ' . $type2);
} }
} }
$this->resetNamespace(); $this->resetNamespace();
@ -199,6 +200,9 @@ class Preprocessor extends CompilerBase
if (!empty($class->extends)) { if (!empty($class->extends)) {
$this->parentClass = $this->getParentClass($class->extends); $this->parentClass = $this->getParentClass($class->extends);
$parentClassLower = strtolower($this->parentClass); $parentClassLower = strtolower($this->parentClass);
if ($parentClassLower === $fullClassNameLower) {
$this->fatalError($class, "Class {$fullClassName} cannot extend itself");
}
$this->classExtends[$fullClassNameLower] = $parentClassLower; $this->classExtends[$fullClassNameLower] = $parentClassLower;
if (!$this->isInternalClass($parentClassLower)) { if (!$this->isInternalClass($parentClassLower)) {
$this->symbolCallInFile[$this->file][] = $parentClassLower; $this->symbolCallInFile[$this->file][] = $parentClassLower;
@ -239,11 +243,12 @@ class Preprocessor extends CompilerBase
$this->prepareFunction($v) . PHP_EOL; $this->prepareFunction($v) . PHP_EOL;
} }
$fullClassName = $this->getNamespacedClassName($this->class); $fullClassName = $this->getFullClassName();
$fullMethodName = $fullClassName . '::' . $v->name; $fullMethodName = $fullClassName . '::' . $v->name;
$this->classMethodOverride[strtolower($fullMethodName)] = false; $this->classMethodOverride[strtolower($fullMethodName)] = false;
// 查找父类是否有同名方法,递归查找 // 查找父类是否有同名方法,递归查找
$fullClassNameLower = strtolower($fullClassName); $fullClassNameLower = strtolower($fullClassName);
while (isset($this->classExtends[$fullClassNameLower])) { while (isset($this->classExtends[$fullClassNameLower])) {
$parentClass = $this->classExtends[$fullClassNameLower]; $parentClass = $this->classExtends[$fullClassNameLower];
$parentMethodLower = strtolower($parentClass . '::' . $v->name); $parentMethodLower = strtolower($parentClass . '::' . $v->name);

@ -404,6 +404,11 @@ class Translator extends Preprocessor
return $objectFiles; return $objectFiles;
} }
public function output(string $message, string $style = 'out'): void
{
$this->climate->{$style}($message);
}
public function build(array $objectFiles): void public function build(array $objectFiles): void
{ {
$objectList = implode(' ', $objectFiles); $objectList = implode(' ', $objectFiles);
@ -865,7 +870,7 @@ class Translator extends Preprocessor
$extends = $class->extends; $extends = $class->extends;
} }
$fullName = ltrim($this->namespace . '\\' . $this->class, '\\'); $fullName = $this->getFullClassName();
if ($this->hasNativeClass($fullName)) { if ($this->hasNativeClass($fullName)) {
$this->classDef = $this->getClassDef($fullName); $this->classDef = $this->getClassDef($fullName);
} else { } else {

Loading…
Cancel
Save