diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index 0887add..dc0af35 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -167,13 +167,13 @@ class Dist extends Command $this->log('删除注释并生成版权声明'); $this->clearCommentAndGenerateCopyright(); - $this->log('编译env配置信息'); - // 根据配置pack_env扫描编译 - // $this->packEnv(); + $this->packEnv(); + $this->log('编译所有代码以全命名空间路径调用'); $this->packClassUseName(); + return; @@ -378,7 +378,7 @@ class Dist extends Command $copyright = Config::get('dist.copyright', []); foreach ($copyright as $text) { - $copyright_stmts[] = new Comment('// '.$text); + $copyright_stmts[] = new Comment('// ' . $text); } $copyright_stmts_item = new Nop(); @@ -416,9 +416,6 @@ class Dist extends Command public function packClassUseName() { - // 删除无效的use - // 将所有的use类名混淆 - $list_file = $this->tempFilesystem->listContents('', true); @@ -580,16 +577,48 @@ class Dist extends Command public function packEnv() { + + $env_pack_mode = Config::get('dist.pack_env.pack_env_mode'); + + if ($env_pack_mode == 3) { + $this->debug('跳过ENV配置打包'); + return; + } $list_files = $this->tempFilesystem->listContents('', true); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); - $prettyPrinter = new PrettyPrinterTools(); + $pretty_printer = new PrettyPrinterTools(); foreach ($list_files as $item_file) { $path = $item_file['path']; + $target_include = $this->getAllInclude(); + + $target_exclude = $this->getAllExclude(); + + if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) { + continue; + } + + $this->debug('编译: ' . $path); + + $file_content = $this->tempFilesystem->read($path); + $file_stmts = $parser->parse($file_content); + + $env_traverser = new NodeTraverser; + $env_traverser->addVisitor( + new NameResolver() + ); + $env_traverser->addVisitor( + new ReadEnvVisitorNodeTools($path) + ); + + $file_stmts = $env_traverser->traverse($file_stmts); + + // 生成代码 + $result_content = $pretty_printer->prettyPrintFile($file_stmts); $this->tempFilesystem->put($path, $result_content); } diff --git a/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php b/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php index 09d2fc1..5c45f7a 100644 --- a/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php +++ b/app/common/tools/phpparser/ReadEnvVisitorNodeTools.php @@ -60,7 +60,6 @@ class ReadEnvVisitorNodeTools extends NodeVisitorAbstract } } } else if ($node instanceof StaticCall) { - if ($node->class instanceof FullyQualified) { $class_name = $node->class->toString(); if ($class_name == 'think\\facade\\Env') { @@ -101,7 +100,7 @@ class ReadEnvVisitorNodeTools extends NodeVisitorAbstract } else if (is_string($value)) { - $env_pack_mode = Config::get('dist.pack_env_mode'); + $env_pack_mode = Config::get('dist.pack_env.pack_env_mode'); if ($env_pack_mode == 0) { return new FuncCall(new Name('base64_decode'), [ diff --git a/config/dist.php b/config/dist.php index 2fde76e..d59af6e 100644 --- a/config/dist.php +++ b/config/dist.php @@ -51,7 +51,7 @@ return [ ], 'pack_env' => [ // 0:base64方式处理,1:明文打包,3:不要编译env配置 - 'pack_env_mode' => 1, + 'pack_env_mode' => 0, // 实际上任何代码都可以,但是尽量只编译业务代码 'include_path' => [ "/^app/",