From c42236da7e6eb4579bb0902591058d762761cbb3 Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 21 Jul 2025 19:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90curd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/utils/scaffold/Generate.php | 84 +++++++++---------- .../scaffold/templates/ControllerStub.stub | 33 ++++++++ .../utils/scaffold/templates/LogicStub.stub | 17 ++++ .../utils/scaffold/templates/ModelStub.stub | 49 +++++++++++ .../utils/scaffold/templates/ServiceStub.stub | 12 +-- .../scaffold/templates/ValidateStub.stub | 4 +- app/controller/IndexController.php | 44 ++++++---- composer.json | 3 +- composer.lock | 68 ++++++++++++++- config/route.php | 4 + 10 files changed, 248 insertions(+), 70 deletions(-) create mode 100644 app/common/utils/scaffold/templates/LogicStub.stub diff --git a/app/common/utils/scaffold/Generate.php b/app/common/utils/scaffold/Generate.php index d0b8a7d..687780d 100644 --- a/app/common/utils/scaffold/Generate.php +++ b/app/common/utils/scaffold/Generate.php @@ -7,19 +7,8 @@ use app\common\utils\TableInspector; class Generate { - // 表名 - protected $name = ''; - - // 控制器名 - protected $convertName = ''; - - // 模块名 - protected $moduleName = ''; - - // 表前缀 - protected $dbPrefix = 'hs_'; - + // 配置项 protected $options = []; public function __construct(array $options = []) @@ -43,7 +32,7 @@ class Generate $modelPath = __DIR__ . "/../../../{$this->options['model']['module']}/model/{$this->options['model']['name']}.php"; $validatePath = __DIR__ . "/../../../{$this->options['validate']['module']}/validate/{$this->options['validate']['name']}Validate.php"; - // // 创建目录(如果不存在) + // 创建目录(如果不存在) $fs->mkdir(__DIR__ . "/../../../{$this->options['controller']['module']}/controller"); $fs->mkdir(__DIR__ . "/../../../{$this->options['service']['module']}/service"); $fs->mkdir(__DIR__ . "/../../../{$this->options['logic']['module']}/logic"); @@ -58,19 +47,19 @@ class Generate $this->deleteFileIfExists($validatePath); // 生成控制器文件 - $ControllerStub = $this->generateFile($templateDir, 'ControllerStub.stub', $controllerPath); + $ControllerStub = $this->generateFile($templateDir, 'ControllerStub.stub', $controllerPath, 'controller'); // 生成服务类文件 - $ServiceStub = $this->generateFile($templateDir, 'ServiceStub.stub', $servicePath); + $ServiceStub = $this->generateFile($templateDir, 'ServiceStub.stub', $servicePath, 'service'); // 生成逻辑类文件 - $LogicStub = $this->generateFile($templateDir, 'LogicStub.stub', $logicPath); + $LogicStub = $this->generateFile($templateDir, 'LogicStub.stub', $logicPath, 'logic'); // 生成模型类文件 - $ModelStub = $this->generateFile($templateDir, 'ModelStub.stub', $modelPath); + $ModelStub = $this->generateFile($templateDir, 'ModelStub.stub', $modelPath, 'model'); // 生成验证器类文件 - $ValidatorStub = $this->generateFile($templateDir, 'ValidatorStub.stub', $validatePath); + $ValidatorStub = $this->generateFile($templateDir, 'ValidatorStub.stub', $validatePath, 'validate'); return $ControllerStub . $ServiceStub . $LogicStub . $ModelStub . $ValidatorStub . "文件生成成功!
"; } @@ -103,7 +92,7 @@ class Generate * @param string $templateFile 模板文件名 * @param string $outputPath 输出文件路径 */ - protected function generateFile($templateDir, $templateFile, $outputPath) + protected function generateFile($templateDir, $templateFile, $outputPath, $fileType) { $templatePath = $templateDir . $templateFile; @@ -113,16 +102,38 @@ class Generate } $content = file_get_contents($templatePath); - $content = str_replace('{{name}}', $this->convertName, $content); - // 填入table名 - $content = str_replace('{{table}}', $this->dbPrefix.$this->name, $content); - - // 填入module名 - $content = str_replace('{{module}}', $this->options['controller']['module'], $content); - - // 填入字段 - $content = str_replace('{{fillableFields}}', $this->fillableFields(), $content); + switch ($fileType) { + case 'controller': + $content = str_replace('{{controllerModule}}', $this->options['controller']['module'], $content); + $content = str_replace('{{serviceModule}}', $this->options['service']['module'], $content); + $content = str_replace('{{serviceName}}', $this->options['service']['name'], $content); + $content = str_replace('{{controllerName}}', $this->options['controller']['name'], $content); + break; + case 'service': + $content = str_replace('{{serviceModule}}', $this->options['service']['module'], $content); + $content = str_replace('{{validateName}}', $this->options['validate']['name'], $content); + $content = str_replace('{{modelModule}}', $this->options['model']['module'], $content); + $content = str_replace('{{modelName}}', $this->options['model']['name'], $content); + $content = str_replace('{{serviceName}}', $this->options['service']['name'], $content); + break; + case 'logic': + $content = str_replace('{{logicModule}}', $this->options['logic']['module'], $content); + $content = str_replace('{{modelModule}}', $this->options['model']['module'], $content); + $content = str_replace('{{modelName}}', $this->options['model']['name'], $content); + $content = str_replace('{{logicName}}', $this->options['logic']['name'], $content); + break; + case 'model': + $content = str_replace('{{modelModule}}', $this->options['model']['module'], $content); + $content = str_replace('{{modelName}}', $this->options['model']['name'], $content); + $content = str_replace('{{table}}', $this->options['table'], $content); + $content = str_replace('{{fillable}}', $this->options['fillable'], $content); + break; + case 'validate': + $content = str_replace('{{validateModule}}', $this->options['validate']['module'], $content); + $content = str_replace('{{validateName}}', $this->options['validate']['name'], $content); + break; + } file_put_contents($outputPath, $content); @@ -141,23 +152,6 @@ class Generate echo "删除已存在文件: {$path} \n"; } } - - /** - * 设置名称 - */ - public function setName($name = '') - { - $this->name = $name; - $this->convertName = ucfirst(camelize($name)); - } - - /** - * 设置模块名称 - */ - public function setModule($name = '') - { - $this->options['controller']['module'] = $name; - } } /** diff --git a/app/common/utils/scaffold/templates/ControllerStub.stub b/app/common/utils/scaffold/templates/ControllerStub.stub index e69de29..a74d97c 100644 --- a/app/common/utils/scaffold/templates/ControllerStub.stub +++ b/app/common/utils/scaffold/templates/ControllerStub.stub @@ -0,0 +1,33 @@ +service = new {{serviceName}}Service(); + } + + /** + * 列表页数据格式化 + * + * @param array $data + * @return array + */ + // protected function indexFormat($data) + // { + // if (isset($data['created_at'])) + // $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']); + // if (isset($data['updated_at'])) + // $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); + // return $data; + // } +} \ No newline at end of file diff --git a/app/common/utils/scaffold/templates/LogicStub.stub b/app/common/utils/scaffold/templates/LogicStub.stub new file mode 100644 index 0000000..da1afdc --- /dev/null +++ b/app/common/utils/scaffold/templates/LogicStub.stub @@ -0,0 +1,17 @@ + [ - 'module' => 'commonName', - 'name' => 'ControllerName' + 'module' => 'admin', + 'name' => 'systems' ], 'model' => [ - 'module' => 'commonName', - 'name' => 'modelName' + 'module' => 'admin', + 'name' => 'systems' ], 'service' => [ - 'module' => 'commonName', - 'name' => 'serviceName' + 'module' => 'admin', + 'name' => 'systems' ], 'logic' => [ - 'module' => 'commonName', - 'name' => 'logicName' + 'module' => 'admin', + 'name' => 'systems' ], 'validate' => [ - 'module' => 'commonName', - 'name' => 'validateName' + 'module' => 'admin', + 'name' => 'systems' ], - 'table' => 'tableName', - 'fillable' => [] + 'table' => 'systems', + 'fillable' => [ + 'code', + 'name', + 'type', + 'desc', + 'icon', + 'domain_url', + 'is_role', + 'sort', + 'status' + ] ]; + + $result = (new Generate($options))->doGenerate(); + return $result; } } \ No newline at end of file diff --git a/composer.json b/composer.json index e4e63bb..9847f65 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "shopwwi/webman-filesystem": "1.1", "shopwwi/filesystem-oss": "^1.0", "webman/redis-queue": "^2.1", - "symfony/translation": "^7.3" + "symfony/translation": "^7.3", + "symfony/filesystem": "^7.3" }, "suggest": { "ext-event": "For better performance. " diff --git a/composer.lock b/composer.lock index b9b58c7..3736333 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "30cc3cbb162022972a21efed9b69d1e1", + "content-hash": "6c03076d4c7c86514f6b2d0073545bea", "packages": [ { "name": "aliyuncs/oss-sdk-php", @@ -4284,6 +4284,72 @@ ], "time": "2024-09-25T14:21:43+00:00" }, + { + "name": "symfony/filesystem", + "version": "v7.3.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/filesystem.git", + "reference": "b8dce482de9d7c9fe2891155035a7248ab5c7fdb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/b8dce482de9d7c9fe2891155035a7248ab5c7fdb", + "reference": "b8dce482de9d7c9fe2891155035a7248ab5c7fdb", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.8" + }, + "require-dev": { + "symfony/process": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Filesystem\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides basic utilities for the filesystem", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/filesystem/tree/v7.3.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-10-25T15:15:23+00:00" + }, { "name": "symfony/finder", "version": "v7.3.0", diff --git a/config/route.php b/config/route.php index e5bb7f2..d0e9290 100644 --- a/config/route.php +++ b/config/route.php @@ -26,6 +26,10 @@ use Webman\Route; // POST /admin/user/{user_id}/kickout_all // 一键踢全部 +Route::any('/c', [app\controller\IndexController::class, 'createResource']); +Route::any('/perm', [app\controller\IndexController::class, 'perm']); + + // 公共 Route::group('/common', function () { Route::group('/auth', function () {