add:新增创建模块
This commit is contained in:
parent
c8e9e6361c
commit
e4a5ae0c37
@ -3,6 +3,7 @@ namespace catchAdmin\system\controller;
|
|||||||
|
|
||||||
use catcher\base\CatchController;
|
use catcher\base\CatchController;
|
||||||
use catcher\CatchResponse;
|
use catcher\CatchResponse;
|
||||||
|
use catcher\generate\CreateModule;
|
||||||
use catcher\generate\Generator;
|
use catcher\generate\Generator;
|
||||||
use think\Request;
|
use think\Request;
|
||||||
|
|
||||||
@ -25,4 +26,10 @@ class Generate extends CatchController
|
|||||||
{
|
{
|
||||||
return CatchResponse::success($generator->preview($request->param()));
|
return CatchResponse::success($generator->preview($request->param()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function createModule(Request $request, CreateModule $module)
|
||||||
|
{
|
||||||
|
return CatchResponse::success($module->generate($request->post()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ $router->group(function () use ($router) {
|
|||||||
// 代码生成
|
// 代码生成
|
||||||
$router->post('generate', '\catchAdmin\system\controller\Generate@save');
|
$router->post('generate', '\catchAdmin\system\controller\Generate@save');
|
||||||
$router->post('generate/preview', '\catchAdmin\system\controller\Generate@preview'); // 预览
|
$router->post('generate/preview', '\catchAdmin\system\controller\Generate@preview'); // 预览
|
||||||
|
$router->post('generate/create/module', '\catchAdmin\system\controller\Generate@createModule'); // 创建模块
|
||||||
|
|
||||||
// 敏感词
|
// 敏感词
|
||||||
$router->resource('sensitive/word', '\catchAdmin\system\controller\SensitiveWord');
|
$router->resource('sensitive/word', '\catchAdmin\system\controller\SensitiveWord');
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"alias": "{MODULE}",
|
"alias": "{MODULE}",
|
||||||
"description": "{DESCRIPTION}",
|
"description": "{DESCRIPTION}",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"keywords": [],
|
"keywords": [{KEYWORDS}],
|
||||||
"order": 0,
|
"order": 0,
|
||||||
"services": [
|
"services": [
|
||||||
"{SERVICE}"
|
"{SERVICE}"
|
||||||
|
205
extend/catcher/generate/CreateModule.php
Normal file
205
extend/catcher/generate/CreateModule.php
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
<?php
|
||||||
|
namespace catcher\generate;
|
||||||
|
|
||||||
|
|
||||||
|
use catcher\CatchAdmin;
|
||||||
|
use catcher\facade\FileSystem;
|
||||||
|
use catcher\library\Composer;
|
||||||
|
|
||||||
|
class CreateModule
|
||||||
|
{
|
||||||
|
protected $module;
|
||||||
|
|
||||||
|
protected $moduleDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $stubDir;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $namespaces;
|
||||||
|
|
||||||
|
protected $name;
|
||||||
|
|
||||||
|
protected $description;
|
||||||
|
|
||||||
|
protected $dirs;
|
||||||
|
|
||||||
|
protected $keywords;
|
||||||
|
|
||||||
|
public function generate($params)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->module = $params['alias'];
|
||||||
|
|
||||||
|
$this->name = $params['name'];
|
||||||
|
|
||||||
|
$this->description = $params['description'] ?? '';
|
||||||
|
|
||||||
|
$this->keywords = $params['keywords'] ?? '';
|
||||||
|
|
||||||
|
$this->dirs = $params['dirs'];
|
||||||
|
|
||||||
|
$this->init();
|
||||||
|
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
$this->rollback();
|
||||||
|
dd($exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$this->moduleDir = CatchAdmin::moduleDirectory($this->module);
|
||||||
|
|
||||||
|
$this->stubDir =dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
||||||
|
'command'.DIRECTORY_SEPARATOR.
|
||||||
|
'stubs' . DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
|
$psr4 = (new Composer())->psr4Autoload();
|
||||||
|
|
||||||
|
foreach ($psr4 as $namespace => $des) {
|
||||||
|
if ($des === CatchAdmin::$root) {
|
||||||
|
$this->namespaces = $namespace . $this->module . '\\';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->createFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模块文件
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
protected function moduleFiles()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
$this->moduleDir . ucfirst($this->module). 'Service.php',
|
||||||
|
$this->moduleDir . 'module.json',
|
||||||
|
$this->moduleDir . 'route.php',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建失败 rollback
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function rollback()
|
||||||
|
{
|
||||||
|
FileSystem::deleteDirectory($this->moduleDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模块文件夹
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
protected function modulePath()
|
||||||
|
{
|
||||||
|
|
||||||
|
$dirs = [];
|
||||||
|
foreach (explode(',', $this->dirs) as $dir) {
|
||||||
|
if ($dir == 'database') {
|
||||||
|
$dirs[] = $this->moduleDir . 'database' . DIRECTORY_SEPARATOR . 'migrations';
|
||||||
|
$dirs[] = $this->moduleDir . 'database' . DIRECTORY_SEPARATOR . 'seeds';
|
||||||
|
} else {
|
||||||
|
$dirs[] = $this->moduleDir . $dir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $dirs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建路径
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createDir()
|
||||||
|
{
|
||||||
|
foreach ($this->modulePath() as $path)
|
||||||
|
{
|
||||||
|
CatchAdmin::makeDirectory($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建文件
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createFile()
|
||||||
|
{
|
||||||
|
$this->createDir();
|
||||||
|
$this->createService();
|
||||||
|
$this->createRoute();
|
||||||
|
$this->createModuleJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 service
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createService()
|
||||||
|
{
|
||||||
|
$service = FileSystem::sharedGet($this->stubDir . 'service.stub');
|
||||||
|
|
||||||
|
$content = str_replace(['{NAMESPACE}', '{SERVICE}'],
|
||||||
|
[substr($this->namespaces, 0, -1),
|
||||||
|
ucfirst($this->module) . 'Service'], $service);
|
||||||
|
|
||||||
|
FileSystem::put($this->moduleDir . ucfirst($this->module) . 'Service.php', $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 module.json
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createModuleJson()
|
||||||
|
{
|
||||||
|
$moduleJson = FileSystem::sharedGet( $this->stubDir . 'module.stub');
|
||||||
|
|
||||||
|
$keywords = '';
|
||||||
|
foreach (explode(',',$this->keywords) as $k) {
|
||||||
|
$keywords .= "\"{$k}\",";
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = str_replace(['{NAME}','{DESCRIPTION}','{MODULE}', '{KEYWORDS}','{SERVICE}'],
|
||||||
|
[
|
||||||
|
$this->name,
|
||||||
|
$this->description,
|
||||||
|
$this->module,
|
||||||
|
trim($keywords, ','),
|
||||||
|
'\\\\'. str_replace('\\', '\\\\',$this->namespaces . ucfirst($this->module) . 'Service')
|
||||||
|
], $moduleJson);
|
||||||
|
|
||||||
|
FileSystem::put($this->moduleDir . 'module.json', $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建路由文件
|
||||||
|
*
|
||||||
|
* @time 2020年06月25日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function createRoute()
|
||||||
|
{
|
||||||
|
FileSystem::put($this->moduleDir . 'route.php', FileSystem::sharedGet($this->stubDir . 'route.stub'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user