diff --git a/.gitignore b/.gitignore index 5e9baa6c..ace7fe76 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,8 @@ /tmp /projects/wordpress /projects/workerman +/projects/thinkphp +/projects/laravel +/projects/tmp /.php-cs-fixer.cache *.o \ No newline at end of file diff --git a/bin/compiler.php b/bin/compiler.php index 2554a9d3..e9faa681 100755 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -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]); } } diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 4592cdd1..6a34b1c6 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -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 === '') { diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 0d22c178..d3dfd0cd 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -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); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index cdbb7ffb..62ba54ab 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -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 {