catchAdmin/extend/catcher/command/CreateModuleCommand.php

199 lines
5.0 KiB
PHP
Raw Normal View History

2019-12-18 18:54:01 +08:00
<?php
2019-12-19 07:19:15 +08:00
namespace catcher\command;
2019-12-18 18:54:01 +08:00
2020-06-25 09:25:02 +08:00
use catcher\library\Compress;
2019-12-18 18:54:01 +08:00
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
2019-12-19 07:19:15 +08:00
use catcher\CatchAdmin;
2019-12-18 18:54:01 +08:00
class CreateModuleCommand extends Command
{
protected $module;
protected $moduleDir;
2019-12-19 07:19:15 +08:00
protected $stubDir;
2020-07-03 21:24:42 +08:00
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $description;
2019-12-18 18:54:01 +08:00
protected $namespaces;
protected function configure()
{
2019-12-19 07:19:15 +08:00
$this->setName('create:module')
2019-12-18 18:54:01 +08:00
->addArgument('module', Argument::REQUIRED, 'module name')
->setDescription('create module service');
}
protected function execute(Input $input, Output $output)
{
2020-06-25 09:25:02 +08:00
try {
$this->module = strtolower($input->getArgument('module'));
2019-12-18 18:54:01 +08:00
2020-07-03 21:24:42 +08:00
$this->name = $output->ask($input, '请输入模块中文名称');
if (!$this->name) {
while (true) {
$this->name = $output->ask($input, '请输入模块中文名称');
if ($this->name) {
break;
}
}
}
$this->description = $output->ask($input, '请输入模块描述');
$this->description = $this->description ? : '';
2020-06-25 09:25:02 +08:00
$this->moduleDir = CatchAdmin::moduleDirectory($this->module);
2019-12-18 18:54:01 +08:00
2020-06-25 09:25:02 +08:00
$this->stubDir = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR;
2019-12-18 18:54:01 +08:00
2020-06-25 09:25:02 +08:00
$composer = json_decode(file_get_contents($this->app->getRootPath() . 'composer.json'), true);
2020-01-23 17:16:02 +08:00
2020-06-25 09:25:02 +08:00
$psr4 = $composer['autoload']['psr-4'];
foreach ($psr4 as $namespace => $des) {
if ($des === CatchAdmin::NAME) {
$this->namespaces = $namespace . $this->module . '\\';
break;
}
2020-01-23 17:16:02 +08:00
}
2020-06-25 09:25:02 +08:00
$this->createFile();
} catch (\Exception $exception) {
$this->rollback();
$output->error($exception->getMessage());
exit;
2020-01-23 17:16:02 +08:00
}
2019-12-18 18:54:01 +08:00
2020-06-25 09:25:02 +08:00
$output->info('module created');
2019-12-18 18:54:01 +08:00
}
2020-06-25 09:25:02 +08:00
/**
* 创建失败 rollback
*
* @time 2020年06月25日
* @return void
*/
protected function rollback()
2019-12-18 18:54:01 +08:00
{
2020-06-25 09:25:02 +08:00
(new Compress())->rmDir($this->moduleDir);
2019-12-18 18:54:01 +08:00
}
2020-06-25 09:25:02 +08:00
/**
* 模块文件夹
*
* @time 2020年06月25日
* @return string[]
*/
protected function modulePath()
2019-12-18 18:54:01 +08:00
{
2020-06-25 09:25:02 +08:00
return [
$this->moduleDir . 'controller',
$this->moduleDir . 'model',
$this->moduleDir . 'database' . DIRECTORY_SEPARATOR . 'migrations',
$this->moduleDir . 'database' . DIRECTORY_SEPARATOR . 'seeds',
];
2019-12-18 18:54:01 +08:00
}
2020-06-25 09:25:02 +08:00
/**
* 模块文件
*
* @time 2020年06月25日
* @return string[]
*/
protected function moduleFiles()
2019-12-18 18:54:01 +08:00
{
2020-06-25 09:25:02 +08:00
return [
$this->moduleDir . ucfirst($this->module). 'Service.php',
$this->moduleDir . 'module.json',
$this->moduleDir . 'route.php',
];
}
2019-12-19 07:19:15 +08:00
2020-06-25 09:25:02 +08:00
/**
* 创建路径
*
* @time 2020年06月25日
* @return void
*/
protected function createDir()
{
foreach ($this->modulePath() as $path)
{
CatchAdmin::makeDirectory($path);
2019-12-19 07:19:15 +08:00
}
2019-12-18 18:54:01 +08:00
}
2020-06-25 09:25:02 +08:00
/**
* 创建文件
*
* @time 2020年06月25日
* @return void
*/
protected function createFile()
2019-12-18 18:54:01 +08:00
{
2020-06-25 09:25:02 +08:00
$this->createDir();
$this->createService();
$this->createRoute();
$this->createModuleJson();
}
2019-12-19 07:19:15 +08:00
2020-06-25 09:25:02 +08:00
/**
* 创建 service
*
* @time 2020年06月25日
* @return void
*/
protected function createService()
{
$service = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'service.stub');
2020-01-23 18:21:08 +08:00
2020-07-03 21:24:42 +08:00
$content = str_replace(['{NAMESPACE}', '{SERVICE}'],
[substr($this->namespaces, 0, -1),
ucfirst($this->module) . 'Service'], $service);
2019-12-19 07:19:15 +08:00
2020-06-25 09:25:02 +08:00
file_put_contents($this->moduleDir . ucfirst($this->module) . 'Service.php', $content);
2019-12-19 07:19:15 +08:00
2019-12-18 18:54:01 +08:00
}
2020-06-25 09:25:02 +08:00
/**
* 创建 module.json
*
* @time 2020年06月25日
* @return void
*/
protected function createModuleJson()
2019-12-18 18:54:01 +08:00
{
2020-06-25 09:25:02 +08:00
$moduleJson = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'module.stub');
2019-12-18 18:54:01 +08:00
2020-07-03 21:24:42 +08:00
$content = str_replace(['{NAME}','{DESCRIPTION}','{MODULE}', '{SERVICE}'],
[$this->name, $this->description,
$this->module, '\\\\'. str_replace('\\', '\\\\',$this->namespaces . ucfirst($this->module) . 'Service')], $moduleJson);
2020-06-25 09:25:02 +08:00
file_put_contents($this->moduleDir . 'module.json', $content);
2019-12-18 18:54:01 +08:00
}
2020-06-25 09:25:02 +08:00
/**
* 创建路由文件
*
* @time 2020年06月25日
* @return void
*/
protected function createRoute()
2019-12-18 18:54:01 +08:00
{
2020-06-25 09:25:02 +08:00
file_put_contents($this->moduleDir . 'route.php', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route.stub'));
2019-12-18 18:54:01 +08:00
}
}