自动生成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
{
// 表名
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;
}
}
/**