自动生成curd
This commit is contained in:
@@ -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 . "<info>文件生成成功!</info> <br/>";
|
||||
}
|
||||
@@ -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 "删除已存在文件: <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
|
||||
|
||||
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\model\Role;
|
||||
use app\{{modelModule}}\model\{{modelName}};
|
||||
|
||||
class RoleService
|
||||
class {{serviceName}}Service
|
||||
{
|
||||
|
||||
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
|
||||
|
||||
namespace app\{{module}}\validate;
|
||||
namespace app\{{validateModule}}\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class {{name}}Validator extends Validate
|
||||
class {{validateName}}Validator extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
Reference in New Issue
Block a user