From 9c170cf8b12be0abc30afdc44f6538de1cca549e Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 26 Nov 2022 17:17:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8F=98=E9=87=8F=E5=90=8D?= =?UTF-8?q?=E6=B7=B7=E6=B7=86=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/command/build/Dist.php | 80 +++++++++++++++++++++++++------ config/dist.php | 40 +++++++--------- 2 files changed, 82 insertions(+), 38 deletions(-) diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index dc0af35..97243aa 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -174,11 +174,11 @@ class Dist extends Command $this->log('编译所有代码以全命名空间路径调用'); $this->packClassUseName(); + $this->log('编译混淆变量名'); + $this->packFakeVar(); return; - $this->log('编译混淆变量名'); - // 根据配置pack_var扫描编译 $this->log('编译标准类库'); @@ -329,14 +329,13 @@ class Dist extends Command $pretty_printer = new PrettyPrinterTools(); + $target_include = $this->getAllInclude(); + $target_exclude = $this->getAllExclude(); foreach ($list_file 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; @@ -397,20 +396,18 @@ class Dist extends Command protected function getAllInclude() { return array_merge( + Config::get('dist.include_path', []), Config::get('dist.pack_app.include_path', []), - Config::get('dist.pack_vars.include_path', []), Config::get('dist.pack_config.include_path', []), - Config::get('dist.pack_env.include_path', []), ); } protected function getAllExclude() { return array_merge( + Config::get('dist.exclude_path', []), Config::get('dist.pack_app.exclude_path', []), - Config::get('dist.pack_vars.exclude_path', []), Config::get('dist.pack_config.exclude_path', []), - Config::get('dist.pack_env.exclude_path', []), ); } @@ -423,12 +420,13 @@ class Dist extends Command $pretty_printer = new PrettyPrinterTools(); + $target_include = $this->getAllInclude(); + + $target_exclude = $this->getAllExclude(); + foreach ($list_file 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; @@ -590,13 +588,13 @@ class Dist extends Command $pretty_printer = new PrettyPrinterTools(); + $target_include = $this->getAllInclude(); + + $target_exclude = $this->getAllExclude(); 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; @@ -627,6 +625,58 @@ class Dist extends Command $this->tempFilesystem->delete('.env'); } } + public function packFakeVar() + { + + + $pack_vars_mode = Config::get('dist.pack_vars.pack_vars_mode'); + + if ($pack_vars_mode == 1) { + return; + } + + $list_files = $this->tempFilesystem->listContents('', true); + + $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + + $pretty_printer = new PrettyPrinterTools(); + + $target_include = $this->getAllInclude(); + + $target_exclude = $this->getAllExclude(); + + + if ($pack_vars_mode == 2) { + $target_exclude = array_merge($target_exclude, Config::get('dist.pack_vars.controller_path', [])); + } + + foreach ($list_files as $item_file) { + $path = $item_file['path']; + + + + 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); + + $fake_var_traverser = new NodeTraverser; + $fake_var_traverser->addVisitor( + new NodeFakeVarVisitorTools() + ); + + $file_stmts = $fake_var_traverser->traverse($file_stmts); + + // 生成代码 + $result_content = $pretty_printer->prettyPrintFile($file_stmts); + + $this->tempFilesystem->put($path, $result_content); + } + } diff --git a/config/dist.php b/config/dist.php index d59af6e..03a68e8 100644 --- a/config/dist.php +++ b/config/dist.php @@ -11,6 +11,15 @@ return [ '/^runtime/', ], + // 基础的加载规则 + 'include_path' => [ + '/^app/', + '/^extend/', + '/^database/', + '/^config/', + '/^route/', + ], + 'pack_app' => [ // 应当是标准的thinkphp类库文件,比如控制器、模型等 'include_path' => [ @@ -20,18 +29,6 @@ return [ // 应当是thinkphp的其他非类库文件,比如配置、中间件配置、自定义加载类、函数库、路由等 'exclude_path' => [], ], - 'pack_vars' => [ - // 实际上任何代码都可以,但是尽量只编译业务代码 - 'include_path' => [ - "/^app/", - "/^config/", - "/^database/", - "/^extend/", - "/^route/", - ], - // 基本不需要排除 - 'exclude_path' => [], - ], 'pack_config' => [ // 主要是config、middleware等直接return的文件,会自动判断是否return。自定义的return的php文件也会编译 'include_path' => [ @@ -51,17 +48,14 @@ return [ ], 'pack_env' => [ // 0:base64方式处理,1:明文打包,3:不要编译env配置 - 'pack_env_mode' => 0, - // 实际上任何代码都可以,但是尽量只编译业务代码 - 'include_path' => [ - "/^app/", - "/^config/", - "/^database/", - "/^extend/", - "/^route/", - ], - // 基本不需要排除 - 'exclude_path' => [], + 'pack_env_mode' => 0 + ], + 'pack_vars' => [ + // 0:全部编译,1:关闭编译,2:不编译controller的方法 + 'pack_vars_mode' => 0, + 'controller_path' => [ + '/\/controller\//', + ] ], // 全局函数库,否则无论是否以/开头,都以项目根目录开头定位,如果有其他的文件,在这里声明 // 不支持项目以外的位置定义