完成路由文件的打包压缩

This commit is contained in:
2022-11-19 17:03:51 +08:00
parent 78ead61684
commit 4e17c7125f
2 changed files with 71 additions and 3 deletions

View File

@@ -150,6 +150,7 @@ class Dist extends Command
$this->buildFunctionFile($lib_function_path);
$this->buildMigrateFile();
$this->buildRouteFile();
$lib_dir_const_file = '/lib.' . uniqid() . '.php';
$lib_dir_const_path = $this->distPath . '/lib' . $lib_dir_const_file;
@@ -172,6 +173,69 @@ class Dist extends Command
$output->info('打包完成');
}
/**
* 打包路由文件
*
* @return void
*/
public function buildRouteFile()
{
$route_dir = 'route';
$list_files = $this->appFilesystem->listContents($route_dir, true);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$prettyPrinter = new MinifyPrinterTools();
foreach ($list_files as $item_file) {
if ($item_file['type'] == 'dir') {
continue;
}
$file_content = $this->appFilesystem->read($item_file['path']);
$file_stmts = $parser->parse($file_content);
$traverser = new NodeTraverser();
$traverser->addVisitor(new class($item_file['path'], $this) extends NodeVisitorAbstract
{
protected $name;
protected $mainClass;
public function __construct($name, $main_class)
{
$this->name = $name;
$this->mainClass = $main_class;
}
public function leaveNode(Node $node)
{
$name = $this->name;
if ($node instanceof Dir) {
// Clean out the function body
$const_key = 'dirconst' . uniqid();
$this->mainClass->constDirList[$const_key] = dirname($name);
return new ConstFetch(new Name($const_key));
}
if ($node->hasAttribute('comments')) {
$node->setAttribute('comments', []);
}
}
});
$function_code = $prettyPrinter->prettyPrintFile($file_stmts);
$this->distFilesystem->put($item_file['path'], $function_code);
}
}
/**
* 处理数据库迁移代码
*
* @return void
*/
public function buildMigrateFile()
{
$database_dir = 'database';
@@ -629,7 +693,11 @@ class Dist extends Command
public function isSkip($path)
{
$system_skip_path = ['/app\/common.php/'];
$system_skip_path = [
'/app\/common.php/',
'/^database\/*/',
'/^route\/*/',
];
$skip_path = Config::get('dist.skip_path', []);

View File

@@ -21,7 +21,7 @@ return [
'/^vendor/',
'/^config/',
'/^lib\//',
'/^database\/*/',
'/event\.php/',
'/middleware\.php/',
'/provider\.php/',
@@ -30,7 +30,7 @@ return [
'/config.php/',
'/^public\/index\.php/',
'/^public\/router\.php/',
'/^route\/*/',
'/^app\/admin\/service\/initAdminData\/*/',
]
];