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
/projects/wordpress
/projects/workerman
/projects/thinkphp
/projects/laravel
/projects/tmp
/.php-cs-fixer.cache
*.o

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

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

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

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

Loading…
Cancel
Save