From 02a16ee48fb9793c3c11cc80f271dc67e51212f8 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 19 Oct 2017 11:53:01 +0800 Subject: [PATCH] Add model generator class --- src/CrudMake.php | 29 ++--------------------------- src/Generators/BaseGenerator.php | 7 +++++++ src/Generators/ControllerGenerator.php | 8 +++----- src/Generators/ModelGenerator.php | 31 +++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 src/Generators/ModelGenerator.php diff --git a/src/CrudMake.php b/src/CrudMake.php index 156543f..cb6b24d 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -5,6 +5,7 @@ namespace Luthfi\CrudGenerator; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Luthfi\CrudGenerator\Generators\ControllerGenerator; +use Luthfi\CrudGenerator\Generators\ModelGenerator; class CrudMake extends Command { @@ -77,7 +78,7 @@ class CrudMake extends Command if (! $this->modelExists()) { $this->generateResourceRoute(); - $this->generateModel(); + app(ModelGenerator::class, ['command' => $this])->generate(); $this->generateMigration(); app(ControllerGenerator::class, ['command' => $this])->generate(); $this->generateViews(); @@ -142,21 +143,6 @@ class CrudMake extends Command } /** - * Generate the model file - * - * @return void - */ - public function generateModel() - { - $modelPath = $this->modelNames['model_path']; - $modelDirectory = $this->makeDirectory(app_path($modelPath)); - - $this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getModelContent()); - - $this->info($this->modelNames['model_name'].' model generated.'); - } - - /** * Generate migration file for the model * * @return void @@ -295,17 +281,6 @@ class CrudMake extends Command } /** - * Get model content from model stub - * - * @return string Replaced proper model names in model file content - */ - public function getModelContent() - { - $stub = $this->files->get(__DIR__.'/stubs/model.stub'); - return $this->replaceStubString($stub); - } - - /** * Get migration file content from migration stub * * @return string Replaced proper model names in migration file content diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php index 6c32c46..8d864c0 100644 --- a/src/Generators/BaseGenerator.php +++ b/src/Generators/BaseGenerator.php @@ -101,6 +101,13 @@ abstract class BaseGenerator abstract public function generate(); /** + * Get class file content + * + * @return void + */ + abstract protected function getContent(); + + /** * Make directory if the path is not exists * @param string $path Absolute path of targetted directory * @return string Absolute path diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index ca3434f..819ca01 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -19,17 +19,15 @@ class ControllerGenerator extends BaseGenerator $controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory)); $controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php'; - $this->generateFile($controllerPath, $this->getControllerContent()); + $this->generateFile($controllerPath, $this->getContent()); $this->command->info($this->modelNames['plural_model_name'].'Controller generated.'); } /** - * Get controller content from controller stub - * - * @return string Replaced proper model names in controller file content + * {@inheritDoc} */ - public function getControllerContent() + public function getContent() { $stub = $this->files->get(__DIR__.'/../stubs/controller.model.stub'); diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php new file mode 100644 index 0000000..b5c1138 --- /dev/null +++ b/src/Generators/ModelGenerator.php @@ -0,0 +1,31 @@ +modelNames['model_path']; + $modelDirectory = $this->makeDirectory(app_path($modelPath)); + + $this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getContent()); + + $this->command->info($this->modelNames['model_name'].' model generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/model.stub'); + return $this->replaceStubString($stub); + } +} \ No newline at end of file