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
|
|
|
|
|
|
|
use think\console\Command;
|
|
|
|
use think\console\Input;
|
|
|
|
use think\console\input\Argument;
|
2019-12-19 07:19:15 +08:00
|
|
|
use think\console\input\Option;
|
2019-12-18 18:54:01 +08:00
|
|
|
use think\console\Output;
|
2019-12-19 07:19:15 +08:00
|
|
|
use catcher\CatchAdmin;
|
|
|
|
use think\helper\Str;
|
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;
|
|
|
|
|
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')
|
2019-12-19 07:19:15 +08:00
|
|
|
->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', '-se',Option::VALUE_REQUIRED, 'service name')
|
2019-12-18 18:54:01 +08:00
|
|
|
->setDescription('create module service');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
|
|
{
|
|
|
|
$this->module = strtolower($input->getArgument('module'));
|
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
|
2019-12-18 18:54:01 +08:00
|
|
|
$this->moduleDir = CatchAdmin::moduleDirectory($this->module);
|
2019-12-19 07:19:15 +08:00
|
|
|
$this->stubDir = __DIR__ . DIRECTORY_SEPARATOR .'stubs'. DIRECTORY_SEPARATOR;
|
2019-12-18 18:54:01 +08:00
|
|
|
|
2020-01-23 17:16:02 +08:00
|
|
|
$composer = json_decode(file_get_contents($this->app->getRootPath() . 'composer.json'), true);
|
2019-12-18 18:54:01 +08:00
|
|
|
|
2020-01-23 17:16:02 +08:00
|
|
|
$psr4 = $composer['autoload']['psr-4'];
|
|
|
|
|
|
|
|
foreach ($psr4 as $namespace => $des) {
|
|
|
|
if ($des === CatchAdmin::NAME) {
|
|
|
|
$this->namespaces = $namespace . $this->module . '\\';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 18:54:01 +08:00
|
|
|
$this->createController();
|
2019-12-19 07:19:15 +08:00
|
|
|
$this->createService();
|
|
|
|
$this->createMigration();
|
|
|
|
$this->createSeeds();
|
2019-12-18 18:54:01 +08:00
|
|
|
$this->createRoute();
|
|
|
|
$this->moduleJson();
|
|
|
|
|
|
|
|
$output->warning('module created');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function createController()
|
|
|
|
{
|
2019-12-19 07:19:15 +08:00
|
|
|
$controllers = $this->input->getOption('controller');
|
2019-12-18 18:54:01 +08:00
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
$controllerPath = $this->moduleDir . 'controller' . DIRECTORY_SEPARATOR;
|
2019-12-18 18:54:01 +08:00
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
CatchAdmin::makeDirectory($controllerPath);
|
|
|
|
|
|
|
|
$controllers = $controllers ? explode(',', $controllers) : ['Index'];
|
2019-12-18 18:54:01 +08:00
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
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->createView($controllers);
|
|
|
|
$this->createRequest($controllers);
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
protected function createView($controllers)
|
2019-12-18 18:54:01 +08:00
|
|
|
{
|
2019-12-19 07:19:15 +08:00
|
|
|
$viewPath = $this->moduleDir . 'view' . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
CatchAdmin::makeDirectory($viewPath);
|
|
|
|
|
|
|
|
$default = ['index.', 'create.', 'edit.'];
|
|
|
|
|
|
|
|
$viewSuffix = config('view.view_suffix');
|
|
|
|
|
|
|
|
if (count($controllers) === 1) {
|
|
|
|
foreach ($default as $v) {
|
|
|
|
file_put_contents($viewPath . $v .$viewSuffix, '');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($controllers as $controller) {
|
|
|
|
CatchAdmin::makeDirectory($viewPath . ucwords($controller));
|
|
|
|
foreach ($default as $v) {
|
|
|
|
file_put_contents($viewPath . ucwords($controller) . DIRECTORY_SEPARATOR .$v . $viewSuffix, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 18:54:01 +08:00
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
$this->output->info('🎉 create view successfully');
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
protected function createRequest($controllers)
|
2019-12-18 18:54:01 +08:00
|
|
|
{
|
2019-12-19 07:19:15 +08:00
|
|
|
$requestPath = $this->moduleDir . DIRECTORY_SEPARATOR . 'request' . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
CatchAdmin::makeDirectory($requestPath);
|
|
|
|
|
|
|
|
$default = ['CreateRequest.php', 'UpdateRequest.php'];
|
|
|
|
|
|
|
|
if (count($controllers) === 1) {
|
|
|
|
foreach ($default as $v) {
|
|
|
|
file_put_contents($requestPath . $v, str_replace(
|
|
|
|
['{NAMESPACE}', '{CLASS}'],
|
|
|
|
[$this->namespaces . '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->output->info('🎉 create view successfully');
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
protected function createMigration()
|
2019-12-18 18:54:01 +08:00
|
|
|
{
|
2019-12-19 07:19:15 +08:00
|
|
|
$migrations = $this->input->getOption('migration');
|
|
|
|
|
|
|
|
$migrationPath = $this->moduleDir . 'database'.DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
CatchAdmin::makeDirectory($migrationPath);
|
|
|
|
|
|
|
|
if ($migrations) {
|
|
|
|
$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');
|
|
|
|
}
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 07:19:15 +08:00
|
|
|
protected function createSeeds()
|
2019-12-18 18:54:01 +08:00
|
|
|
{
|
2019-12-19 07:19:15 +08:00
|
|
|
$seeds = $this->input->getOption('seed');
|
|
|
|
|
|
|
|
$seedPath = $this->moduleDir . 'database'.DIRECTORY_SEPARATOR.'seeds'.DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
CatchAdmin::makeDirectory($seedPath);
|
|
|
|
|
|
|
|
if ($seeds) {
|
|
|
|
$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');
|
|
|
|
}
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function moduleJson()
|
|
|
|
{
|
|
|
|
file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR .'module.json', str_replace(
|
|
|
|
['{MODULE}', '{SERVICE}'],
|
|
|
|
[$this->module, $this->namespaces. ucfirst($this->module) . 'Service'],
|
2019-12-19 07:19:15 +08:00
|
|
|
file_get_contents($this->stubDir . 'module.stub')));
|
|
|
|
$this->output->info('🎉 create module.json successfully');
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createRoute()
|
|
|
|
{
|
|
|
|
file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR .'route.php',
|
2019-12-19 07:19:15 +08:00
|
|
|
file_get_contents($this->stubDir . 'route.stub'));
|
|
|
|
$this->output->info('🎉 create route.php successfully');
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createService()
|
|
|
|
{
|
2019-12-19 07:19:15 +08:00
|
|
|
$service = $this->input->getOption('service');
|
|
|
|
if ($service) {
|
|
|
|
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . ucfirst($this->module) . 'Service.php', str_replace(
|
|
|
|
['{CLASS}', '{NAMESPACE}'],
|
|
|
|
[ucfirst($this->module), $this->namespaces . '\\' . $this->module],
|
|
|
|
file_get_contents($this->stubDir.'service.stub')));
|
|
|
|
$this->output->info('🎉 create service successfully');
|
|
|
|
}
|
2019-12-18 18:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|