新增模块创建命令

This commit is contained in:
wuyanwen 2019-12-19 07:19:15 +08:00
parent 7e304cacf0
commit 122b624580
3 changed files with 149 additions and 77 deletions

View File

@ -1,11 +1,13 @@
<?php <?php
namespace jaguarjack\think\module\command; namespace catcher\command;
use catcher\CatchAdmin;
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 think\helper\Str;
class CreateModuleCommand extends Command class CreateModuleCommand extends Command
{ {
@ -13,12 +15,18 @@ class CreateModuleCommand extends Command
protected $moduleDir; protected $moduleDir;
protected $stubDir;
protected $namespaces; protected $namespaces;
protected function configure() protected function configure()
{ {
$this->setName('module:create') $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', '-se',Option::VALUE_REQUIRED, 'service name')
->setDescription('create module service'); ->setDescription('create module service');
} }
@ -26,16 +34,16 @@ class CreateModuleCommand extends Command
{ {
$this->module = strtolower($input->getArgument('module')); $this->module = strtolower($input->getArgument('module'));
$this->moduleDir = CatchAdmin::moduleDirectory($this->module);
$this->namespaces = CatchAdmin::NAME . '\\\\' . $this->module . '\\\\'; $this->moduleDir = CatchAdmin::moduleDirectory($this->module);
$this->stubDir = __DIR__ . DIRECTORY_SEPARATOR .'stubs'. DIRECTORY_SEPARATOR;
$this->namespaces = CatchAdmin::NAME . '\\' . $this->module . '\\';
$this->createController(); $this->createController();
$this->createRequest(); $this->createService();
$this->createModel(); $this->createMigration();
// $this->createService(); $this->createSeeds();
$this->createView();
$this->createValidate();
$this->createRoute(); $this->createRoute();
$this->moduleJson(); $this->moduleJson();
@ -45,64 +53,125 @@ class CreateModuleCommand extends Command
protected function createController() protected function createController()
{ {
mkdir($this->moduleDir . 'controller' . DIRECTORY_SEPARATOR); $controllers = $this->input->getOption('controller');
return file_put_contents($this->moduleDir . 'controller' . DIRECTORY_SEPARATOR . 'Index.php', str_replace(
['{CLASS}', '{NAMESPACE}', '{MODULE}'], $controllerPath = $this->moduleDir . 'controller' . DIRECTORY_SEPARATOR;
['Index', $this->namespaces . 'controller', $this->module],
file_get_contents(__DIR__ . DIRECTORY_SEPARATOR .'stubs'.DIRECTORY_SEPARATOR. 'controller.stub') CatchAdmin::makeDirectory($controllerPath);
));
$controllers = $controllers ? explode(',', $controllers) : ['Index'];
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);
} }
protected function createModel()
protected function createView($controllers)
{ {
$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, '');
}
}
}
$this->output->info('🎉 create view successfully');
} }
protected function createView() protected function createRequest($controllers)
{
mkdir($this->moduleDir . DIRECTORY_SEPARATOR . 'view');
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'index.html', '');
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'create.html', '');
file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'edit.html', '');
}
protected function createValidate()
{
$validatePath = $this->moduleDir . DIRECTORY_SEPARATOR . 'validate' . DIRECTORY_SEPARATOR;
mkdir($validatePath);
file_put_contents($validatePath . 'CreateValidate.php', str_replace(
['{Namespace}', '{Class}'],
[$this->namespaces . 'validate', 'Create'],
file_get_contents(__DIR__ . 'stubs' . DIRECTORY_SEPARATOR . 'validate.stub')));
file_put_contents($validatePath . 'UpdateValidate.php', str_replace(
['{Namespace}', '{Class}'],
[$this->namespaces . 'validate', 'Update'],
file_get_contents(__DIR__ . 'stubs' . DIRECTORY_SEPARATOR . 'validate.stub')));
}
protected function createRequest()
{ {
$requestPath = $this->moduleDir . DIRECTORY_SEPARATOR . 'request' . DIRECTORY_SEPARATOR; $requestPath = $this->moduleDir . DIRECTORY_SEPARATOR . 'request' . DIRECTORY_SEPARATOR;
mkdir($requestPath);
file_put_contents($validatePath . 'CreateRequest.php', str_replace(
['{Namespace}', '{Class}'],
[$this->namespaces . 'request', 'Create'],
file_get_contents(__DIR__ . 'stubs' . DIRECTORY_SEPARATOR . 'request.stub')));
file_put_contents($validatePath . 'UpdateRequest.php', str_replace( CatchAdmin::makeDirectory($requestPath);
['{Namespace}', '{Class}'],
[$this->namespaces . 'request', 'Update'], $default = ['CreateRequest.php', 'UpdateRequest.php'];
file_get_contents(__DIR__ . 'stubs' . DIRECTORY_SEPARATOR . 'request.stub')));
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');
} }
protected function database() protected function createMigration()
{ {
mkdir($this->moduleDir . DIRECTORY_SEPARATOR . 'database'); $migrations = $this->input->getOption('migration');
mkdir($this->moduleDir . DIRECTORY_SEPARATOR . 'database'.DIRECTORY_SEPARATOR.'migrations');
mkdir($this->moduleDir . DIRECTORY_SEPARATOR . 'database'.DIRECTORY_SEPARATOR . 'seeds'); $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');
}
}
protected function createSeeds()
{
$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');
}
} }
protected function moduleJson() protected function moduleJson()
@ -110,21 +179,27 @@ class CreateModuleCommand extends Command
file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR .'module.json', str_replace( file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR .'module.json', str_replace(
['{MODULE}', '{SERVICE}'], ['{MODULE}', '{SERVICE}'],
[$this->module, $this->namespaces. ucfirst($this->module) . 'Service'], [$this->module, $this->namespaces. ucfirst($this->module) . 'Service'],
file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'module.stub'))); file_get_contents($this->stubDir . 'module.stub')));
$this->output->info('🎉 create module.json successfully');
} }
protected function createRoute() protected function createRoute()
{ {
file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR .'route.php', file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR .'route.php',
file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route.stub')); file_get_contents($this->stubDir . 'route.stub'));
$this->output->info('🎉 create route.php successfully');
} }
protected function createService() protected function createService()
{ {
file_put_contents($this->moduleDir.DIRECTORY_SEPARATOR . ucfirst($this->module) . 'Service.php', str_replace( $service = $this->input->getOption('service');
['{CLASS}', '{NAMESPACE}'], if ($service) {
[ucfirst($this->module), $this->namespaces . '\\' . $this->module], file_put_contents($this->moduleDir . DIRECTORY_SEPARATOR . ucfirst($this->module) . 'Service.php', str_replace(
file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'provider.stub'))); ['{CLASS}', '{NAMESPACE}'],
[ucfirst($this->module), $this->namespaces . '\\' . $this->module],
file_get_contents($this->stubDir.'service.stub')));
$this->output->info('🎉 create service successfully');
}
} }

View File

@ -1,18 +1,18 @@
<?php <?php
namespace {NAMESPACE}; namespace {NAMESPACE};
use app\BaseController; use catcher\base\CatchController;
class {CLASS} extends BaseController class {CLASS} extends CatchController
{ {
public function index() public function index()
{ {
return $this->fetch('{MODULE}::index'); return $this->fetch();
} }
public function create() public function create()
{ {
return $this->fetch('{MODULE}::create'); return $this->fetch();
} }
public function save() public function save()
@ -23,7 +23,7 @@ class {CLASS} extends BaseController
public function edit() public function edit()
{ {
return $this->fetch('{MODULE}::edit'); return $this->fetch();
} }
public function update() public function update()

View File

@ -1,17 +1,14 @@
<?php <?php
namespace {NAMESPACE}; namespace {NAMESPACE};
use catcher\base\BaseRequest; use catcher\base\CatchRequest;
class {CLASS}Request extends BaseRequest class {CLASS}Request extends CatchRequest
{ {
/** public function rule()
* {
* @time 2019年11月27日 return [
* @return string // rule
*/ ];
protected function getValidate() }
{
// TODO: Implement getValidate() method.
}
} }