diff --git a/app/common.php b/app/common.php index af71042..cd7fc8d 100644 --- a/app/common.php +++ b/app/common.php @@ -74,7 +74,6 @@ if (!function_exists('xdebug')) { function xdebug($data, $type = 'xdebug', $suffix = null, $force = false, $file = null) { // 调试 - __DIR__; !is_dir(runtime_path() . 'xdebug/') && mkdir(runtime_path() . 'xdebug/'); if (is_null($file)) { $file = is_null($suffix) ? runtime_path() . 'xdebug/' . date('Ymd') . '.txt' : runtime_path() . 'xdebug/' . date('Ymd') . "_{$suffix}" . '.txt'; diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index 25a2b44..1d54a14 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace app\common\command\build; use app\common\tools\PathTools; +use app\common\tools\phpparser\MinifyPrinterTools; use app\common\tools\phpparser\NodeVisitorTools; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; @@ -158,9 +159,9 @@ class Dist extends Command PathTools::intiDir($lib_php_path); $this->buildIncludeIndexFile([ - $lib_function_file, $lib_dir_const_file, $lib_php_file, + $lib_function_file, ]); @@ -206,18 +207,13 @@ class Dist extends Command )); } - $prettyPrinter = new Standard(); + $prettyPrinter = new MinifyPrinterTools(); $dir_const_code = $prettyPrinter->prettyPrintFile($dir_const_stmts); file_put_contents($lib_dir_const_path, $dir_const_code); } - public function buildDirConstCode($stmt) - { - # code... - } - public function buildFunctionFile($lib_function_path) { $function_path = Config::get('dist.function_path', []); @@ -257,22 +253,22 @@ class Dist extends Command return new ConstFetch(new Name($const_key)); } - if($node instanceof Comment){ - return NodeTraverser::REMOVE_NODE; + if ($node->hasAttribute('comments')) { + $node->setAttribute('comments', []); } } }); $stmts = $traverser->traverse($stmts); - + foreach ($stmts as $stmt_item) { $function_stmts[] = $stmt_item; } } - $prettyPrinter = new Standard(); + $prettyPrinter = new MinifyPrinterTools(); $function_code = $prettyPrinter->prettyPrintFile($function_stmts); @@ -303,7 +299,7 @@ class Dist extends Command foreach ($files as $file_name) { $file_stmts[] = new Expression(new Include_(new Concat(new Dir, new String_($file_name)), Include_::TYPE_REQUIRE_ONCE)); } - $prettyPrinter = new Standard(); + $prettyPrinter = new MinifyPrinterTools(); $newCode = $prettyPrinter->prettyPrintFile($file_stmts); diff --git a/app/common/tools/phpparser/MinifyPrinterTools.php b/app/common/tools/phpparser/MinifyPrinterTools.php new file mode 100644 index 0000000..b0dd808 --- /dev/null +++ b/app/common/tools/phpparser/MinifyPrinterTools.php @@ -0,0 +1,47 @@ +indentLevel = 0; + $this->nl = ""; + $this->origTokens = null; + } + + /** + * Set indentation level + * + * @param int $level Level in number of spaces + */ + protected function setIndentLevel(int $level) + { + $this->indentLevel = $level; + $this->nl = "" . \str_repeat('', $level); + } + + /** + * Increase indentation level. + */ + protected function indent() + { + $this->indentLevel += 4; + $this->nl .= ''; + } + + /** + * Decrease indentation level. + */ + protected function outdent() + { + assert($this->indentLevel >= 4); + $this->indentLevel -= 4; + $this->nl = "" . str_repeat('', $this->indentLevel); + } +} diff --git a/config/dist.php b/config/dist.php index b7e1a08..dba908a 100644 --- a/config/dist.php +++ b/config/dist.php @@ -4,8 +4,9 @@ return [ // 全局函数库,否则无论是否以/开头,都以项目根目录开头定位,如果有其他的文件,在这里声明 // 不支持项目以外的位置定义 // 只能包含函数,不支持命名空间 - // 这些代码不能包含__DIR__、__FILE__等代码位置常量,不会影响打包,但代码逻辑会被破坏 - // 将忽略注释和注解,代码业务请不要依赖注解 + // 这些代码将忽略include、require表达式,所以如果用include 方式引入其他函数库,要在这里声明, + // !如果使用include方式读取文件内容或配置,将失败 + // !将忽略注释和注解,代码业务请不要依赖注解 'function_path' => [ 'app/common.php' ], @@ -14,6 +15,7 @@ return [ '/^\.git/', '/^dist/', '/^runtime/', + '/app\/common.php/', ], // 支持正则表达式,将文件原封不动的挪到输出目录 'ignore_path' => [ @@ -26,7 +28,6 @@ return [ '/provider\.php/', '/service\.php/', '/^app\/.*\/config\/.*/', - '/app\/common.php/', '/config.php/', '/^public\/index\.php/', '/^public\/router\.php/',