From b9a6cc68112931f1f667b7dea6a7b99e038e3740 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 24 Oct 2017 22:25:37 +0800 Subject: [PATCH] Generate lang file within locale config --- src/CrudMake.php | 6 ++++-- src/Generators/LangFileGenerator.php | 9 +++++---- src/config.php | 10 +++++----- tests/CrudMakeCommandTest.php | 25 ++++++++++++++++++++----- tests/Generators/LangGeneratorTest.php | 7 +++++-- tests/TestCase.php | 25 ++++++++++++++----------- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index 417684d..254626c 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -86,7 +86,7 @@ class CrudMake extends Command { $this->getModelName(); - if (! $this->modelExists()) { + if ( ! $this->modelExists()) { app(WebRouteGenerator::class, ['command' => $this])->generate(); app(ModelGenerator::class, ['command' => $this])->generate(); app(MigrationGenerator::class, ['command' => $this])->generate(); @@ -165,6 +165,8 @@ class CrudMake extends Command */ public function modelExists() { - return $this->files->exists(app_path($this->modelNames['model_path'].'/'.$this->modelNames['model_name'].'.php')); + return $this->files->exists( + app_path($this->modelNames['model_path'].'/'.$this->modelNames['model_name'].'.php') + ); } } diff --git a/src/Generators/LangFileGenerator.php b/src/Generators/LangFileGenerator.php index 25e23d8..5518a51 100644 --- a/src/Generators/LangFileGenerator.php +++ b/src/Generators/LangFileGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Lang File Generator Class -*/ + * Lang File Generator Class + */ class LangFileGenerator extends BaseGenerator { /** @@ -12,7 +12,8 @@ class LangFileGenerator extends BaseGenerator */ public function generate() { - $langPath = $this->makeDirectory(resource_path('lang/en')); + $locale = config('app.locale'); + $langPath = $this->makeDirectory(resource_path('lang/'.$locale)); $this->createAppLangFile($langPath); $this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent()); @@ -46,7 +47,7 @@ class LangFileGenerator extends BaseGenerator */ private function createAppLangFile($langPath) { - if (! $this->files->exists($langPath.'/app.php')) { + if ( ! $this->files->exists($langPath.'/app.php')) { $this->generateFile($langPath.'/app.php', $this->getAppLangFileContent()); $this->command->info('lang/app.php generated.'); } diff --git a/src/config.php b/src/config.php index 0f62936..0a03a7a 100644 --- a/src/config.php +++ b/src/config.php @@ -9,7 +9,7 @@ return [ | | Default Laravel view layout | - */ + */ 'default_layout_view' => 'layouts.app', @@ -20,9 +20,9 @@ return [ | | Base TestCase Path on Laravel application | - */ + */ - 'base_test_path' => 'tests/BrowserKitTest.php', + 'base_test_path' => 'tests/BrowserKitTest.php', /* |-------------------------------------------------------------------------- @@ -32,8 +32,8 @@ return [ | Base Test Class that used on Laravel application | according to 'base_test_path' config above | - */ + */ - 'base_test_class' => 'Tests\BrowserKitTest', + 'base_test_class' => 'Tests\BrowserKitTest', ]; diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index 99d365f..594a401 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -21,7 +21,10 @@ class CrudMakeCommandTest extends TestCase $this->assertFileExists(resource_path("views/{$this->table_name}/index.blade.php")); $this->assertFileExists(resource_path("views/{$this->table_name}/forms.blade.php")); - $this->assertFileExists(resource_path("lang/en/{$this->lang_name}.php")); + + $localeConfig = config('app.locale'); + $this->assertFileExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php")); + $this->assertFileExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileExists(base_path("routes/web.php")); $this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php")); @@ -45,7 +48,10 @@ class CrudMakeCommandTest extends TestCase $this->assertFileNotExists(resource_path("views/{$this->table_name}/index.blade.php")); $this->assertFileNotExists(resource_path("views/{$this->table_name}/forms.blade.php")); - $this->assertFileNotExists(resource_path("lang/en/{$this->lang_name}.php")); + + $localeConfig = config('app.locale'); + $this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php")); + $this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileNotExists(base_path("routes/web.php")); $this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php")); @@ -69,7 +75,10 @@ class CrudMakeCommandTest extends TestCase $this->assertFileNotExists(resource_path("views/problems/index.blade.php")); $this->assertFileNotExists(resource_path("views/problems/forms.blade.php")); - $this->assertFileNotExists(resource_path("lang/en/problem.php")); + + $localeConfig = config('app.locale'); + $this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php")); + $this->assertFileNotExists(database_path("factories/ProblemFactory.php")); $this->assertFileNotExists(app_path("Policies/ProblemPolicy.php")); $this->assertFileNotExists(base_path("tests/Feature/ManageProblemsTest.php")); @@ -102,7 +111,10 @@ class CrudMakeCommandTest extends TestCase $this->assertFileExists(resource_path("views/{$tableName}/index.blade.php")); $this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php")); - $this->assertFileExists(resource_path("lang/en/{$langName}.php")); + + $localeConfig = config('app.locale'); + $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); + $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); $this->assertFileExists(app_path("Policies/{$modelName}Policy.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); @@ -132,7 +144,10 @@ class CrudMakeCommandTest extends TestCase $this->assertFileExists(resource_path("views/{$tableName}/index.blade.php")); $this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php")); - $this->assertFileExists(resource_path("lang/en/{$langName}.php")); + + $localeConfig = config('app.locale'); + $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); + $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); $this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); diff --git a/tests/Generators/LangGeneratorTest.php b/tests/Generators/LangGeneratorTest.php index 4f8a179..368b266 100644 --- a/tests/Generators/LangGeneratorTest.php +++ b/tests/Generators/LangGeneratorTest.php @@ -11,7 +11,8 @@ class LangGeneratorTest extends TestCase { $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $langPath = resource_path('lang/en/'.$this->lang_name.'.php'); + $locale = config('app.locale'); + $langPath = resource_path('lang/'.$locale.'/'.$this->lang_name.'.php'); $displayModelName = ucwords(str_replace('_', ' ', snake_case($this->model_name))); $this->assertFileExists($langPath); $langFileContent = "artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $langPath = resource_path('lang/en/app.php'); + $locale = config('app.locale'); + $langPath = resource_path('lang/'.$locale.'/app.php'); + $this->assertFileExists($langPath); $appLangContent = "model_name = class_basename('References/Category'); - $this->full_model_name = 'App\\' . $this->model_name; - $this->plural_model_name = str_plural($this->model_name); - $this->table_name = snake_case($this->plural_model_name); - $this->lang_name = snake_case($this->model_name); + $this->full_model_name = 'App\\'.$this->model_name; + $this->plural_model_name = str_plural($this->model_name); + $this->table_name = snake_case($this->plural_model_name); + $this->lang_name = snake_case($this->model_name); $this->collection_model_var_name = camel_case($this->plural_model_name); - $this->single_model_var_name = camel_case($this->model_name); + $this->single_model_var_name = camel_case($this->model_name); } public function tearDown() @@ -36,14 +36,17 @@ abstract class TestCase extends BaseTestCase protected function cleanUpGeneratedFiles() { - $this->removeFileOrDir(app_path($this->model_name . '.php')); + $this->removeFileOrDir(app_path($this->model_name.'.php')); $this->removeFileOrDir(app_path('Entities')); $this->removeFileOrDir(app_path('Http')); $this->removeFileOrDir(database_path('migrations')); $this->removeFileOrDir(database_path('factories')); - $this->removeFileOrDir(resource_path('views/' . $this->table_name)); - $this->removeFileOrDir(resource_path("lang/en/app.php")); - $this->removeFileOrDir(resource_path("lang/en/{$this->lang_name}.php")); + $this->removeFileOrDir(resource_path('views/'.$this->table_name)); + + $locale = config('app.locale'); + $this->removeFileOrDir(resource_path("lang/{$locale}/app.php")); + $this->removeFileOrDir(resource_path("lang/{$locale}/{$this->lang_name}.php")); + $this->removeFileOrDir(base_path('routes')); $this->removeFileOrDir(app_path('Policies')); $this->removeFileOrDir(app_path('Providers')); @@ -53,11 +56,11 @@ abstract class TestCase extends BaseTestCase protected function removeFileOrDir($path) { if (file_exists($path) && is_file($path)) { - exec('rm ' . $path); + exec('rm '.$path); } if (file_exists($path) && is_dir($path)) { - exec('rm -r ' . $path); + exec('rm -r '.$path); } }