修改创建模块命令
This commit is contained in:
parent
fca8980715
commit
707070a06e
@ -1,13 +1,12 @@
|
||||
<?php
|
||||
namespace catcher\command;
|
||||
|
||||
use catcher\library\Compress;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use catcher\CatchAdmin;
|
||||
use think\helper\Str;
|
||||
|
||||
class CreateModuleCommand extends Command
|
||||
{
|
||||
@ -23,18 +22,16 @@ class CreateModuleCommand extends Command
|
||||
{
|
||||
$this->setName('create:module')
|
||||
->addArgument('module', Argument::REQUIRED, 'module name')
|
||||
->addOption('controller', '-c',Option::VALUE_REQUIRED, 'controller name')
|
||||
->addOption('migration', '-m',Option::VALUE_REQUIRED, 'migration name')
|
||||
->addOption('seed', '-s',Option::VALUE_REQUIRED, 'seed name')
|
||||
->addOption('service', '-e',Option::VALUE_REQUIRED, 'service name')
|
||||
->setDescription('create module service');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
try {
|
||||
$this->module = strtolower($input->getArgument('module'));
|
||||
|
||||
$this->moduleDir = CatchAdmin::moduleDirectory($this->module);
|
||||
|
||||
$this->stubDir = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR;
|
||||
|
||||
$composer = json_decode(file_get_contents($this->app->getRootPath() . 'composer.json'), true);
|
||||
@ -47,173 +44,126 @@ class CreateModuleCommand extends Command
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->createController();
|
||||
|
||||
$this->createFile();
|
||||
} catch (\Exception $exception) {
|
||||
$this->rollback();
|
||||
$output->error($exception->getMessage());
|
||||
exit;
|
||||
}
|
||||
|
||||
$output->info('module created');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败 rollback
|
||||
*
|
||||
* @time 2020年06月25日
|
||||
* @return void
|
||||
*/
|
||||
protected function rollback()
|
||||
{
|
||||
(new Compress())->rmDir($this->moduleDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模块文件夹
|
||||
*
|
||||
* @time 2020年06月25日
|
||||
* @return string[]
|
||||
*/
|
||||
protected function modulePath()
|
||||
{
|
||||
return [
|
||||
$this->moduleDir . 'controller',
|
||||
$this->moduleDir . 'model',
|
||||
$this->moduleDir . 'database' . DIRECTORY_SEPARATOR . 'migrations',
|
||||
$this->moduleDir . 'database' . DIRECTORY_SEPARATOR . 'seeds',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 模块文件
|
||||
*
|
||||
* @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',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建路径
|
||||
*
|
||||
* @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->createMigration();
|
||||
$this->createSeeds();
|
||||
$this->createRoute();
|
||||
|
||||
$output->warning('module created');
|
||||
$this->createModuleJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 service
|
||||
*
|
||||
* @time 2020年01月23日
|
||||
* @time 2020年06月25日
|
||||
* @return void
|
||||
*/
|
||||
protected function createController(): void
|
||||
protected function createService()
|
||||
{
|
||||
$controllers = $this->input->getOption('controller');
|
||||
$service = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'service.stub');
|
||||
|
||||
$controllerPath = $this->moduleDir . 'controller' . DIRECTORY_SEPARATOR;
|
||||
$content = str_replace(['{NAMESPACE}', '{SERVICE}'], [substr($this->namespaces, 0, -1), ucfirst($this->module) . 'Service'], $service);
|
||||
|
||||
if ($controllers) {
|
||||
CatchAdmin::makeDirectory($controllerPath);
|
||||
file_put_contents($this->moduleDir . ucfirst($this->module) . 'Service.php', $content);
|
||||
|
||||
foreach ($controllers as $controller) {
|
||||
file_put_contents($controllerPath . ucfirst($controller) . '.php', str_replace(
|
||||
['{CLASS}', '{NAMESPACE}', '{MODULE}'],
|
||||
[ucfirst($controller), $this->namespaces . 'controller', $this->module],
|
||||
file_get_contents($this->stubDir . 'controller.stub')
|
||||
));
|
||||
}
|
||||
|
||||
$this->output->info('🎉 create controller successfully');
|
||||
$this->createRequest($controllers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 module.json
|
||||
*
|
||||
* @time 2020年01月23日
|
||||
* @param $controllers
|
||||
* @time 2020年06月25日
|
||||
* @return void
|
||||
*/
|
||||
protected function createRequest($controllers): void
|
||||
protected function createModuleJson()
|
||||
{
|
||||
$requestPath = $this->moduleDir . 'request' . DIRECTORY_SEPARATOR;
|
||||
$moduleJson = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'module.stub');
|
||||
|
||||
CatchAdmin::makeDirectory($requestPath);
|
||||
$content = str_replace(['{MODULE}', '{SERVICE}'], [$this->module, '\\\\'. str_replace('\\', '\\\\',$this->namespaces . ucfirst($this->module) . 'Service')], $moduleJson);
|
||||
|
||||
$default = ['CreateRequest.php', 'UpdateRequest.php'];
|
||||
|
||||
if (count($controllers) === 1) {
|
||||
CatchAdmin::makeDirectory($requestPath . ucwords($controllers[0]));
|
||||
foreach ($default as $v) {
|
||||
file_put_contents($requestPath . $controllers[0] . DIRECTORY_SEPARATOR . $v, str_replace(
|
||||
['{NAMESPACE}', '{CLASS}'],
|
||||
[$this->namespaces . $controllers[0]. '\\request', 'Create'],
|
||||
file_get_contents($this->stubDir. 'request.stub')));
|
||||
}
|
||||
} else {
|
||||
foreach ($controllers as $controller) {
|
||||
CatchAdmin::makeDirectory($requestPath . ucwords($controller));
|
||||
foreach ($default as $v) {
|
||||
file_put_contents($requestPath . ucwords($controller). DIRECTORY_SEPARATOR . $v, str_replace(
|
||||
['{NAMESPACE}', '{CLASS}'],
|
||||
[$this->namespaces . 'request' . ucwords($controller), 'Create'],
|
||||
file_get_contents($this->stubDir . 'request.stub')));
|
||||
}
|
||||
}
|
||||
}
|
||||
file_put_contents($this->moduleDir . 'module.json', $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建路由文件
|
||||
*
|
||||
* @time 2020年01月23日
|
||||
* @time 2020年06月25日
|
||||
* @return void
|
||||
*/
|
||||
protected function createMigration(): void
|
||||
protected function createRoute()
|
||||
{
|
||||
$migrations = $this->input->getOption('migration');
|
||||
|
||||
$migrationPath = $this->moduleDir . 'database'.DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR;
|
||||
|
||||
if ($migrations) {
|
||||
CatchAdmin::makeDirectory($migrationPath);
|
||||
$migrations = explode(',', $migrations);
|
||||
|
||||
foreach ($migrations as $migration) {
|
||||
$filename = date('Ymdhis', time()) . '_' .Str::snake($migration) . '.php';
|
||||
|
||||
file_put_contents($migrationPath . $filename, str_replace(
|
||||
['{CLASS}'], [ucfirst($migration)], file_get_contents($this->stubDir.'migration.stub')));
|
||||
}
|
||||
$this->output->info('🎉 create migrations successfully');
|
||||
file_put_contents($this->moduleDir . 'route.php', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route.stub'));
|
||||
}
|
||||
}
|
||||
|
||||
protected function createSeeds()
|
||||
{
|
||||
$seeds = $this->input->getOption('seed');
|
||||
|
||||
$seedPath = $this->moduleDir . 'database'.DIRECTORY_SEPARATOR.'seeds'.DIRECTORY_SEPARATOR;
|
||||
|
||||
if ($seeds) {
|
||||
CatchAdmin::makeDirectory($seedPath);
|
||||
|
||||
$seeds = explode(',', $seeds);
|
||||
|
||||
foreach ($seeds as $seed) {
|
||||
$filename = ucfirst(Str::camel($seed)) . 'Seed.php';
|
||||
|
||||
file_put_contents($seedPath . $filename, str_replace(
|
||||
['{CLASS}'], [ucfirst($seed)], file_get_contents($this->stubDir.'seeder.stub')));
|
||||
}
|
||||
|
||||
$this->output->info('🎉 create seeds successfully');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年01月23日
|
||||
* @param $service
|
||||
* @return void
|
||||
*/
|
||||
protected function moduleJson($service): void
|
||||
{
|
||||
if (!file_exists($this->moduleDir.DIRECTORY_SEPARATOR .'module.json')) {
|
||||
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . 'module.json', str_replace(
|
||||
['{MODULE}', '{SERVICE}'],
|
||||
[$this->module, $service ? $this->namespaces . ucfirst($service) . 'Service' : ''],
|
||||
file_get_contents($this->stubDir . 'module.stub')));
|
||||
$this->output->info('🎉 create module.json successfully');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年01月23日
|
||||
* @return void
|
||||
*/
|
||||
protected function createRoute(): void
|
||||
{
|
||||
if (!file_exists($this->moduleDir.DIRECTORY_SEPARATOR .'route.php')) {
|
||||
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . 'route.php',
|
||||
file_get_contents($this->stubDir . 'route.stub'));
|
||||
$this->output->info('🎉 create route.php successfully');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年01月23日
|
||||
* @return void
|
||||
*/
|
||||
protected function createService(): void
|
||||
{
|
||||
$service = $this->input->getOption('service');
|
||||
if ($service) {
|
||||
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . ucfirst($this->module) . 'Service.php', str_replace(
|
||||
['{CLASS}', '{NAMESPACE}'],
|
||||
[ucfirst($service), $this->namespaces . '\\' . $this->module],
|
||||
file_get_contents($this->stubDir.'service.stub')));
|
||||
$this->output->info('🎉 create service successfully');
|
||||
}
|
||||
|
||||
$this->moduleJson($service);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -9,5 +9,6 @@
|
||||
],
|
||||
"aliases": {},
|
||||
"files": [],
|
||||
"requires": []
|
||||
"requires": [],
|
||||
"enable": true
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
// you should user `$router`
|
||||
// $router->get('index', 'controller@method');
|
||||
$router->group(function () use ($router){
|
||||
|
||||
});
|
||||
|
||||
|
@ -1,15 +1,13 @@
|
||||
<?php
|
||||
namespace {NAMESPACE};
|
||||
|
||||
use think\Service;
|
||||
use catcher\ModuleService;
|
||||
|
||||
class {CLASS}Service extends Service
|
||||
class {SERVICE} extends ModuleService
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
public function loadRouteFrom()
|
||||
{
|
||||
// TODO: Implement loadRouteFrom() method.
|
||||
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
|
||||
}
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ class Compress
|
||||
* @param $packageDir
|
||||
* @return void
|
||||
*/
|
||||
protected function rmDir($packageDir)
|
||||
public function rmDir($packageDir)
|
||||
{
|
||||
$fileSystemIterator = new \FilesystemIterator($packageDir);
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user