diff --git a/.gitignore b/.gitignore index 90fa80c..2fb05c2 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ runtime ul.db /app/tools/controller/Install.php /app/common/command/curd/migrate_output.php -/dist \ No newline at end of file +/dist +/temp \ No newline at end of file diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index 2b0aa57..524b8df 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -68,6 +68,11 @@ class Dist extends Command */ protected $distFilesystem; + /** + * @var FileSystem + */ + protected $tempFilesystem; + protected function configure() { // 指令配置 @@ -83,24 +88,32 @@ class Dist extends Command $app_path = App::getRootPath(); $dist_path = $app_path . 'dist'; - PathTools::intiDir($dist_path . '.temp'); + $temp_path = $app_path . 'temp'; + PathTools::intiDir($temp_path . '.temp'); + + $this->distPath = $dist_path; + $app_adapter = new Local($app_path); - $app_filesystem = new Filesystem($app_adapter); - $this->appFilesystem = $app_filesystem; + + $dist_adapter = new Local($dist_path); - $dist_filesystem = new Filesystem($dist_adapter); - $this->distFilesystem = $dist_filesystem; + + $temp_adapter = new Local($temp_path); + $temp_filesystem = new Filesystem($temp_adapter); + $this->tempFilesystem = $temp_filesystem; + + $list_dist = $dist_filesystem->listContents(); foreach ($list_dist as $file_info) { @@ -113,7 +126,9 @@ class Dist extends Command $this->packEnv(); - $list_content = $app_filesystem->listContents('', true); + return ; + + $list_content = $temp_filesystem->listContents('', true); foreach ($list_content as $file_info) { if ($file_info['type'] == 'dir') { continue; @@ -125,7 +140,7 @@ class Dist extends Command continue; } - $file_content = $app_filesystem->read($file_path); + $file_content = $temp_filesystem->read($file_path); $path_info = pathinfo($file_path); if (!$this->isIgnored($file_path)) { @@ -172,26 +187,35 @@ class Dist extends Command $lib_function_file, ]); - + $this->buildAllAppDir(); - + $this->clearTempDir(); $output->info('打包完成'); } + public function clearTempDir() + { + $list_dist = $this->tempFilesystem->listContents(); + + foreach ($list_dist as $file_info) { + if ($file_info['type'] == 'file') { + $this->tempFilesystem->delete($file_info['path']); + } else { + $this->tempFilesystem->deleteDir($file_info['path']); + } + } + } 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']; @@ -199,17 +223,21 @@ class Dist extends Command continue; } - if (!isset($item_file['extension'])) { - continue; - } - if ($item_file['extension'] != 'php') { - continue; + $skip_path = Config::get('dist.skip_path', []); + + foreach ($skip_path as $rule) { + if (preg_match($rule, $path)) { + continue 2; + } } + - if (!$this->isPackEnv($path)) { + $file_content = $this->appFilesystem->read($path); + + if (!$this->isPackEnv($item_file)) { + $this->tempFilesystem->put($path, $file_content); continue; } - $file_content = $this->appFilesystem->read($path); $file_stmts = $parser->parse($file_content); @@ -220,7 +248,7 @@ class Dist extends Command // Resolve names $file_stmts = $nodeTraverser->traverse($file_stmts); - $env_pack_visitor = new ReadEnvVisitorNodeTools; + $env_pack_visitor = new ReadEnvVisitorNodeTools($path); $env_traverser = new NodeTraverser; $env_traverser->addVisitor($env_pack_visitor); @@ -228,16 +256,27 @@ class Dist extends Command $result_content = $prettyPrinter->prettyPrintFile($file_stmts); - $this->appFilesystem->put($path, $result_content); + $this->tempFilesystem->put($path, $result_content); + } + + if($this->tempFilesystem->has('.env')){ + $this->tempFilesystem->delete('.env'); } } - public function isPackEnv($path) + public function isPackEnv($item_file) { - $pack_env_path = Config::get('dist.pack_env_path'); + if (!isset($item_file['extension'])) { + return false; + } + if ($item_file['extension'] != 'php') { + return false; + } + + $pack_env_path = Config::get('dist.pack_env_path', []); foreach ($pack_env_path as $rule) { - if (preg_match($rule, $path)) { + if (preg_match($rule, $item_file['path'])) { return true; } } @@ -254,7 +293,7 @@ class Dist extends Command { $route_dir = 'route'; - $list_files = $this->appFilesystem->listContents($route_dir, true); + $list_files = $this->tempFilesystem->listContents($route_dir, true); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); @@ -265,7 +304,7 @@ class Dist extends Command continue; } - $file_content = $this->appFilesystem->read($item_file['path']); + $file_content = $this->tempFilesystem->read($item_file['path']); $file_stmts = $parser->parse($file_content); @@ -305,7 +344,7 @@ class Dist extends Command $function_code = $prettyPrinter->prettyPrintFile($file_stmts); - + $this->distFilesystem->put($item_file['path'], $function_code); } } @@ -319,7 +358,7 @@ class Dist extends Command { $database_dir = 'database'; - $list_files = $this->appFilesystem->listContents($database_dir, true); + $list_files = $this->tempFilesystem->listContents($database_dir, true); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); @@ -331,7 +370,7 @@ class Dist extends Command continue; } - $file_content = $this->appFilesystem->read($item_file['path']); + $file_content = $this->tempFilesystem->read($item_file['path']); $file_stmts = $parser->parse($file_content); diff --git a/app/common/tools/phpparser/NodeVisitorTools.php b/app/common/tools/phpparser/NodeVisitorTools.php index de0105f..87c561f 100644 --- a/app/common/tools/phpparser/NodeVisitorTools.php +++ b/app/common/tools/phpparser/NodeVisitorTools.php @@ -82,15 +82,14 @@ class NodeVisitorTools extends NodeVisitorAbstract return; } + if(isset($this->usedClass[$used_class_str])){ + return NodeTraverser::REMOVE_NODE; + } + $name .= md5($used_class_str); $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); } diff --git a/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php b/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php index be783fa..9f721ef 100644 --- a/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php +++ b/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php @@ -8,65 +8,106 @@ use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\Cast\Bool_; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Identifier; 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\NodeTraverser; use PhpParser\NodeVisitorAbstract; -class ReadEnvVisitorNodeTools extends \PhpParser\NodeVisitorAbstract + +class ReadEnvVisitorNodeTools extends NodeVisitorAbstract { - public function leaveNode(\PhpParser\Node $node) + protected $path; + + protected $skipFiles = [ + 'app/common/tools/phpparser/ReadEnvVisitorNodeTools.php' + ]; + + public function __construct($path) { - if ($node instanceof \PhpParser\Node\Expr\FuncCall) { - if ($node->name instanceof \PhpParser\Node\Name\FullyQualified) { + $this->path = $path; + } + + public function enterNode(Node $node) + { + if (in_array($this->path, $this->skipFiles)) { + return NodeTraverser::DONT_TRAVERSE_CHILDREN; + } + } + + public function leaveNode(Node $node) + { + + + if ($node instanceof FuncCall) { + if ($node->name instanceof Name) { $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); + + $return_node = $this->returnEnvValue($env_value); + return $return_node; } } - } - } - 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')); - } + } else if ($node instanceof StaticCall) { + + if ($node->class instanceof FullyQualified) { + $class_name = $node->class->toString(); + if ($class_name == 'think\\facade\\Env') { + if ($node->name instanceof Identifier) { + if ($node->name == 'get') { + $env_arg_1 = $this->getArg($node, 0); + $env_arg_2 = $this->getArg($node, 1); + + $env_value = env($env_arg_1, $env_arg_2); + + $return_node = $this->returnEnvValue($env_value); + return $return_node; } } } } } } -} \ No newline at end of file + + public function getArg($node, $index) + { + $env = null; + if (isset($node->args[$index])) { + if ($node->args[$index]->value instanceof String_) { + $env = $node->args[$index]->value->value; + } else { + // 发现非字符串方式读取,应当提示 + } + } + + return $env; + } + + public function returnEnvValue($value) + { + if (is_array($value)) { + return new Array_($value); + } else if (is_string($value)) { + return new String_($value); + } else if (is_integer($value)) { + return new LNumber($value); + } else if (is_float($value)) { + return new DNumber($value); + } else if (is_bool($value)) { + if ($value) { + return new ConstFetch(new Name('true')); + } else { + return new ConstFetch(new Name('false')); + } + } + } +} diff --git a/config/dist.php b/config/dist.php index 45d16b1..8d9b457 100644 --- a/config/dist.php +++ b/config/dist.php @@ -21,7 +21,6 @@ return [ '/^vendor/', '/^config/', '/^lib\//', - '/event\.php/', '/middleware\.php/', '/provider\.php/', @@ -32,5 +31,14 @@ return [ '/^public\/router\.php/', '/^app\/admin\/service\/initAdminData\/*/', + ], + // 希望将env打包的文件 + 'pack_env_path'=>[ + '/^app/', + '/^config/', + '/^database/', + '/^extend/', + '/^lib/', + '/^route/', ] ];