优化代码工程;开始打包函数库

This commit is contained in:
2022-11-18 17:16:30 +08:00
parent 63ff351d02
commit 086a60563f
2 changed files with 129 additions and 34 deletions

View File

@@ -35,6 +35,7 @@ use think\console\input\Argument;
use think\console\input\Option; use think\console\input\Option;
use think\console\Output; use think\console\Output;
use think\facade\App; use think\facade\App;
use think\facade\Config;
use think\facade\View; use think\facade\View;
use think\helper\Str; use think\helper\Str;
@@ -135,15 +136,48 @@ class Dist extends Command
$lib_php_file = '/lib.' . uniqid() . '.php'; $lib_php_file = '/lib.' . uniqid() . '.php';
$lib_php_path = $this->distPath . '/lib' . $lib_php_file; $lib_php_path = $this->distPath . '/lib' . $lib_php_file;
$this->buildMainClassFile($lib_php_path);
$lib_function_file = '/lib.' . uniqid() . '.php';
$lib_function_path = $this->distPath . '/lib' . $lib_function_file;
$this->buildFunctionFile($lib_function_path);
$lib_dir_const_file = '/lib.' . uniqid() . '.php';
$lib_dir_const_path = $this->distPath . '/lib' . $lib_dir_const_file;
$this->buildDirConstFile($lib_dir_const_path);
PathTools::intiDir($lib_php_path); PathTools::intiDir($lib_php_path);
$this->buildIncludeIndexFile([
$lib_function_file,
$lib_dir_const_file,
$lib_php_file,
]);
$this->buildAllAppDir();
$output->info('打包完成');
}
public function buildMainClassFile($lib_php_path)
{
$prettyPrinter = new Standard(); $prettyPrinter = new Standard();
// 根据调用次数排序 // 根据调用次数排序
$this->parsePackList(); $this->parsePackList();
@@ -158,10 +192,10 @@ class Dist extends Command
$newCode = $prettyPrinter->prettyPrintFile($stmts); $newCode = $prettyPrinter->prettyPrintFile($stmts);
file_put_contents($lib_php_path, $newCode); file_put_contents($lib_php_path, $newCode);
}
$lib_dir_const_file = '/lib.dir.const.' . uniqid() . '.php'; public function buildDirConstFile($lib_dir_const_path)
$lib_dir_const_path = $this->distPath . '/lib' . $lib_dir_const_file; {
$dir_const_stmts = []; $dir_const_stmts = [];
foreach ($this->constDirList as $const_key => $const_value) { foreach ($this->constDirList as $const_key => $const_value) {
@@ -178,20 +212,66 @@ class Dist extends Command
)); ));
} }
$prettyPrinter = new Standard();
$dir_const_code = $prettyPrinter->prettyPrintFile($dir_const_stmts); $dir_const_code = $prettyPrinter->prettyPrintFile($dir_const_stmts);
file_put_contents($lib_dir_const_path, $dir_const_code); file_put_contents($lib_dir_const_path, $dir_const_code);
}
$this->buildIncludeIndexFile([ public function buildFunctionFile($lib_function_path)
$lib_dir_const_file, {
$lib_php_file, $function_path = Config::get('dist.function_path', []);
]);
$this->buildAllAppDir(); $function_stmts = [];
$output->info('打包完成'); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
foreach ($function_path as $function_file) {
$function_file_path = App::getRootPath() . '' . $function_file;
$function_content = file_get_contents($function_file_path);
$stmts = $parser->parse($function_content);
$traverser = new NodeTraverser();
$traverser->addVisitor(new class($function_file, $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));
}
}
});
foreach ($stmts as $stmt_item) {
$function_stmts[] = $stmt_item;
}
}
$prettyPrinter = new Standard();
$function_code = $prettyPrinter->prettyPrintFile($function_stmts);
file_put_contents($lib_function_path, $function_code);
} }
public function buildAllAppDir() public function buildAllAppDir()
@@ -418,11 +498,7 @@ class Dist extends Command
public function isSkip($path) public function isSkip($path)
{ {
$skip_path = [ $skip_path = Config::get('dist.skip_path', []);
'/^\.git/',
'/^dist/',
'/^runtime/',
];
foreach ($skip_path as $rule) { foreach ($skip_path as $rule) {
if (preg_match($rule, $path)) { if (preg_match($rule, $path)) {
@@ -436,23 +512,7 @@ class Dist extends Command
public function isIgnored($path) public function isIgnored($path)
{ {
$ignore_path = [ $ignore_path = Config::get('dist.ignore_path', []);
'/^vendor/',
'/^config/',
'/^lib\//',
'/^database\/*/',
'/event\.php/',
'/middleware\.php/',
'/provider\.php/',
'/service\.php/',
'/^app\/.*\/config\/.*/',
'/app\/common.php/',
'/config.php/',
'/^public\/index\.php/',
'/^public\/router\.php/',
'/^route\/*/',
'/^app\/admin\/service\/initAdminData\/*/',
];
foreach ($ignore_path as $rule) { foreach ($ignore_path as $rule) {

35
config/dist.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
return [
// 全局函数库,否则无论是否以/开头,都以项目根目录开头定位,如果有其他的文件,在这里声明
// 不支持项目以外的位置定义
// 只能包含函数,不支持命名空间
// 这些代码不能包含__DIR__、__FILE__等代码位置常量不会影响打包但代码逻辑会被破坏
'function_path' => [
'app/common.php'
],
// 支持正则表达式,直接跳过所有文件
'skip_path' => [
'/^\.git/',
'/^dist/',
'/^runtime/',
],
// 支持正则表达式,将文件原封不动的挪到输出目录
'ignore_path' => [
'/^vendor/',
'/^config/',
'/^lib\//',
'/^database\/*/',
'/event\.php/',
'/middleware\.php/',
'/provider\.php/',
'/service\.php/',
'/^app\/.*\/config\/.*/',
'/app\/common.php/',
'/config.php/',
'/^public\/index\.php/',
'/^public\/router\.php/',
'/^route\/*/',
'/^app\/admin\/service\/initAdminData\/*/',
]
];