From e6ce09262a79dcad43261f6719060f6a508e95f8 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 15 Oct 2017 13:10:14 +0800 Subject: [PATCH] Set proper display name for two-words model name --- src/CrudMake.php | 20 ++++++++++++------ src/stubs/lang.stub | 2 +- tests/CrudMakeCommandTest.php | 2 ++ tests/Generators/LangGeneratorTest.php | 37 +++++++++++++++++----------------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index 35265e1..13aac5e 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -2,7 +2,6 @@ namespace Luthfi\CrudGenerator; -use Illuminate\Support\Str; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; @@ -63,7 +62,7 @@ class CrudMake extends Command { $this->getModelName(); - if ( ! $this->modelExists()) { + if (! $this->modelExists()) { $this->generateResourceRoute(); $this->generateModel(); @@ -75,9 +74,9 @@ class CrudMake extends Command $this->generateTests(); $this->info('CRUD files generated successfully!'); + } else { + $this->error("{$this->modelNames['model_name']} model already exists."); } - - $this->error("{$this->modelNames['model_name']} model already exists."); } /** @@ -313,7 +312,16 @@ class CrudMake extends Command public function getLangFileContent() { $stub = $this->files->get(__DIR__.'/stubs/lang.stub'); - return $this->replaceStubString($stub); + + $displayModelName = ucwords(str_replace('_', ' ', snake_case($this->modelNames['model_name']))); + + $properLangFileContent = str_replace( + $this->modelNames['model_name'], + $displayModelName, + $this->replaceStubString($stub) + ); + + return $properLangFileContent; } /** @@ -411,7 +419,7 @@ class CrudMake extends Command */ protected function replaceStubString($stub) { - return str_replace($this->stubModelNames, $this->modelNames, $stub ); + return str_replace($this->stubModelNames, $this->modelNames, $stub); } /** diff --git a/src/stubs/lang.stub b/src/stubs/lang.stub index c296fc5..77e5e19 100644 --- a/src/stubs/lang.stub +++ b/src/stubs/lang.stub @@ -2,7 +2,7 @@ return [ // Labels - 'master' => 'Master', + 'master' => 'Master', 'list' => 'Master List', 'search' => 'Search Master', 'not_found' => 'Master not found.', diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index 7cd5157..bf7a85b 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -37,6 +37,8 @@ class CrudMakeCommandTest extends TestCase { $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + $this->assertNotRegExp("/{$this->model_name} model already exists./", app(Kernel::class)->output()); + $this->assertFileExists(app_path($this->model_name.'.php')); $this->assertFileExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php")); diff --git a/tests/Generators/LangGeneratorTest.php b/tests/Generators/LangGeneratorTest.php index 5c6b4c9..88ba84b 100644 --- a/tests/Generators/LangGeneratorTest.php +++ b/tests/Generators/LangGeneratorTest.php @@ -12,33 +12,34 @@ class LangGeneratorTest extends TestCase $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); $langPath = resource_path('lang/en/'.$this->lang_name.'.php'); + $displayModelName = ucwords(str_replace('_', ' ', snake_case($this->model_name))); $this->assertFileExists($langPath); $langFileContent = "lang_name}' => '{$this->model_name}', - 'list' => '{$this->model_name} List', - 'search' => 'Search {$this->model_name}', - 'not_found' => '{$this->model_name} not found.', - 'empty' => '{$this->model_name} is empty.', - 'back_to_show' => 'Back to {$this->model_name} Detail', - 'back_to_index' => 'Back to {$this->model_name} List', + '{$this->lang_name}' => '{$displayModelName}', + 'list' => '{$displayModelName} List', + 'search' => 'Search {$displayModelName}', + 'not_found' => '{$displayModelName} not found.', + 'empty' => '{$displayModelName} is empty.', + 'back_to_show' => 'Back to {$displayModelName} Detail', + 'back_to_index' => 'Back to {$displayModelName} List', // Actions - 'create' => 'Create new {$this->model_name}', - 'created' => 'Create new {$this->model_name} succeded.', - 'edit' => 'Edit {$this->model_name}', - 'update' => 'Update {$this->model_name}', - 'updated' => 'Update {$this->model_name} succeded.', - 'delete' => 'Delete {$this->model_name}', - 'delete_confirm' => 'Are you sure to delete this {$this->model_name}?', - 'deleted' => 'Delete {$this->model_name} succeded.', - 'undeleted' => '{$this->model_name} not deleted.', + 'create' => 'Create new {$displayModelName}', + 'created' => 'Create new {$displayModelName} succeded.', + 'edit' => 'Edit {$displayModelName}', + 'update' => 'Update {$displayModelName}', + 'updated' => 'Update {$displayModelName} succeded.', + 'delete' => 'Delete {$displayModelName}', + 'delete_confirm' => 'Are you sure to delete this {$displayModelName}?', + 'deleted' => 'Delete {$displayModelName} succeded.', + 'undeleted' => '{$displayModelName} not deleted.', // Attributes - 'name' => '{$this->model_name} Name', - 'description' => '{$this->model_name} Description', + 'name' => '{$displayModelName} Name', + 'description' => '{$displayModelName} Description', ]; "; $this->assertEquals($langFileContent, file_get_contents($langPath));