diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index bc0f891..2b0aa57 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -31,7 +31,10 @@ use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use PhpParser\ParserFactory; use app\common\tools\phpparser\PrettyPrinterTools as Standard; +use app\common\tools\phpparser\PrettyPrinterTools; +use app\common\tools\phpparser\ReadEnvVisitorNodeTools; use PhpParser\Comment; +use PhpParser\NodeVisitor\NameResolver; use think\console\Command; use think\console\Input; use think\console\input\Argument; @@ -108,6 +111,8 @@ class Dist extends Command } } + $this->packEnv(); + $list_content = $app_filesystem->listContents('', true); foreach ($list_content as $file_info) { if ($file_info['type'] == 'dir') { @@ -167,6 +172,7 @@ class Dist extends Command $lib_function_file, ]); + $this->buildAllAppDir(); @@ -174,6 +180,71 @@ class Dist extends Command $output->info('打包完成'); } + + public function packEnv() + { + + + $list_files = $this->appFilesystem->listContents('', true); + + $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + + $prettyPrinter = new PrettyPrinterTools(); + + + foreach ($list_files as $item_file) { + $path = $item_file['path']; + + if ($item_file['type'] == 'dir') { + continue; + } + + if (!isset($item_file['extension'])) { + continue; + } + if ($item_file['extension'] != 'php') { + continue; + } + + if (!$this->isPackEnv($path)) { + continue; + } + $file_content = $this->appFilesystem->read($path); + + $file_stmts = $parser->parse($file_content); + + $nameResolver = new NameResolver(); + $nodeTraverser = new NodeTraverser; + $nodeTraverser->addVisitor($nameResolver); + + // Resolve names + $file_stmts = $nodeTraverser->traverse($file_stmts); + + $env_pack_visitor = new ReadEnvVisitorNodeTools; + $env_traverser = new NodeTraverser; + + $env_traverser->addVisitor($env_pack_visitor); + $file_stmts = $env_traverser->traverse($file_stmts); + + $result_content = $prettyPrinter->prettyPrintFile($file_stmts); + + $this->appFilesystem->put($path, $result_content); + } + } + + public function isPackEnv($path) + { + $pack_env_path = Config::get('dist.pack_env_path'); + foreach ($pack_env_path as $rule) { + + if (preg_match($rule, $path)) { + return true; + } + } + + return false; + } + /** * 打包路由文件 * @@ -229,11 +300,12 @@ class Dist extends Command $traverser_var_faker = new NodeTraverser(); $traverser_var_faker->addVisitor(new NodeFakeVarVisitorTools); - + $file_stmts = $traverser_var_faker->traverse($file_stmts); - $function_code = $prettyPrinter->prettyPrintFile($file_stmts); + $function_code = $prettyPrinter->prettyPrintFile($file_stmts); + $this->distFilesystem->put($item_file['path'], $function_code); } } @@ -295,7 +367,7 @@ class Dist extends Command $traverser_var_faker = new NodeTraverser(); $traverser_var_faker->addVisitor(new NodeFakeVarVisitorTools); - + $file_stmts = $traverser_var_faker->traverse($file_stmts); diff --git a/app/common/tools/phpparser/NodeVisitorTools.php b/app/common/tools/phpparser/NodeVisitorTools.php index 699387b..de0105f 100644 --- a/app/common/tools/phpparser/NodeVisitorTools.php +++ b/app/common/tools/phpparser/NodeVisitorTools.php @@ -60,7 +60,7 @@ class NodeVisitorTools extends NodeVisitorAbstract foreach ($comments as $comment_item) { if ($comment_item instanceof Doc) { $new_comments[] = $comment_item; - } + } } $node->setAttribute('comments', $new_comments); @@ -86,9 +86,12 @@ class NodeVisitorTools extends NodeVisitorAbstract $this->usedClass[$used_class_str] = $name; if (!empty($use_item->alias->name)) { + + $name .= md5($use_item->alias->name); + $this->usedClass[$use_item->alias->name] = $name; } - + $use_item->alias = new Identifier($name); } } else if ($node instanceof Class_) { diff --git a/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php b/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php index 6057bdb..be783fa 100644 --- a/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php +++ b/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php @@ -2,9 +2,71 @@ namespace app\common\tools\phpparser; +use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean; +use PhpParser\Node; +use PhpParser\Node\Expr\Array_; +use PhpParser\Node\Expr\Cast\Bool_; +use PhpParser\Node\Expr\ConstFetch; +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Name; +use PhpParser\Node\Name\FullyQualified; +use PhpParser\Node\Scalar\DNumber; +use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\String_; use PhpParser\NodeVisitorAbstract; - -class ReadEnvVisitorNodeTools extends NodeVisitorAbstract +class ReadEnvVisitorNodeTools extends \PhpParser\NodeVisitorAbstract { - -} + public function leaveNode(\PhpParser\Node $node) + { + if ($node instanceof \PhpParser\Node\Expr\FuncCall) { + if ($node->name instanceof \PhpParser\Node\Name\FullyQualified) { + $function_name = $node->name->toString(); + if ($function_name == 'env') { + dump($node); + $env_arg_1 = $this->getArg($node, 0); + $env_arg_2 = $this->getArg($node, 1); + $env_value = env($env_arg_1, $env_arg_2); + return $this->returnEnvValue($env_value); + } + } + } + } + public function getArg($node, $index) + { + $env = null; + if (isset($node->args[$index])) { + if ($node->args[$index]->value instanceof \PhpParser\Node\Scalar\String_) { + $env = $node->args[$index]->value->value; + } else { + // 发现非字符串方式读取,应当提示 + } + } + return $env; + } + public function returnEnvValue($value) + { + if (is_array($value)) { + return new \PhpParser\Node\Expr\Array_($value); + } else { + if (is_string($value)) { + return new \PhpParser\Node\Scalar\String_($value); + } else { + if (is_integer($value)) { + return new \PhpParser\Node\Scalar\LNumber($value); + } else { + if (is_float($value)) { + return new \PhpParser\Node\Scalar\DNumber($value); + } else { + if (is_bool($value)) { + if ($value) { + return new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('true')); + } else { + return new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('false')); + } + } + } + } + } + } + } +} \ No newline at end of file