From 98be84bb9321ba7f4068dfe617eb96d406f9e771 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 19 Oct 2017 08:43:16 +0800 Subject: [PATCH] Add controller generator class --- src/CrudMake.php | 55 +---------------- src/Generators/BaseGenerator.php | 105 +++++++++++++++++++++++++++++++++ src/Generators/ControllerGenerator.php | 59 ++++++++++++++++++ 3 files changed, 166 insertions(+), 53 deletions(-) create mode 100644 src/Generators/BaseGenerator.php create mode 100644 src/Generators/ControllerGenerator.php diff --git a/src/CrudMake.php b/src/CrudMake.php index 69880f6..c398f30 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -4,6 +4,7 @@ namespace Luthfi\CrudGenerator; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; +use Luthfi\CrudGenerator\Generators\ControllerGenerator; class CrudMake extends Command { @@ -78,7 +79,7 @@ class CrudMake extends Command $this->generateModel(); $this->generateMigration(); - $this->generateController(); + app(ControllerGenerator::class, ['modelNames' => $this->modelNames, 'command' => $this])->generate(); $this->generateViews(); $this->generateLangFile(); $this->generateModelFactory(); @@ -156,25 +157,6 @@ class CrudMake extends Command } /** - * Generate controller for model CRUD operation - * - * @return void - */ - public function generateController() - { - $parentControllerDirectory = ''; - if (! is_null($this->option('parent'))) { - $parentControllerDirectory = '/'.$this->option('parent'); - } - $controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory)); - - $controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php'; - $this->generateFile($controllerPath, $this->getControllerContent()); - - $this->info($this->modelNames['plural_model_name'].'Controller generated.'); - } - - /** * Generate migration file for the model * * @return void @@ -313,39 +295,6 @@ class CrudMake extends Command } /** - * Get controller content from controller stub - * - * @return string Replaced proper model names in controller file content - */ - public function getControllerContent() - { - $stub = $this->files->get(__DIR__.'/stubs/controller.model.stub'); - - $controllerFileContent = $this->replaceStubString($stub); - - if (! is_null($parentName = $this->option('parent'))) { - - $searches = [ - 'App\Http\Controllers;', - "use {$this->modelNames['full_model_name']};\n" - ]; - - $replacements = [ - "App\Http\Controllers\\{$parentName};", - "use {$this->modelNames['full_model_name']};\nuse App\Http\Controllers\Controller;\n" - ]; - - $controllerFileContent = str_replace( - $searches, - $replacements, - $controllerFileContent - ); - } - - return $controllerFileContent; - } - - /** * Get model content from model stub * * @return string Replaced proper model names in model file content diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php new file mode 100644 index 0000000..373ee75 --- /dev/null +++ b/src/Generators/BaseGenerator.php @@ -0,0 +1,105 @@ +files = $files; + + $this->command = $command; + + $this->modelNames = $modelNames; + + $this->stubModelNames = [ + 'model_namespace' => 'mstrNmspc', + 'full_model_name' => 'fullMstr', + 'plural_model_name' => 'Masters', + 'model_name' => 'Master', + 'table_name' => 'masters', + 'lang_name' => 'master', + 'collection_model_var_name' => 'mstrCollections', + 'single_model_var_name' => 'singleMstr', + ]; + } + + /** + * Generate class file content + * + * @return void + */ + abstract public function generate(); + + /** + * Make directory if the path is not exists + * @param string $path Absolute path of targetted directory + * @return string Absolute path + */ + protected function makeDirectory($path) + { + if (! $this->files->isDirectory($path)) { + $this->files->makeDirectory($path, 0777, true, true); + } + + return $path; + } + + /** + * Generate file on filesystem + * @param string $path Absoute path of file + * @param string $content Generated file content + * @return string Absolute path of file + */ + protected function generateFile($path, $content) + { + $this->files->put($path, $content); + + return $path; + } + + /** + * Replace all string of model names + * + * @param string $stub String of file or class stub with default content + * @return string Replaced content + */ + protected function replaceStubString($stub) + { + return str_replace($this->stubModelNames, $this->modelNames, $stub); + } +} \ No newline at end of file diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php new file mode 100644 index 0000000..ca3434f --- /dev/null +++ b/src/Generators/ControllerGenerator.php @@ -0,0 +1,59 @@ +command->option('parent'))) { + $parentControllerDirectory = '/'.$this->command->option('parent'); + } + $controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory)); + + $controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php'; + $this->generateFile($controllerPath, $this->getControllerContent()); + + $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 + */ + public function getControllerContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/controller.model.stub'); + + $controllerFileContent = $this->replaceStubString($stub); + + if (! is_null($parentName = $this->command->option('parent'))) { + + $searches = [ + 'App\Http\Controllers;', + "use {$this->modelNames['full_model_name']};\n" + ]; + + $replacements = [ + "App\Http\Controllers\\{$parentName};", + "use {$this->modelNames['full_model_name']};\nuse App\Http\Controllers\Controller;\n" + ]; + + $controllerFileContent = str_replace( + $searches, + $replacements, + $controllerFileContent + ); + } + + return $controllerFileContent; + } +} \ No newline at end of file