完成所有打包的功能点

This commit is contained in:
2022-11-25 16:00:57 +08:00
parent 8a41942ea6
commit ed3ef2609a
5 changed files with 166 additions and 78 deletions

1
.gitignore vendored
View File

@@ -16,3 +16,4 @@ ul.db
/app/tools/controller/Install.php /app/tools/controller/Install.php
/app/common/command/curd/migrate_output.php /app/common/command/curd/migrate_output.php
/dist /dist
/temp

View File

@@ -68,6 +68,11 @@ class Dist extends Command
*/ */
protected $distFilesystem; protected $distFilesystem;
/**
* @var FileSystem
*/
protected $tempFilesystem;
protected function configure() protected function configure()
{ {
// 指令配置 // 指令配置
@@ -83,24 +88,32 @@ class Dist extends Command
$app_path = App::getRootPath(); $app_path = App::getRootPath();
$dist_path = $app_path . 'dist'; $dist_path = $app_path . 'dist';
PathTools::intiDir($dist_path . '.temp'); PathTools::intiDir($dist_path . '.temp');
$temp_path = $app_path . 'temp';
PathTools::intiDir($temp_path . '.temp');
$this->distPath = $dist_path; $this->distPath = $dist_path;
$app_adapter = new Local($app_path); $app_adapter = new Local($app_path);
$app_filesystem = new Filesystem($app_adapter); $app_filesystem = new Filesystem($app_adapter);
$this->appFilesystem = $app_filesystem; $this->appFilesystem = $app_filesystem;
$dist_adapter = new Local($dist_path); $dist_adapter = new Local($dist_path);
$dist_filesystem = new Filesystem($dist_adapter); $dist_filesystem = new Filesystem($dist_adapter);
$this->distFilesystem = $dist_filesystem; $this->distFilesystem = $dist_filesystem;
$temp_adapter = new Local($temp_path);
$temp_filesystem = new Filesystem($temp_adapter);
$this->tempFilesystem = $temp_filesystem;
$list_dist = $dist_filesystem->listContents(); $list_dist = $dist_filesystem->listContents();
foreach ($list_dist as $file_info) { foreach ($list_dist as $file_info) {
@@ -113,7 +126,9 @@ class Dist extends Command
$this->packEnv(); $this->packEnv();
$list_content = $app_filesystem->listContents('', true); return ;
$list_content = $temp_filesystem->listContents('', true);
foreach ($list_content as $file_info) { foreach ($list_content as $file_info) {
if ($file_info['type'] == 'dir') { if ($file_info['type'] == 'dir') {
continue; continue;
@@ -125,7 +140,7 @@ class Dist extends Command
continue; continue;
} }
$file_content = $app_filesystem->read($file_path); $file_content = $temp_filesystem->read($file_path);
$path_info = pathinfo($file_path); $path_info = pathinfo($file_path);
if (!$this->isIgnored($file_path)) { if (!$this->isIgnored($file_path)) {
@@ -175,23 +190,32 @@ class Dist extends Command
$this->buildAllAppDir(); $this->buildAllAppDir();
$this->clearTempDir();
$output->info('打包完成'); $output->info('打包完成');
} }
public function clearTempDir()
{
$list_dist = $this->tempFilesystem->listContents();
foreach ($list_dist as $file_info) {
if ($file_info['type'] == 'file') {
$this->tempFilesystem->delete($file_info['path']);
} else {
$this->tempFilesystem->deleteDir($file_info['path']);
}
}
}
public function packEnv() public function packEnv()
{ {
$list_files = $this->appFilesystem->listContents('', true); $list_files = $this->appFilesystem->listContents('', true);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$prettyPrinter = new PrettyPrinterTools(); $prettyPrinter = new PrettyPrinterTools();
foreach ($list_files as $item_file) { foreach ($list_files as $item_file) {
$path = $item_file['path']; $path = $item_file['path'];
@@ -199,17 +223,21 @@ class Dist extends Command
continue; continue;
} }
if (!isset($item_file['extension'])) { $skip_path = Config::get('dist.skip_path', []);
continue;
} foreach ($skip_path as $rule) {
if ($item_file['extension'] != 'php') { if (preg_match($rule, $path)) {
continue; continue 2;
}
} }
if (!$this->isPackEnv($path)) {
$file_content = $this->appFilesystem->read($path);
if (!$this->isPackEnv($item_file)) {
$this->tempFilesystem->put($path, $file_content);
continue; continue;
} }
$file_content = $this->appFilesystem->read($path);
$file_stmts = $parser->parse($file_content); $file_stmts = $parser->parse($file_content);
@@ -220,7 +248,7 @@ class Dist extends Command
// Resolve names // Resolve names
$file_stmts = $nodeTraverser->traverse($file_stmts); $file_stmts = $nodeTraverser->traverse($file_stmts);
$env_pack_visitor = new ReadEnvVisitorNodeTools; $env_pack_visitor = new ReadEnvVisitorNodeTools($path);
$env_traverser = new NodeTraverser; $env_traverser = new NodeTraverser;
$env_traverser->addVisitor($env_pack_visitor); $env_traverser->addVisitor($env_pack_visitor);
@@ -228,16 +256,27 @@ class Dist extends Command
$result_content = $prettyPrinter->prettyPrintFile($file_stmts); $result_content = $prettyPrinter->prettyPrintFile($file_stmts);
$this->appFilesystem->put($path, $result_content); $this->tempFilesystem->put($path, $result_content);
}
if($this->tempFilesystem->has('.env')){
$this->tempFilesystem->delete('.env');
} }
} }
public function isPackEnv($path) public function isPackEnv($item_file)
{ {
$pack_env_path = Config::get('dist.pack_env_path'); if (!isset($item_file['extension'])) {
return false;
}
if ($item_file['extension'] != 'php') {
return false;
}
$pack_env_path = Config::get('dist.pack_env_path', []);
foreach ($pack_env_path as $rule) { foreach ($pack_env_path as $rule) {
if (preg_match($rule, $path)) { if (preg_match($rule, $item_file['path'])) {
return true; return true;
} }
} }
@@ -254,7 +293,7 @@ class Dist extends Command
{ {
$route_dir = 'route'; $route_dir = 'route';
$list_files = $this->appFilesystem->listContents($route_dir, true); $list_files = $this->tempFilesystem->listContents($route_dir, true);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
@@ -265,7 +304,7 @@ class Dist extends Command
continue; continue;
} }
$file_content = $this->appFilesystem->read($item_file['path']); $file_content = $this->tempFilesystem->read($item_file['path']);
$file_stmts = $parser->parse($file_content); $file_stmts = $parser->parse($file_content);
@@ -319,7 +358,7 @@ class Dist extends Command
{ {
$database_dir = 'database'; $database_dir = 'database';
$list_files = $this->appFilesystem->listContents($database_dir, true); $list_files = $this->tempFilesystem->listContents($database_dir, true);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
@@ -331,7 +370,7 @@ class Dist extends Command
continue; continue;
} }
$file_content = $this->appFilesystem->read($item_file['path']); $file_content = $this->tempFilesystem->read($item_file['path']);
$file_stmts = $parser->parse($file_content); $file_stmts = $parser->parse($file_content);

View File

@@ -82,15 +82,14 @@ class NodeVisitorTools extends NodeVisitorAbstract
return; return;
} }
if(isset($this->usedClass[$used_class_str])){
return NodeTraverser::REMOVE_NODE;
}
$name .= md5($used_class_str); $name .= md5($used_class_str);
$this->usedClass[$used_class_str] = $name; $this->usedClass[$used_class_str] = $name;
if (!empty($use_item->alias->name)) {
$name .= md5($use_item->alias->name);
$this->usedClass[$use_item->alias->name] = $name;
}
$use_item->alias = new Identifier($name); $use_item->alias = new Identifier($name);
} }

View File

@@ -8,65 +8,106 @@ use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Cast\Bool_; use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\DNumber; use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_; use PhpParser\Node\Scalar\String_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract; use PhpParser\NodeVisitorAbstract;
class ReadEnvVisitorNodeTools extends \PhpParser\NodeVisitorAbstract
class ReadEnvVisitorNodeTools extends NodeVisitorAbstract
{ {
public function leaveNode(\PhpParser\Node $node) protected $path;
protected $skipFiles = [
'app/common/tools/phpparser/ReadEnvVisitorNodeTools.php'
];
public function __construct($path)
{ {
if ($node instanceof \PhpParser\Node\Expr\FuncCall) { $this->path = $path;
if ($node->name instanceof \PhpParser\Node\Name\FullyQualified) { }
public function enterNode(Node $node)
{
if (in_array($this->path, $this->skipFiles)) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
}
public function leaveNode(Node $node)
{
if ($node instanceof FuncCall) {
if ($node->name instanceof Name) {
$function_name = $node->name->toString(); $function_name = $node->name->toString();
if ($function_name == 'env') { if ($function_name == 'env') {
dump($node);
$env_arg_1 = $this->getArg($node, 0); $env_arg_1 = $this->getArg($node, 0);
$env_arg_2 = $this->getArg($node, 1); $env_arg_2 = $this->getArg($node, 1);
$env_value = env($env_arg_1, $env_arg_2); $env_value = env($env_arg_1, $env_arg_2);
return $this->returnEnvValue($env_value);
$return_node = $this->returnEnvValue($env_value);
return $return_node;
} }
} }
} } else if ($node instanceof StaticCall) {
}
public function getArg($node, $index) if ($node->class instanceof FullyQualified) {
{ $class_name = $node->class->toString();
$env = null; if ($class_name == 'think\\facade\\Env') {
if (isset($node->args[$index])) { if ($node->name instanceof Identifier) {
if ($node->args[$index]->value instanceof \PhpParser\Node\Scalar\String_) { if ($node->name == 'get') {
$env = $node->args[$index]->value->value; $env_arg_1 = $this->getArg($node, 0);
} else { $env_arg_2 = $this->getArg($node, 1);
// 发现非字符串方式读取,应当提示
} $env_value = env($env_arg_1, $env_arg_2);
}
return $env; $return_node = $this->returnEnvValue($env_value);
} return $return_node;
public function returnEnvValue($value)
{
if (is_array($value)) {
return new \PhpParser\Node\Expr\Array_($value);
} else {
if (is_string($value)) {
return new \PhpParser\Node\Scalar\String_($value);
} else {
if (is_integer($value)) {
return new \PhpParser\Node\Scalar\LNumber($value);
} else {
if (is_float($value)) {
return new \PhpParser\Node\Scalar\DNumber($value);
} else {
if (is_bool($value)) {
if ($value) {
return new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('true'));
} else {
return new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('false'));
}
} }
} }
} }
} }
} }
} }
public function getArg($node, $index)
{
$env = null;
if (isset($node->args[$index])) {
if ($node->args[$index]->value instanceof String_) {
$env = $node->args[$index]->value->value;
} else {
// 发现非字符串方式读取,应当提示
}
}
return $env;
}
public function returnEnvValue($value)
{
if (is_array($value)) {
return new Array_($value);
} else if (is_string($value)) {
return new String_($value);
} else if (is_integer($value)) {
return new LNumber($value);
} else if (is_float($value)) {
return new DNumber($value);
} else if (is_bool($value)) {
if ($value) {
return new ConstFetch(new Name('true'));
} else {
return new ConstFetch(new Name('false'));
}
}
}
} }

View File

@@ -21,7 +21,6 @@ return [
'/^vendor/', '/^vendor/',
'/^config/', '/^config/',
'/^lib\//', '/^lib\//',
'/event\.php/', '/event\.php/',
'/middleware\.php/', '/middleware\.php/',
'/provider\.php/', '/provider\.php/',
@@ -32,5 +31,14 @@ return [
'/^public\/router\.php/', '/^public\/router\.php/',
'/^app\/admin\/service\/initAdminData\/*/', '/^app\/admin\/service\/initAdminData\/*/',
],
// 希望将env打包的文件
'pack_env_path'=>[
'/^app/',
'/^config/',
'/^database/',
'/^extend/',
'/^lib/',
'/^route/',
] ]
]; ];