mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 04:37:46 +08:00
Optimized code.
This commit is contained in:
parent
5b208b4847
commit
ed5e2e7f54
@ -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;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user