diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php index 35cbfe4..ca9cd77 100644 --- a/src/Generators/BaseGenerator.php +++ b/src/Generators/BaseGenerator.php @@ -80,12 +80,14 @@ abstract class BaseGenerator /** * Get class file content * + * @param string $stubName Name of stub file * @return void */ - abstract protected function getContent(); + abstract protected function getContent(string $stubName); /** * Make directory if the path is not exists + * * @param string $path Absolute path of targetted directory * @return string Absolute path */ @@ -121,4 +123,15 @@ abstract class BaseGenerator { return str_replace($this->stubModelNames, $this->command->modelNames, $stub); } + + /** + * Get correct stub file content + * + * @param string $stubName The stub file name + * @return string The stub file content + */ + protected function getStubFileContent(string $stubName) + { + return $this->files->get(__DIR__.'/../stubs/'.$stubName.'.stub'); + } } diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index 2df2830..bc52251 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -19,7 +19,7 @@ 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->getContent()); + $this->generateFile($controllerPath, $this->getContent('controller.model')); $this->command->info($this->modelNames['plural_model_name'].'Controller generated.'); } @@ -27,9 +27,9 @@ class ControllerGenerator extends BaseGenerator /** * {@inheritDoc} */ - public function getContent() + public function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/controller.model.stub'); + $stub = $this->getStubFileContent($stubName); $controllerFileContent = $this->replaceStubString($stub); diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php index 89411a0..14a41a9 100644 --- a/src/Generators/FeatureTestGenerator.php +++ b/src/Generators/FeatureTestGenerator.php @@ -15,16 +15,21 @@ class FeatureTestGenerator extends BaseGenerator $this->createBrowserKitBaseTestClass(); $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); - $this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent()); + + $this->generateFile( + "{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", + $this->getContent('test-feature') + ); + $this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.'); } /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/test-feature.stub'); + $stub = $this->getStubFileContent($stubName); $baseTestClass = config('simple-crud.base_test_class'); $stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub); return $this->replaceStubString($stub); diff --git a/src/Generators/FormViewGenerator.php b/src/Generators/FormViewGenerator.php index 52dc6ed..4b9bd4a 100644 --- a/src/Generators/FormViewGenerator.php +++ b/src/Generators/FormViewGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Form View Generator Class -*/ + * Form View Generator Class + */ class FormViewGenerator extends BaseGenerator { /** @@ -14,7 +14,7 @@ class FormViewGenerator extends BaseGenerator { $viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name'])); - $this->generateFile($viewPath.'/forms.blade.php', $this->getContent()); + $this->generateFile($viewPath.'/forms.blade.php', $this->getContent('view-forms')); $this->command->info($this->modelNames['model_name'].' form view file generated.'); } @@ -22,9 +22,8 @@ class FormViewGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/view-forms.stub'); - return $this->replaceStubString($stub); + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/IndexViewGenerator.php b/src/Generators/IndexViewGenerator.php index 682c66f..0fafa2f 100644 --- a/src/Generators/IndexViewGenerator.php +++ b/src/Generators/IndexViewGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Index View Generator Class -*/ + * Index View Generator Class + */ class IndexViewGenerator extends BaseGenerator { /** @@ -14,7 +14,7 @@ class IndexViewGenerator extends BaseGenerator { $viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name'])); - $this->generateFile($viewPath.'/index.blade.php', $this->getContent()); + $this->generateFile($viewPath.'/index.blade.php', $this->getContent('view-index')); $this->command->info($this->modelNames['model_name'].' index view file generated.'); } @@ -22,9 +22,8 @@ class IndexViewGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/view-index.stub'); - return $this->replaceStubString($stub); + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/LangFileGenerator.php b/src/Generators/LangFileGenerator.php index 0d9a882..aba5254 100644 --- a/src/Generators/LangFileGenerator.php +++ b/src/Generators/LangFileGenerator.php @@ -16,7 +16,8 @@ class LangFileGenerator extends BaseGenerator $langPath = $this->makeDirectory(resource_path('lang/'.$locale)); $this->createAppLangFile($langPath); - $this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent()); + + $this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent('lang-'.$locale)); $this->command->info($this->modelNames['lang_name'].' lang files generated.'); } @@ -24,11 +25,9 @@ class LangFileGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $locale = config('app.locale'); - - $langStubPath = __DIR__.'/../stubs/lang-'.$locale.'.stub'; + $langStubPath = __DIR__.'/../stubs/'.$stubName.'.stub'; if ($this->files->exists($langStubPath)) { $stub = $this->files->get($langStubPath); diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index a7e03a6..56b3d0c 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Migration Generator Class -*/ + * Migration Generator Class + */ class MigrationGenerator extends BaseGenerator { /** @@ -18,7 +18,7 @@ class MigrationGenerator extends BaseGenerator $migrationPath = $this->makeDirectory(database_path('migrations')); $migrationFilePath = $migrationPath.'/'.$prefix."_create_{$tableName}_table.php"; - $this->generateFile($migrationFilePath, $this->getContent()); + $this->generateFile($migrationFilePath, $this->getContent('migration-create')); $this->command->info($this->modelNames['model_name'].' table migration generated.'); } @@ -26,9 +26,8 @@ class MigrationGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/migration-create.stub'); - return $this->replaceStubString($stub); + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 3270218..8962616 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Model Factory Generator Class -*/ + * Model Factory Generator Class + */ class ModelFactoryGenerator extends BaseGenerator { /** @@ -16,7 +16,7 @@ class ModelFactoryGenerator extends BaseGenerator $this->generateFile( $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php', - $this->getContent() + $this->getContent('model-factory') ); $this->command->info($this->modelNames['model_name'].' model factory generated.'); @@ -25,9 +25,8 @@ class ModelFactoryGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/model-factory.stub'); - return $this->replaceStubString($stub); + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index f55a2e8..7292b5b 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Model Generator Class -*/ + * Model Generator Class + */ class ModelGenerator extends BaseGenerator { /** @@ -15,7 +15,10 @@ class ModelGenerator extends BaseGenerator $modelPath = $this->modelNames['model_path']; $modelDirectory = $this->makeDirectory(app_path($modelPath)); - $this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getContent()); + $this->generateFile( + $modelDirectory.'/'.$this->modelNames['model_name'].'.php', + $this->getContent('model') + ); $this->command->info($this->modelNames['model_name'].' model generated.'); } @@ -23,9 +26,8 @@ class ModelGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/model.stub'); - return $this->replaceStubString($stub); + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php index 214dc3b..3d09a57 100644 --- a/src/Generators/ModelPolicyGenerator.php +++ b/src/Generators/ModelPolicyGenerator.php @@ -20,7 +20,7 @@ class ModelPolicyGenerator extends BaseGenerator $this->generateFile( $modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php', - $this->getContent() + $this->getContent('model-policy') ); $this->command->info($this->modelNames['model_name'].' model policy generated.'); @@ -31,9 +31,9 @@ class ModelPolicyGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/model-policy.stub'); + $stub = $this->getStubFileContent($stubName); $policyFileContent = $this->replaceStubString($stub); diff --git a/src/Generators/ModelPolicyTestGenerator.php b/src/Generators/ModelPolicyTestGenerator.php index 475f970..aa22dd7 100644 --- a/src/Generators/ModelPolicyTestGenerator.php +++ b/src/Generators/ModelPolicyTestGenerator.php @@ -13,18 +13,23 @@ class ModelPolicyTestGenerator extends BaseGenerator public function generate() { $modelPolicyTestPath = $this->makeDirectory(base_path('tests/Unit/Policies')); - $this->generateFile("{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php", $this->getContent()); - $this->command->info($this->modelNames['model_name'] . 'PolicyTest (model policy) generated.'); + + $this->generateFile( + "{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php", + $this->getContent('test-policy') + ); + + $this->command->info($this->modelNames['model_name'].'PolicyTest (model policy) generated.'); } /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__ . '/../stubs/test-policy.stub'); + $stub = $this->getStubFileContent($stubName); $baseTestClass = config('simple-crud.base_test_class'); - $stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub); + $stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub); return $this->replaceStubString($stub); } } diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php index f4dca19..70a7199 100644 --- a/src/Generators/ModelTestGenerator.php +++ b/src/Generators/ModelTestGenerator.php @@ -13,18 +13,23 @@ class ModelTestGenerator extends BaseGenerator public function generate() { $unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models')); - $this->generateFile("{$unitTestPath}/{$this->modelNames['model_name']}Test.php", $this->getContent()); - $this->command->info($this->modelNames['model_name'] . 'Test (model) generated.'); + + $this->generateFile( + "{$unitTestPath}/{$this->modelNames['model_name']}Test.php", + $this->getContent('test-unit') + ); + + $this->command->info($this->modelNames['model_name'].'Test (model) generated.'); } /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__ . '/../stubs/test-unit.stub'); + $stub = $this->getStubFileContent($stubName); $baseTestClass = config('simple-crud.base_test_class'); - $stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub); + $stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub); return $this->replaceStubString($stub); } } diff --git a/src/Generators/WebRouteGenerator.php b/src/Generators/WebRouteGenerator.php index fd763bf..57c69fc 100644 --- a/src/Generators/WebRouteGenerator.php +++ b/src/Generators/WebRouteGenerator.php @@ -13,7 +13,8 @@ class WebRouteGenerator extends BaseGenerator public function generate() { $webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php'); - $this->files->append($webRoutePath, $this->getContent()); + + $this->files->append($webRoutePath, $this->getContent('route-web')); $this->command->info($this->modelNames['model_name'].' resource route generated on routes/web.php.'); } @@ -21,9 +22,9 @@ class WebRouteGenerator extends BaseGenerator /** * {@inheritDoc} */ - protected function getContent() + protected function getContent(string $stubName) { - $stub = $this->files->get(__DIR__.'/../stubs/route-web.stub'); + $stub = $this->getStubFileContent($stubName); $webRouteFileContent = $this->replaceStubString($stub);