From 4bf54c0ac1869325359b0a057e005c4eb64388ca Mon Sep 17 00:00:00 2001 From: augushong Date: Mon, 14 Nov 2022 18:02:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=89=93=E5=8C=85=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/class/phpparser/NodeVisitor.php | 55 ++++++++++++++++++---- 1 file changed, 46 insertions(+), 9 deletions(-) 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); } }