From 804118fb081afa2398dd32345049e53e125981c9 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 19 Oct 2017 12:47:21 +0800 Subject: [PATCH] Add lang file, index view, form view and model factory generator class --- src/CrudMake.php | 135 ++----------------------------- src/Generators/FormViewGenerator.php | 30 +++++++ src/Generators/IndexViewGenerator.php | 30 +++++++ src/Generators/LangFileGenerator.php | 64 +++++++++++++++ src/Generators/ModelFactoryGenerator.php | 33 ++++++++ 5 files changed, 165 insertions(+), 127 deletions(-) create mode 100644 src/Generators/FormViewGenerator.php create mode 100644 src/Generators/IndexViewGenerator.php create mode 100644 src/Generators/LangFileGenerator.php create mode 100644 src/Generators/ModelFactoryGenerator.php diff --git a/src/CrudMake.php b/src/CrudMake.php index 2f8f00c..06d9b3c 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -5,7 +5,11 @@ namespace Luthfi\CrudGenerator; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Luthfi\CrudGenerator\Generators\ControllerGenerator; +use Luthfi\CrudGenerator\Generators\FormViewGenerator; +use Luthfi\CrudGenerator\Generators\IndexViewGenerator; +use Luthfi\CrudGenerator\Generators\LangFileGenerator; use Luthfi\CrudGenerator\Generators\MigrationGenerator; +use Luthfi\CrudGenerator\Generators\ModelFactoryGenerator; use Luthfi\CrudGenerator\Generators\ModelGenerator; class CrudMake extends Command @@ -82,9 +86,10 @@ class CrudMake extends Command app(ModelGenerator::class, ['command' => $this])->generate(); app(MigrationGenerator::class, ['command' => $this])->generate(); app(ControllerGenerator::class, ['command' => $this])->generate(); - $this->generateViews(); - $this->generateLangFile(); - $this->generateModelFactory(); + app(IndexViewGenerator::class, ['command' => $this])->generate(); + app(FormViewGenerator::class, ['command' => $this])->generate(); + app(LangFileGenerator::class, ['command' => $this])->generate(); + app(ModelFactoryGenerator::class, ['command' => $this])->generate(); $this->generateTests(); $this->info('CRUD files generated successfully!'); @@ -144,77 +149,6 @@ class CrudMake extends Command } /** - * Generate the index view and forms view files - * - * @return void - */ - public function generateViews() - { - $viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name'])); - - $this->generateFile($viewPath.'/index.blade.php', $this->getIndexViewContent()); - $this->generateFile($viewPath.'/forms.blade.php', $this->getFormsViewContent()); - - $this->info($this->modelNames['model_name'].' view files generated.'); - } - - /** - * Generate lang file for current model - * - * @return void - */ - public function generateLangFile() - { - $langPath = $this->makeDirectory(resource_path('lang/en')); - - $this->createAppLangFile($langPath); - $this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getLangFileContent()); - - $this->info($this->modelNames['lang_name'].' lang files generated.'); - } - - /** - * Generate lang/app.php file if it doesn't exists - * - * @param string $langPath Directory path of lang files - * @return void - */ - public function createAppLangFile($langPath) - { - if (! $this->files->exists($langPath.'/app.php')) { - $this->generateFile($langPath.'/app.php', $this->getAppLangFileContent()); - $this->info('lang/app.php generated.'); - } - } - - /** - * Get lang/app.php file content - * - * @return string - */ - public function getAppLangFileContent() - { - return $this->files->get(__DIR__.'/stubs/lang-app.stub'); - } - - /** - * Generate model factory file - * - * @return void - */ - public function generateModelFactory() - { - $modelFactoryPath = $this->makeDirectory(database_path('factories')); - - $this->generateFile( - $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php', - $this->getModelFactoryContent() - ); - - $this->info($this->modelNames['model_name'].' model factory generated.'); - } - - /** * Generate Feature for CRUD Operation and and Unit Testing for Model behaviour * @return void */ @@ -264,59 +198,6 @@ class CrudMake extends Command } /** - * Get index view file content from index view stub - * - * @return string Replaced proper model names in view file content - */ - public function getIndexViewContent() - { - $stub = $this->files->get(__DIR__.'/stubs/view-index.stub'); - return $this->replaceStubString($stub); - } - - /** - * Get forms view file content from forms view stub - * - * @return string Replaced proper model names in forms view file content - */ - public function getFormsViewContent() - { - $stub = $this->files->get(__DIR__.'/stubs/view-forms.stub'); - return $this->replaceStubString($stub); - } - - /** - * Get lang file content from lang file stub - * - * @return string Replaced proper model names in lang file content - */ - public function getLangFileContent() - { - $stub = $this->files->get(__DIR__.'/stubs/lang.stub'); - - $displayModelName = ucwords(str_replace('_', ' ', snake_case($this->modelNames['model_name']))); - - $properLangFileContent = str_replace( - $this->modelNames['model_name'], - $displayModelName, - $this->replaceStubString($stub) - ); - - return $properLangFileContent; - } - - /** - * Get model factory file content from model factory stub - * - * @return string Replaced proper model names in model factory file content - */ - public function getModelFactoryContent() - { - $stub = $this->files->get(__DIR__.'/stubs/model-factory.stub'); - return $this->replaceStubString($stub); - } - - /** * Get BrowserKitBaseTest class file content * * @return string diff --git a/src/Generators/FormViewGenerator.php b/src/Generators/FormViewGenerator.php new file mode 100644 index 0000000..fb47157 --- /dev/null +++ b/src/Generators/FormViewGenerator.php @@ -0,0 +1,30 @@ +makeDirectory(resource_path('views/'.$this->modelNames['table_name'])); + + $this->generateFile($viewPath.'/forms.blade.php', $this->getContent()); + + $this->command->info($this->modelNames['model_name'].' form view file generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/view-forms.stub'); + return $this->replaceStubString($stub); + } +} \ No newline at end of file diff --git a/src/Generators/IndexViewGenerator.php b/src/Generators/IndexViewGenerator.php new file mode 100644 index 0000000..98a1add --- /dev/null +++ b/src/Generators/IndexViewGenerator.php @@ -0,0 +1,30 @@ +makeDirectory(resource_path('views/'.$this->modelNames['table_name'])); + + $this->generateFile($viewPath.'/index.blade.php', $this->getContent()); + + $this->command->info($this->modelNames['model_name'].' index view file generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/view-index.stub'); + return $this->replaceStubString($stub); + } +} \ No newline at end of file diff --git a/src/Generators/LangFileGenerator.php b/src/Generators/LangFileGenerator.php new file mode 100644 index 0000000..d0d0e93 --- /dev/null +++ b/src/Generators/LangFileGenerator.php @@ -0,0 +1,64 @@ +makeDirectory(resource_path('lang/en')); + + $this->createAppLangFile($langPath); + $this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent()); + + $this->command->info($this->modelNames['lang_name'].' lang files generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/lang.stub'); + + $displayModelName = ucwords(str_replace('_', ' ', snake_case($this->modelNames['model_name']))); + + $properLangFileContent = str_replace( + $this->modelNames['model_name'], + $displayModelName, + $this->replaceStubString($stub) + ); + + return $properLangFileContent; + } + + /** + * Generate lang/app.php file if it doesn't exists + * + * @param string $langPath Directory path of lang files + * @return void + */ + private function createAppLangFile($langPath) + { + if (! $this->files->exists($langPath.'/app.php')) { + $this->generateFile($langPath.'/app.php', $this->getAppLangFileContent()); + $this->command->info('lang/app.php generated.'); + } + } + + /** + * Get lang/app.php file content + * + * @return string + */ + private function getAppLangFileContent() + { + return $this->files->get(__DIR__.'/../stubs/lang-app.stub'); + } +} \ No newline at end of file diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php new file mode 100644 index 0000000..bb9ec9a --- /dev/null +++ b/src/Generators/ModelFactoryGenerator.php @@ -0,0 +1,33 @@ +makeDirectory(database_path('factories')); + + $this->generateFile( + $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php', + $this->getContent() + ); + + $this->command->info($this->modelNames['model_name'].' model factory generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/model-factory.stub'); + return $this->replaceStubString($stub); + } +} \ No newline at end of file