开始代码打包

This commit is contained in:
2022-11-02 13:38:49 +08:00
parent c3b8c93376
commit f4c00c97ca
7 changed files with 379 additions and 4 deletions

3
.gitignore vendored
View File

@@ -14,4 +14,5 @@ vendor
runtime
ul.db
/app/tools/controller/Install.php
/app/common/command/curd/migrate_output.php
/app/common/command/curd/migrate_output.php
/dist

View File

@@ -0,0 +1,61 @@
<?php
namespace app\common\class\phpparser;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
class NodeVisitor extends NodeVisitorAbstract
{
protected $cmd;
protected $name;
public function __construct($cmd, $name)
{
$this->cmd = $cmd;
$this->name = $name;
}
public function leaveNode(Node $node)
{
if ($node instanceof Use_) {
// $this->cmd->addUsedClass($node);
// return NodeTraverser::REMOVE_NODE;
foreach ($node->uses as &$use_item) {
$name = 'class';
$name .= md5($this->name);
$name .= md5(end($use_item->name->parts));
$use_item->alias = new Identifier($name);
}
}else if ($node instanceof Class_){
if(!empty($node->extends)){
$name = 'class';
$name .= md5($this->name);
$name .= md5(end($node->extends->parts));
$node->extends = new Name($name);
}
}
return null;
}
}

View File

@@ -0,0 +1,239 @@
<?php
declare(strict_types=1);
namespace app\common\command\build;
use app\common\class\phpparser\NodeVisitor;
use app\common\exception\DirFindedException;
use app\common\tools\PathTools;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use PhpParser\Node;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\App;
class Dist extends Command
{
public $packList = [];
public $usedClass = [];
protected $distPath;
protected function configure()
{
// 指令配置
$this->setName('build:dist')
->setDescription('the build:dist command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('build:dist');
$app_path = App::getRootPath();
$dist_path = $app_path . 'dist';
PathTools::intiDir($dist_path . '.temp');
$this->distPath = $dist_path;
$app_adapter = new Local($app_path);
$app_filesystem = new Filesystem($app_adapter);
$dist_adapter = new Local($dist_path);
$dist_filesystem = new Filesystem($dist_adapter);
$list_dist = $dist_filesystem->listContents();
foreach ($list_dist as $file_info) {
if ($file_info['type'] == 'file') {
$dist_filesystem->delete($file_info['path']);
} else {
$dist_filesystem->deleteDir($file_info['path']);
}
}
$list_content = $app_filesystem->listContents('', true);
foreach ($list_content as $file_info) {
if ($file_info['type'] == 'dir') {
continue;
}
$file_path = $file_info['path'];
if ($this->isSkip($file_path)) {
continue;
}
$file_content = $app_filesystem->read($file_path);
$path_info = pathinfo($file_path);
if (!$this->isIgnored($file_path)) {
if (isset($path_info['extension'])) {
if ($path_info['extension'] == 'php') {
$file_content = $this->buildPhpContent($file_content, $file_path);
}
}
}
if (!is_null($file_content)) {
$dist_filesystem->write($file_path, $file_content);
}
}
$lib_php_path = $this->distPath . '/lib/index.' . uniqid() . '.php';
PathTools::intiDir($lib_php_path);
$prettyPrinter = new Standard();
$stmts = array_merge($this->usedClass, $this->packList);
$newCode = $prettyPrinter->prettyPrintFile($stmts);
file_put_contents($lib_php_path, $newCode);
$output->info('打包完成');
}
public function buildPhpContent($content, $name)
{
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$stmts = $parser->parse($content);
$is_magic_const_dir = $this->scanForMagicConstDir($stmts);
if ($is_magic_const_dir) {
return $content;
}
$stmts = $this->parseStmts($stmts, $name);
$this->packList = array_merge($this->packList, $stmts);
return null;
}
public function addUsedClass(Node $node)
{
if (array_search($node, $this->usedClass)) {
dump('已存在');
} else {
$this->usedClass[] = $node;
}
}
public function parseStmts($stmts, $name)
{
$traverser = new NodeTraverser();
$node_visitor = new NodeVisitor($this, $name);
$traverser->addVisitor($node_visitor);
$stmts = $traverser->traverse($stmts);
return $stmts;
}
public function scanForMagicConstDir($stmts)
{
$is_dir_find = false;
$traverser = new NodeTraverser();
$traverser->addVisitor(new class extends NodeVisitorAbstract
{
public function enterNode(Node $node)
{
if ($node instanceof Dir) {
// Clean out the function body
throw new DirFindedException("finded", 1);
}
}
});
try {
$stmts = $traverser->traverse($stmts);
} catch (DirFindedException $th) {
$is_dir_find = true;
}
return $is_dir_find;
}
public function isSkip($path)
{
$skip_path = [
'/^\.git/',
'/^dist/',
'/^runtime/',
];
foreach ($skip_path as $rule) {
if (preg_match($rule, $path)) {
return true;
}
}
return false;
}
public function isIgnored($path)
{
$ignore_path = [
'/^vendor/',
'/^config/',
'/event\.php/',
'/middleware\.php/',
'/provider\.php/',
'/service\.php/',
'/^app\/.*\/config\/.*/',
'/config.php/',
'/^public\/index\.php/',
'/^public\/router\.php/',
];
foreach ($ignore_path as $rule) {
if (preg_match($rule, $path)) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace app\common\exception;
use Exception;
class DirFindedException extends Exception
{
}

View File

@@ -30,7 +30,8 @@
"overtrue/flysystem-qiniu": "*",
"xxtime/flysystem-aliyun-oss": "^1.5",
"chunpat/flysystem-tencent-cos": "^0.0.1",
"doctrine/annotations": "^1.13"
"doctrine/annotations": "^1.13",
"nikic/php-parser": "^4.15"
},
"require-dev": {
"symfony/var-dumper": "^4.2"

64
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b891b8bf24dd134097273cb805ddae06",
"content-hash": "c5692d63c499bd4c9f4f12a349a58fe3",
"packages": [
{
"name": "aliyuncs/oss-sdk-php",
@@ -1272,6 +1272,68 @@
],
"time": "2021-07-05T08:18:36+00:00"
},
{
"name": "nikic/php-parser",
"version": "v4.15.1",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "0ef6c55a3f47f89d7a374e6f835197a0b5fcf900"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/0ef6c55a3f47f89d7a374e6f835197a0b5fcf900",
"reference": "0ef6c55a3f47f89d7a374e6f835197a0b5fcf900",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-tokenizer": "*",
"php": ">=7.0"
},
"require-dev": {
"ircmaxell/php-yacc": "^0.0.7",
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0"
},
"bin": [
"bin/php-parse"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.9-dev"
}
},
"autoload": {
"psr-4": {
"PhpParser\\": "lib/PhpParser"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Nikita Popov"
}
],
"description": "A PHP parser written in PHP",
"keywords": [
"parser",
"php"
],
"support": {
"issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v4.15.1"
},
"time": "2022-09-04T07:30:47+00:00"
},
{
"name": "overtrue/flysystem-qiniu",
"version": "1.0.6",

View File

@@ -6,6 +6,7 @@
use app\common\command\admin\Clear;
use app\common\command\admin\Version;
use app\common\command\admin\ResetPassword;
use app\common\command\build\Dist;
use app\common\command\curd\Migrate;
use app\common\command\Timer;
@@ -19,6 +20,7 @@ return [
Timer::class,
Version::class,
Migrate::class,
Clear::class
Clear::class,
Dist::class
],
];