diff --git a/app/common/class/phpparser/NodeVisitor.php b/app/common/class/phpparser/NodeVisitor.php index 1f4f17a..7675f76 100644 --- a/app/common/class/phpparser/NodeVisitor.php +++ b/app/common/class/phpparser/NodeVisitor.php @@ -4,18 +4,28 @@ namespace app\common\class\phpparser; use PhpParser\Comment; use PhpParser\Node; +use PhpParser\Node\Expr\New_; +use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\TraitUseAdaptation\Alias; use PhpParser\Node\Stmt\Use_; use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; +use think\helper\Str; class NodeVisitor extends NodeVisitorAbstract { protected $cmd; protected $name; + + + public $usedClass = []; + + public $callClass = []; + public function __construct($cmd, $name) { $this->cmd = $cmd; @@ -37,22 +47,49 @@ class NodeVisitor extends NodeVisitorAbstract $name .= md5($this->name); - $name .= md5(end($use_item->name->parts)); + $used_class_str = implode('\\', $use_item->name->parts); + + $name .= md5($used_class_str); + + $this->usedClass[$used_class_str] = $name; + if (!empty($use_item->alias->name)) { + $this->usedClass[$use_item->alias->name] = $name; + } $use_item->alias = new Identifier($name); } - }else if ($node instanceof Class_){ - - - if(!empty($node->extends)){ + } else if ($node instanceof Class_) { - $name = 'class'; + if (!empty($node->extends)) { - $name .= md5($this->name); - $name .= md5(end($node->extends->parts)); + $used_class_str = implode('\\', $node->extends->parts); + + foreach ($this->usedClass as $class_name => $class_name_md5) { + if (Str::endsWith($class_name, $used_class_str)) { + $node->extends = new Name($class_name_md5); + } + } + } + } else if ($node instanceof Expression) { + if ( + $node->expr instanceof StaticCall || + $node->expr instanceof New_ + ) { + $used_class_str = implode('\\', $node->expr->class->parts); + if ($used_class_str != 'static' && $used_class_str != 'self' && $used_class_str != 'parent') { + $is_replaced = false; + foreach ($this->usedClass as $class_name => $class_name_md5) { + if (Str::endsWith($class_name, $used_class_str)) { + $is_replaced = true; + $node->expr->class = new Name($class_name_md5); + } + } + if(!$is_replaced){ + dump($node); + } + } - $node->extends = new Name($name); } }