清理技术债务

This commit is contained in:
2022-12-14 17:12:43 +08:00
parent e6e68561b9
commit b593840316

View File

@@ -702,40 +702,6 @@ class Dist extends Command
}
}
/**
* 统一声明目录魔术常量
*
* @param string $lib_dir_const_path
* @return void
*/
public function buildDirConstFile()
{
$dir_const_stmts = [];
foreach ($this->constDirList as $const_key => $const_value) {
$dir_const_stmts[] = new Expression(new FuncCall(
new Name('define'),
[
new Arg(new String_($const_key)),
new Arg(new Concat(
new Dir(),
new String_('/../' . $const_value)
)),
]
));
}
$prettyPrinter = new MinifyPrinterTools();
$dir_const_code = $prettyPrinter->prettyPrintFile($dir_const_stmts);
$dir_const_path = 'lib/' . uniqid() . '.php';
$this->includeLibPath['dir_const_file'] = $dir_const_path;
$this->tempFilesystem->put($dir_const_path, $dir_const_code);
}
public function buildMagicVarMapFile()
{
$dir_const_stmts = [];
@@ -765,139 +731,6 @@ class Dist extends Command
}
/**
* 打包路由文件
*
* @return void
*/
public function buildRouteFile()
{
$route_dir = 'route';
$list_files = $this->tempFilesystem->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->tempFilesystem->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', []);
}
}
});
$traverser_var_faker = new NodeTraverser();
$traverser_var_faker->addVisitor(new NodeFakeVarVisitorTools);
$file_stmts = $traverser_var_faker->traverse($file_stmts);
$function_code = $prettyPrinter->prettyPrintFile($file_stmts);
$this->distFilesystem->put($item_file['path'], $function_code);
}
}
/**
* 处理数据库迁移代码
*
* @return void
*/
public function buildMigrateFile()
{
$database_dir = 'database';
$list_files = $this->tempFilesystem->listContents($database_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->tempFilesystem->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', []);
}
}
});
$file_stmts = $traverser->traverse($file_stmts);
$traverser_var_faker = new NodeTraverser();
$traverser_var_faker->addVisitor(new NodeFakeVarVisitorTools);
$file_stmts = $traverser_var_faker->traverse($file_stmts);
$function_code = $prettyPrinter->prettyPrintFile($file_stmts);
$this->distFilesystem->put($item_file['path'], $function_code);
}
}
public function packMainClassFile()
{
@@ -1169,9 +1002,6 @@ class Dist extends Command
return $content;
}
// $stmts = $this->parseStmts($stmts, $name);
$class_name = null;
$extend_name = null;
$namespace_name = null;
@@ -1221,98 +1051,5 @@ class Dist extends Command
}
/**
* 遍历扫描代码
*
* @param Node\Stmt[]|null $stmts
* @param string $name
* @return Node\Stmt[]
*/
public function parseStmts($stmts, $name)
{
$traverser = new NodeTraverser();
$node_visitor = new NodeVisitorTools($this, $name);
$traverser->addVisitor($node_visitor);
$stmts = $traverser->traverse($stmts);
return $stmts;
}
/**
* 扫描魔术变量
*
* @param Node\Stmt[]|null $stmts
* @param string $name
* @return void
*/
public function scanForMagicConstDir($stmts, $name)
{
$traverser = new NodeTraverser();
$traverser->addVisitor(new class($name, $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));
}
}
});
$stmts = $traverser->traverse($stmts);
}
public function isSkip($path)
{
$system_skip_path = [
'/app\/common.php/',
'/^database\/*/',
'/^route\/*/',
];
$skip_path = Config::get('dist.skip_path', []);
$skip_path = array_merge($system_skip_path, $skip_path);
foreach ($skip_path as $rule) {
if (preg_match($rule, $path)) {
return true;
}
}
return false;
}
public function isIgnored($path)
{
$ignore_path = Config::get('dist.ignore_path', []);
foreach ($ignore_path as $rule) {
if (preg_match($rule, $path)) {
return true;
}
}
return false;
}
}