diff --git a/app/common/class/phpparser/NodeVisitor.php b/app/common/class/phpparser/NodeVisitor.php deleted file mode 100644 index 05e1bcc..0000000 --- a/app/common/class/phpparser/NodeVisitor.php +++ /dev/null @@ -1,134 +0,0 @@ -cmd = $cmd; - $this->name = $name; - } - - public function leaveNode(Node $node) - { - if ($node instanceof Use_) { - - // $this->cmd->addUsedClass($node); - - // return NodeTraverser::REMOVE_NODE; - - - foreach ($node->uses as &$use_item) { - - $name = 'class'; - - $name .= md5($this->name); - - $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)) { - - - $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 StaticCall || $node instanceof New_) { - - - if($node->class instanceof Variable){ - return; - } - - if($node->class instanceof Class_){ - return; - } - // exit; - - $used_class_str = implode('\\', $node->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->class = new Name($class_name_md5); - } - } - if (!$is_replaced) { - // dump($this->name); - // dump($used_class_str); - // dump($this->usedClass); - // dump($node); - } - } - }else if($node instanceof Param){ - - - if(is_null($node->type)){ - return; - } - - if($node->type instanceof Identifier){ - return; - } - - - $used_class_str = implode('\\', $node->type->parts); - - foreach ($this->usedClass as $class_name => $class_name_md5) { - if (Str::endsWith($class_name, $used_class_str)) { - $is_replaced = true; - $node->type = new Name($class_name_md5); - } - } - - } - - return null; - } -} diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index 9490b7b..0f1fdab 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -4,9 +4,9 @@ declare(strict_types=1); namespace app\common\command\build; -use app\common\class\phpparser\NodeVisitor; use app\common\exception\DirFindedException; use app\common\tools\PathTools; +use app\common\tools\phpparser\NodeVisitorTools; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use PhpParser\Node; @@ -23,6 +23,7 @@ use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use think\facade\App; +use think\facade\View; class Dist extends Command { @@ -104,8 +105,10 @@ class Dist extends Command + $lib_php_file = 'lib/index.' . uniqid() . '.php'; + $lib_php_path = $this->distPath . '/' . $lib_php_file; + - $lib_php_path = $this->distPath . '/lib/index.' . uniqid() . '.php'; PathTools::intiDir($lib_php_path); $prettyPrinter = new Standard(); @@ -115,6 +118,18 @@ class Dist extends Command file_put_contents($lib_php_path, $newCode); + $index_code = file_get_contents(__DIR__ . '/tpl/index.php.temp'); + + $index_code = str_replace('{$lib_php_file}', $lib_php_file, $index_code); + + $dist_filesystem->put('public/index.php', $index_code); + + $think_code = file_get_contents(__DIR__ . '/tpl/think.temp'); + + $think_code = str_replace('{$lib_php_file}', $lib_php_file, $think_code); + + $dist_filesystem->put('think', $think_code); + $output->info('打包完成'); } @@ -135,8 +150,6 @@ class Dist extends Command $stmts = $this->parseStmts($stmts, $name); - - $this->packList = array_merge($this->packList, $stmts); return null; @@ -156,7 +169,7 @@ class Dist extends Command $traverser = new NodeTraverser(); - $node_visitor = new NodeVisitor($this, $name); + $node_visitor = new NodeVisitorTools($this, $name); $traverser->addVisitor($node_visitor); diff --git a/app/common/command/build/tpl/index.php.temp b/app/common/command/build/tpl/index.php.temp new file mode 100644 index 0000000..08c1cc5 --- /dev/null +++ b/app/common/command/build/tpl/index.php.temp @@ -0,0 +1,21 @@ +http; + +$response = $http->run(); + +$response->send(); + +$http->end($response); diff --git a/app/common/command/build/tpl/think.temp b/app/common/command/build/tpl/think.temp new file mode 100644 index 0000000..ad7bd90 --- /dev/null +++ b/app/common/command/build/tpl/think.temp @@ -0,0 +1,17 @@ +#!/usr/bin/env php +console->run(); \ No newline at end of file diff --git a/app/common/tools/phpparser/NodeVisitorTools.php b/app/common/tools/phpparser/NodeVisitorTools.php new file mode 100644 index 0000000..a5071bc --- /dev/null +++ b/app/common/tools/phpparser/NodeVisitorTools.php @@ -0,0 +1,234 @@ +cmd = $cmd; + $this->name = $name; + } + + public function leaveNode(Node $node) + { + + if ($node instanceof Stmt) { + if (isset($node->attributes['comments'])) { + + $comments = $node->attributes['comments']; + $new_comments = []; + foreach ($comments as $comment_item) { + if ($comment_item instanceof Comment) { + } else { + $new_comments[] = $comment_item; + } + } + + $node->attributes['comments'] = $new_comments; + } + } + + if ($node instanceof Use_) { + + foreach ($node->uses as &$use_item) { + + $name = 'class'; + + $name .= md5($this->name); + + $used_class_str = implode('\\', $use_item->name->parts); + + + if (in_array($used_class_str, $this->skipClass)) { + return; + } + + $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)) { + + + $used_class_str = $node->extends->toString(); + $result_name = $this->findClassName($used_class_str); + if (!empty($result_name)) { + $node->extends = new Name($result_name); + } + } + + if (!empty($node->implements)) { + foreach ($node->implements as &$node_implements) { + $used_class_str = implode('\\', $node_implements->parts); + + foreach ($this->usedClass as $class_name => $class_name_md5) { + if (Str::endsWith($class_name, '\\' . $used_class_str)) { + $node_implements = new Name($class_name_md5); + } + } + } + } + } else if ( + $node instanceof StaticCall || + $node instanceof New_ || + $node instanceof ClassConstFetch || + $node instanceof Instanceof_ + ) { + + if ($node->class instanceof Variable) { + return; + } + + if ($node->class instanceof Class_) { + return; + } + + + $used_class_str = $node->class->toString(); + + if ($used_class_str != 'static' && $used_class_str != 'self' && $used_class_str != 'parent') { + $result_name = $this->findClassName($used_class_str); + + if (!empty($result_name)) { + $node->class = new Name($result_name); + } + } + } else if ($node instanceof Param) { + + + if (is_null($node->type)) { + return; + } + + if ($node->type instanceof Identifier) { + return; + } + + // dump($node); + $used_class_str = $node->type->toString(); + + $result_name = $this->findClassName($used_class_str); + + if (!empty($result_name)) { + + + $node->type = new Name($result_name); + } + } else if ($node instanceof Declare_) { + return NodeTraverser::REMOVE_NODE; + } else if ($node instanceof TraitUse) { + foreach ($node->traits as &$node_name) { + $used_class_str = $node_name->toString(); + + $result_name = $this->findClassName($used_class_str); + if (!empty($result_name)) { + $node_name = new Name($result_name); + } + } + } else if ($node instanceof Catch_) { + foreach ($node->types as &$node_name) { + $used_class_str = $node_name->toString(); + + $result_name = $this->findClassName($used_class_str); + + if (!empty($result_name)) { + $node_name = new Name($result_name); + } + } + } else if ($node instanceof ClassMethod) { + + if (empty($node->returnType)) { + return; + } + + if ($node->returnType instanceof Identifier) { + return; + } + + $used_class_str = $node->returnType->toString(); + $result_name = $this->findClassName($used_class_str); + + if (!empty($result_name)) { + + $node->returnType = new Name($result_name); + } + } + + return null; + } + + public function findClassName($class_name_str) + { + $name = null; + foreach ($this->usedClass as $class_name => $class_name_md5) { + + $class_name_arr = explode('\\', $class_name); + + $class_name_str_arr = explode('\\', $class_name_str); + + $class_name_str_arr = array_reverse($class_name_str_arr); + + $last_index = 0; + foreach ($class_name_str_arr as $class_item) { + $last_class_item = array_pop($class_name_arr); + if ($last_class_item != $class_item) { + break; + } + + $last_index++; + } + + if ($last_index == count($class_name_str_arr)) { + $name = $class_name_md5; + break; + } + } + return $name; + } +}