diff --git a/src/database/src/Commands/Ast/ModelRewriteInheritanceVisitor.php b/src/database/src/Commands/Ast/ModelRewriteInheritanceVisitor.php index 787928a2c..855a54b77 100644 --- a/src/database/src/Commands/Ast/ModelRewriteInheritanceVisitor.php +++ b/src/database/src/Commands/Ast/ModelRewriteInheritanceVisitor.php @@ -30,10 +30,51 @@ class ModelRewriteInheritanceVisitor extends NodeVisitorAbstract */ protected $data; + /** + * @var null|string + */ + protected $parentClass; + + /** + * @var bool + */ + protected $shouldAddUseUse = true; + public function __construct(ModelOption $option, ModelData $data) { $this->option = $option; $this->data = $data; + + if (! empty($option->getUses())) { + preg_match_all('/\s*([a-z0-9\\\\]+)(as)?([a-z0-9]+)?;?\s*/is', $option->getUses(), $match); + if (isset($match[1][0])) { + $this->parentClass = $match[1][0]; + } + } + } + + public function afterTraverse(array $nodes) + { + if (empty($this->option->getUses())) { + return null; + } + + $use = new Node\Stmt\UseUse( + new Node\Name($this->parentClass), + $this->option->getInheritance() + ); + + foreach ($nodes as $namespace) { + if (! $namespace instanceof Node\Stmt\Namespace_) { + continue; + } + + if ($this->shouldAddUseUse) { + array_unshift($namespace->stmts, new Node\Stmt\Use_([$use])); + } + } + + return null; } public function leaveNode(Node $node) @@ -46,24 +87,14 @@ class ModelRewriteInheritanceVisitor extends NodeVisitorAbstract } return $node; case $node instanceof Node\Stmt\UseUse: - $modelParent = get_parent_class($this->data->getClass()); - - $class = end($node->name->parts); - $alias = is_object($node->alias) ? $node->alias->name : ''; - if ($class == $modelParent || $alias == $modelParent) { - preg_match_all('/\s*([a-z0-9\\\\]+)(as)?([a-z0-9]+)?;?\s*/is', $this->option->getUses(), $match); - if (! empty($match) && isset($match[1][0])) { - $newClass = $match[1][0]; - $newAlias = $match[1][2] ?? ''; - - $node->name->parts = explode('\\', $newClass); - $node->alias = null; - - if (! empty($newAlias)) { - $node->alias = new Identifier($newAlias); - $node->alias->setAttribute('startLine', $node->getAttribute('startLine')); - $node->alias->setAttribute('endLine', $node->getAttribute('endLine')); - } + $class = implode('\\', $node->name->parts); + $alias = is_object($node->alias) ? $node->alias->name : null; + if ($class == $this->parentClass) { + // The parent class is exists. + $this->shouldAddUseUse = false; + if (end($node->name->parts) !== $this->option->getInheritance() && $alias !== $this->option->getInheritance()) { + // Rewrite the alias, if the class is not equal with inheritance. + $node->alias = new Identifier($this->option->getInheritance()); } } return $node; diff --git a/src/database/src/Commands/Ast/ModelUpdateVisitor.php b/src/database/src/Commands/Ast/ModelUpdateVisitor.php index a2a9aff52..37ce0861e 100644 --- a/src/database/src/Commands/Ast/ModelUpdateVisitor.php +++ b/src/database/src/Commands/Ast/ModelUpdateVisitor.php @@ -47,12 +47,6 @@ class ModelUpdateVisitor extends NodeVisitorAbstract return $node; case $node instanceof Node\Stmt\Class_: - //更改模型继承的父类名; - $inheritance = $this->option->getInheritance(); - if (is_object($node->extends) && ! empty($inheritance)) { - $node->extends->parts = [$inheritance]; - } - $doc = '/**' . PHP_EOL; foreach ($this->columns as $column) { [$name, $type, $comment] = $this->getProperty($column);