自动生成curd

This commit is contained in:
mzl
2025-07-21 19:11:29 +08:00
parent 6580e21880
commit c42236da7e
10 changed files with 248 additions and 70 deletions
+39 -45
View File
@@ -7,19 +7,8 @@ use app\common\utils\TableInspector;
class Generate class Generate
{ {
// 表名
protected $name = '';
// 控制器名
protected $convertName = '';
// 模块名
protected $moduleName = '';
// 表前缀
protected $dbPrefix = 'hs_';
// 配置项
protected $options = []; protected $options = [];
public function __construct(array $options = []) public function __construct(array $options = [])
@@ -43,7 +32,7 @@ class Generate
$modelPath = __DIR__ . "/../../../{$this->options['model']['module']}/model/{$this->options['model']['name']}.php"; $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"; $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['controller']['module']}/controller");
$fs->mkdir(__DIR__ . "/../../../{$this->options['service']['module']}/service"); $fs->mkdir(__DIR__ . "/../../../{$this->options['service']['module']}/service");
$fs->mkdir(__DIR__ . "/../../../{$this->options['logic']['module']}/logic"); $fs->mkdir(__DIR__ . "/../../../{$this->options['logic']['module']}/logic");
@@ -58,19 +47,19 @@ class Generate
$this->deleteFileIfExists($validatePath); $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 . "<info>文件生成成功!</info> <br/>"; return $ControllerStub . $ServiceStub . $LogicStub . $ModelStub . $ValidatorStub . "<info>文件生成成功!</info> <br/>";
} }
@@ -103,7 +92,7 @@ class Generate
* @param string $templateFile 模板文件名 * @param string $templateFile 模板文件名
* @param string $outputPath 输出文件路径 * @param string $outputPath 输出文件路径
*/ */
protected function generateFile($templateDir, $templateFile, $outputPath) protected function generateFile($templateDir, $templateFile, $outputPath, $fileType)
{ {
$templatePath = $templateDir . $templateFile; $templatePath = $templateDir . $templateFile;
@@ -113,16 +102,38 @@ class Generate
} }
$content = file_get_contents($templatePath); $content = file_get_contents($templatePath);
$content = str_replace('{{name}}', $this->convertName, $content);
// 填入table名 switch ($fileType) {
$content = str_replace('{{table}}', $this->dbPrefix.$this->name, $content); case 'controller':
$content = str_replace('{{controllerModule}}', $this->options['controller']['module'], $content);
// 填入module名 $content = str_replace('{{serviceModule}}', $this->options['service']['module'], $content);
$content = str_replace('{{module}}', $this->options['controller']['module'], $content); $content = str_replace('{{serviceName}}', $this->options['service']['name'], $content);
$content = str_replace('{{controllerName}}', $this->options['controller']['name'], $content);
// 填入字段 break;
$content = str_replace('{{fillableFields}}', $this->fillableFields(), $content); 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); file_put_contents($outputPath, $content);
@@ -141,23 +152,6 @@ class Generate
echo "删除已存在文件: <comment>{$path}</comment> \n"; echo "删除已存在文件: <comment>{$path}</comment> \n";
} }
} }
/**
* 设置名称
*/
public function setName($name = '')
{
$this->name = $name;
$this->convertName = ucfirst(camelize($name));
}
/**
* 设置模块名称
*/
public function setModule($name = '')
{
$this->options['controller']['module'] = $name;
}
} }
/** /**
@@ -0,0 +1,33 @@
<?php
namespace app\{{controllerModule}}\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\{{serviceModule}}\service\{{serviceName}}Service;
class {{controllerName}}Controller extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->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;
// }
}
@@ -0,0 +1,17 @@
<?php
namespace app\{{logicModule}}\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\{{modelModule}}\model\{{modelName}};
class {{logicName}}Logic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = {{modelName}}::class;
}
@@ -0,0 +1,49 @@
<?php
namespace app\{{modelModule}}\model;
use support\Model;
class {{modelName}} extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = '{{table}}';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = [{{fillable}}];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
@@ -1,12 +1,12 @@
<?php <?php
namespace app\{{module}}\service; namespace app\{{serviceModule}}\service;
use app\common\validate\RoleValidate; use app\{{validateModule}}\validate\{{validateName}}Validate;
use app\common\traits\crud\CrudServiceTrait; use app\common\traits\crud\CrudServiceTrait;
use app\common\model\Role; use app\{{modelModule}}\model\{{modelName}};
class RoleService class {{serviceName}}Service
{ {
use CrudServiceTrait; use CrudServiceTrait;
@@ -14,12 +14,12 @@ class RoleService
/** /**
* 指定当前要操作的模型 * 指定当前要操作的模型
*/ */
protected string $modelClass = Role::class; protected string $modelClass = {{modelName}}::class;
/** /**
* 验证器 * 验证器
*/ */
protected $validator = RoleValidate::class; protected $validator = {{validateName}}Validate::class;
/** /**
* 获取验证器 * 获取验证器
@@ -1,10 +1,10 @@
<?php <?php
namespace app\{{module}}\validate; namespace app\{{validateModule}}\validate;
use think\Validate; use think\Validate;
class {{name}}Validator extends Validate class {{validateName}}Validator extends Validate
{ {
// 规则 // 规则
protected $rule = [ protected $rule = [
+29 -15
View File
@@ -5,6 +5,7 @@ namespace app\controller;
use app\common\model\User; use app\common\model\User;
use app\common\model\UserRoles; use app\common\model\UserRoles;
use app\common\model\UserSystems; use app\common\model\UserSystems;
use app\common\utils\scaffold\Generate;
use support\Request; use support\Request;
use support\Db; use support\Db;
use support\Cache; use support\Cache;
@@ -12,9 +13,9 @@ use support\Cache;
class IndexController class IndexController
{ {
protected $noNeedLogin = ['index', 'view', 'perm']; protected $noNeedLogin = ['index', 'view', 'perm', 'createResource'];
protected $noNeedHeaderData = ['index', 'view', 'perm']; protected $noNeedHeaderData = ['index', 'view', 'perm', 'createResource'];
public function index(Request $request) public function index(Request $request)
{ {
@@ -66,31 +67,44 @@ class IndexController
} }
public function c_() public function createResource()
{ {
$options = [ $options = [
'controller' => [ 'controller' => [
'module' => 'commonName', 'module' => 'admin',
'name' => 'ControllerName' 'name' => 'systems'
], ],
'model' => [ 'model' => [
'module' => 'commonName', 'module' => 'admin',
'name' => 'modelName' 'name' => 'systems'
], ],
'service' => [ 'service' => [
'module' => 'commonName', 'module' => 'admin',
'name' => 'serviceName' 'name' => 'systems'
], ],
'logic' => [ 'logic' => [
'module' => 'commonName', 'module' => 'admin',
'name' => 'logicName' 'name' => 'systems'
], ],
'validate' => [ 'validate' => [
'module' => 'commonName', 'module' => 'admin',
'name' => 'validateName' 'name' => 'systems'
], ],
'table' => 'tableName', 'table' => 'systems',
'fillable' => [] 'fillable' => [
'code',
'name',
'type',
'desc',
'icon',
'domain_url',
'is_role',
'sort',
'status'
]
]; ];
$result = (new Generate($options))->doGenerate();
return $result;
} }
} }
+2 -1
View File
@@ -41,7 +41,8 @@
"shopwwi/webman-filesystem": "1.1", "shopwwi/webman-filesystem": "1.1",
"shopwwi/filesystem-oss": "^1.0", "shopwwi/filesystem-oss": "^1.0",
"webman/redis-queue": "^2.1", "webman/redis-queue": "^2.1",
"symfony/translation": "^7.3" "symfony/translation": "^7.3",
"symfony/filesystem": "^7.3"
}, },
"suggest": { "suggest": {
"ext-event": "For better performance. " "ext-event": "For better performance. "
Generated
+67 -1
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "30cc3cbb162022972a21efed9b69d1e1", "content-hash": "6c03076d4c7c86514f6b2d0073545bea",
"packages": [ "packages": [
{ {
"name": "aliyuncs/oss-sdk-php", "name": "aliyuncs/oss-sdk-php",
@@ -4284,6 +4284,72 @@
], ],
"time": "2024-09-25T14:21:43+00:00" "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", "name": "symfony/finder",
"version": "v7.3.0", "version": "v7.3.0",
+4
View File
@@ -26,6 +26,10 @@ use Webman\Route;
// POST /admin/user/{user_id}/kickout_all // 一键踢全部 // 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('/common', function () {
Route::group('/auth', function () { Route::group('/auth', function () {