修改创建模块命令

This commit is contained in:
JaguarJack 2020-06-25 09:25:02 +08:00
parent fca8980715
commit 707070a06e
5 changed files with 129 additions and 178 deletions

View File

@ -1,13 +1,12 @@
<?php <?php
namespace catcher\command; namespace catcher\command;
use catcher\library\Compress;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
use think\console\input\Argument; use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output; use think\console\Output;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use think\helper\Str;
class CreateModuleCommand extends Command class CreateModuleCommand extends Command
{ {
@ -23,197 +22,148 @@ class CreateModuleCommand extends Command
{ {
$this->setName('create:module') $this->setName('create:module')
->addArgument('module', Argument::REQUIRED, 'module name') ->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'); ->setDescription('create module service');
} }
protected function execute(Input $input, Output $output) protected function execute(Input $input, Output $output)
{ {
$this->module = strtolower($input->getArgument('module')); try {
$this->module = strtolower($input->getArgument('module'));
$this->moduleDir = CatchAdmin::moduleDirectory($this->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); $this->stubDir = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR;
$psr4 = $composer['autoload']['psr-4']; $composer = json_decode(file_get_contents($this->app->getRootPath() . 'composer.json'), true);
foreach ($psr4 as $namespace => $des) { $psr4 = $composer['autoload']['psr-4'];
if ($des === CatchAdmin::NAME) {
$this->namespaces = $namespace . $this->module . '\\';
break;
}
}
$this->createController();
$this->createService();
$this->createMigration();
$this->createSeeds();
$this->createRoute();
$output->warning('module created'); foreach ($psr4 as $namespace => $des) {
} if ($des === CatchAdmin::NAME) {
$this->namespaces = $namespace . $this->module . '\\';
/** break;
*
* @time 2020年01月23日
* @return void
*/
protected function createController(): void
{
$controllers = $this->input->getOption('controller');
$controllerPath = $this->moduleDir . 'controller' . DIRECTORY_SEPARATOR;
if ($controllers) {
CatchAdmin::makeDirectory($controllerPath);
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);
}
}
/**
*
* @time 2020年01月23日
* @param $controllers
* @return void
*/
protected function createRequest($controllers): void
{
$requestPath = $this->moduleDir . 'request' . DIRECTORY_SEPARATOR;
CatchAdmin::makeDirectory($requestPath);
$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')));
} }
} }
$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年01月23日 *
* @return void * @time 2020年06月25日
*/ * @return void
protected function createMigration(): void */
protected function createFile()
{ {
$migrations = $this->input->getOption('migration'); $this->createDir();
$this->createService();
$migrationPath = $this->moduleDir . 'database'.DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR; $this->createRoute();
$this->createModuleJson();
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');
}
} }
protected function createSeeds() /**
* 创建 service
*
* @time 2020年06月25日
* @return void
*/
protected function createService()
{ {
$seeds = $this->input->getOption('seed'); $service = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'service.stub');
$seedPath = $this->moduleDir . 'database'.DIRECTORY_SEPARATOR.'seeds'.DIRECTORY_SEPARATOR; $content = str_replace(['{NAMESPACE}', '{SERVICE}'], [substr($this->namespaces, 0, -1), ucfirst($this->module) . 'Service'], $service);
if ($seeds) { file_put_contents($this->moduleDir . ucfirst($this->module) . 'Service.php', $content);
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');
}
} }
/** /**
* * 创建 module.json
* @time 2020年01月23日 *
* @param $service * @time 2020年06月25日
* @return void * @return void
*/ */
protected function moduleJson($service): void protected function createModuleJson()
{ {
if (!file_exists($this->moduleDir.DIRECTORY_SEPARATOR .'module.json')) { $moduleJson = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'module.stub');
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . 'module.json', str_replace(
['{MODULE}', '{SERVICE}'], $content = str_replace(['{MODULE}', '{SERVICE}'], [$this->module, '\\\\'. str_replace('\\', '\\\\',$this->namespaces . ucfirst($this->module) . 'Service')], $moduleJson);
[$this->module, $service ? $this->namespaces . ucfirst($service) . 'Service' : ''],
file_get_contents($this->stubDir . 'module.stub'))); file_put_contents($this->moduleDir . 'module.json', $content);
$this->output->info('🎉 create module.json successfully');
}
} }
/** /**
* * 创建路由文件
* @time 2020年01月23日 *
* @return void * @time 2020年06月25日
*/ * @return void
protected function createRoute(): void */
protected function createRoute()
{ {
if (!file_exists($this->moduleDir.DIRECTORY_SEPARATOR .'route.php')) { file_put_contents($this->moduleDir . 'route.php', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route.stub'));
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);
}
} }

View File

@ -9,5 +9,6 @@
], ],
"aliases": {}, "aliases": {},
"files": [], "files": [],
"requires": [] "requires": [],
"enable": true
} }

View File

@ -1,5 +1,7 @@
<?php <?php
// you should user `$router` // you should user `$router`
// $router->get('index', 'controller@method'); $router->group(function () use ($router){
});

View File

@ -1,15 +1,13 @@
<?php <?php
namespace {NAMESPACE}; namespace {NAMESPACE};
use think\Service; use catcher\ModuleService;
class {CLASS}Service extends Service class {SERVICE} extends ModuleService
{ {
/** public function loadRouteFrom()
* {
* @return void // TODO: Implement loadRouteFrom() method.
*/ return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
public function boot() }
{
}
} }

View File

@ -150,7 +150,7 @@ class Compress
* @param $packageDir * @param $packageDir
* @return void * @return void
*/ */
protected function rmDir($packageDir) public function rmDir($packageDir)
{ {
$fileSystemIterator = new \FilesystemIterator($packageDir); $fileSystemIterator = new \FilesystemIterator($packageDir);
try { try {