mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-09 03:22:51 +08:00
开始打包env
This commit is contained in:
@@ -31,7 +31,10 @@ use PhpParser\NodeTraverser;
|
|||||||
use PhpParser\NodeVisitorAbstract;
|
use PhpParser\NodeVisitorAbstract;
|
||||||
use PhpParser\ParserFactory;
|
use PhpParser\ParserFactory;
|
||||||
use app\common\tools\phpparser\PrettyPrinterTools as Standard;
|
use app\common\tools\phpparser\PrettyPrinterTools as Standard;
|
||||||
|
use app\common\tools\phpparser\PrettyPrinterTools;
|
||||||
|
use app\common\tools\phpparser\ReadEnvVisitorNodeTools;
|
||||||
use PhpParser\Comment;
|
use PhpParser\Comment;
|
||||||
|
use PhpParser\NodeVisitor\NameResolver;
|
||||||
use think\console\Command;
|
use think\console\Command;
|
||||||
use think\console\Input;
|
use think\console\Input;
|
||||||
use think\console\input\Argument;
|
use think\console\input\Argument;
|
||||||
@@ -108,6 +111,8 @@ class Dist extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->packEnv();
|
||||||
|
|
||||||
$list_content = $app_filesystem->listContents('', true);
|
$list_content = $app_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') {
|
||||||
@@ -168,12 +173,78 @@ class Dist extends Command
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$this->buildAllAppDir();
|
$this->buildAllAppDir();
|
||||||
|
|
||||||
|
|
||||||
$output->info('打包完成');
|
$output->info('打包完成');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function packEnv()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
$list_files = $this->appFilesystem->listContents('', true);
|
||||||
|
|
||||||
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||||
|
|
||||||
|
$prettyPrinter = new PrettyPrinterTools();
|
||||||
|
|
||||||
|
|
||||||
|
foreach ($list_files as $item_file) {
|
||||||
|
$path = $item_file['path'];
|
||||||
|
|
||||||
|
if ($item_file['type'] == 'dir') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($item_file['extension'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($item_file['extension'] != 'php') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->isPackEnv($path)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$file_content = $this->appFilesystem->read($path);
|
||||||
|
|
||||||
|
$file_stmts = $parser->parse($file_content);
|
||||||
|
|
||||||
|
$nameResolver = new NameResolver();
|
||||||
|
$nodeTraverser = new NodeTraverser;
|
||||||
|
$nodeTraverser->addVisitor($nameResolver);
|
||||||
|
|
||||||
|
// Resolve names
|
||||||
|
$file_stmts = $nodeTraverser->traverse($file_stmts);
|
||||||
|
|
||||||
|
$env_pack_visitor = new ReadEnvVisitorNodeTools;
|
||||||
|
$env_traverser = new NodeTraverser;
|
||||||
|
|
||||||
|
$env_traverser->addVisitor($env_pack_visitor);
|
||||||
|
$file_stmts = $env_traverser->traverse($file_stmts);
|
||||||
|
|
||||||
|
$result_content = $prettyPrinter->prettyPrintFile($file_stmts);
|
||||||
|
|
||||||
|
$this->appFilesystem->put($path, $result_content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isPackEnv($path)
|
||||||
|
{
|
||||||
|
$pack_env_path = Config::get('dist.pack_env_path');
|
||||||
|
foreach ($pack_env_path as $rule) {
|
||||||
|
|
||||||
|
if (preg_match($rule, $path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打包路由文件
|
* 打包路由文件
|
||||||
*
|
*
|
||||||
@@ -232,6 +303,7 @@ class Dist extends Command
|
|||||||
|
|
||||||
$file_stmts = $traverser_var_faker->traverse($file_stmts);
|
$file_stmts = $traverser_var_faker->traverse($file_stmts);
|
||||||
|
|
||||||
|
|
||||||
$function_code = $prettyPrinter->prettyPrintFile($file_stmts);
|
$function_code = $prettyPrinter->prettyPrintFile($file_stmts);
|
||||||
|
|
||||||
$this->distFilesystem->put($item_file['path'], $function_code);
|
$this->distFilesystem->put($item_file['path'], $function_code);
|
||||||
|
|||||||
@@ -86,6 +86,9 @@ class NodeVisitorTools extends NodeVisitorAbstract
|
|||||||
|
|
||||||
$this->usedClass[$used_class_str] = $name;
|
$this->usedClass[$used_class_str] = $name;
|
||||||
if (!empty($use_item->alias->name)) {
|
if (!empty($use_item->alias->name)) {
|
||||||
|
|
||||||
|
$name .= md5($use_item->alias->name);
|
||||||
|
|
||||||
$this->usedClass[$use_item->alias->name] = $name;
|
$this->usedClass[$use_item->alias->name] = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,71 @@
|
|||||||
|
|
||||||
namespace app\common\tools\phpparser;
|
namespace app\common\tools\phpparser;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean;
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Expr\Array_;
|
||||||
|
use PhpParser\Node\Expr\Cast\Bool_;
|
||||||
|
use PhpParser\Node\Expr\ConstFetch;
|
||||||
|
use PhpParser\Node\Expr\FuncCall;
|
||||||
|
use PhpParser\Node\Name;
|
||||||
|
use PhpParser\Node\Name\FullyQualified;
|
||||||
|
use PhpParser\Node\Scalar\DNumber;
|
||||||
|
use PhpParser\Node\Scalar\LNumber;
|
||||||
|
use PhpParser\Node\Scalar\String_;
|
||||||
use PhpParser\NodeVisitorAbstract;
|
use PhpParser\NodeVisitorAbstract;
|
||||||
|
class ReadEnvVisitorNodeTools extends \PhpParser\NodeVisitorAbstract
|
||||||
class ReadEnvVisitorNodeTools extends NodeVisitorAbstract
|
|
||||||
{
|
{
|
||||||
|
public function leaveNode(\PhpParser\Node $node)
|
||||||
|
{
|
||||||
|
if ($node instanceof \PhpParser\Node\Expr\FuncCall) {
|
||||||
|
if ($node->name instanceof \PhpParser\Node\Name\FullyQualified) {
|
||||||
|
$function_name = $node->name->toString();
|
||||||
|
if ($function_name == 'env') {
|
||||||
|
dump($node);
|
||||||
|
$env_arg_1 = $this->getArg($node, 0);
|
||||||
|
$env_arg_2 = $this->getArg($node, 1);
|
||||||
|
$env_value = env($env_arg_1, $env_arg_2);
|
||||||
|
return $this->returnEnvValue($env_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function getArg($node, $index)
|
||||||
|
{
|
||||||
|
$env = null;
|
||||||
|
if (isset($node->args[$index])) {
|
||||||
|
if ($node->args[$index]->value instanceof \PhpParser\Node\Scalar\String_) {
|
||||||
|
$env = $node->args[$index]->value->value;
|
||||||
|
} else {
|
||||||
|
// 发现非字符串方式读取,应当提示
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $env;
|
||||||
|
}
|
||||||
|
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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user