diff --git a/src/CrudMake.php b/src/CrudMake.php index 254626c..579e8ca 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -52,14 +52,14 @@ class CrudMake extends Command $this->files = $files; $this->stubModelNames = [ - 'model_namespace' => 'mstrNmspc', - 'full_model_name' => 'fullMstr', - 'plural_model_name' => 'Masters', - 'model_name' => 'Master', - 'table_name' => 'masters', - 'lang_name' => 'master', + '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', + 'single_model_var_name' => 'singleMstr', ]; } @@ -86,24 +86,31 @@ class CrudMake extends Command { $this->getModelName(); - if ( ! $this->modelExists()) { - app(WebRouteGenerator::class, ['command' => $this])->generate(); - app(ModelGenerator::class, ['command' => $this])->generate(); - app(MigrationGenerator::class, ['command' => $this])->generate(); - app(ControllerGenerator::class, ['command' => $this])->generate(); - app(IndexViewGenerator::class, ['command' => $this])->generate(); - app(FormViewGenerator::class, ['command' => $this])->generate(); - app(LangFileGenerator::class, ['command' => $this])->generate(); - app(ModelFactoryGenerator::class, ['command' => $this])->generate(); - app(ModelPolicyGenerator::class, ['command' => $this])->generate(); - app(FeatureTestGenerator::class, ['command' => $this])->generate(); - app(ModelTestGenerator::class, ['command' => $this])->generate(); - app(ModelPolicyTestGenerator::class, ['command' => $this])->generate(); - - $this->info('CRUD files generated successfully!'); - } else { + if ($this->modelExists()) { $this->error("{$this->modelNames['model_name']} model already exists."); + return; } + + // Warn if it has no default layout view based on + // simple-crud.default_layout_view config + if ($this->defaultLayoutNotExists()) { + $this->warn(config('simple-crud.default_layout_view').' view does not exists.'); + } + + app(WebRouteGenerator::class, ['command' => $this])->generate(); + app(ModelGenerator::class, ['command' => $this])->generate(); + app(MigrationGenerator::class, ['command' => $this])->generate(); + app(ControllerGenerator::class, ['command' => $this])->generate(); + app(IndexViewGenerator::class, ['command' => $this])->generate(); + app(FormViewGenerator::class, ['command' => $this])->generate(); + app(LangFileGenerator::class, ['command' => $this])->generate(); + app(ModelFactoryGenerator::class, ['command' => $this])->generate(); + app(ModelPolicyGenerator::class, ['command' => $this])->generate(); + app(FeatureTestGenerator::class, ['command' => $this])->generate(); + app(ModelTestGenerator::class, ['command' => $this])->generate(); + app(ModelPolicyTestGenerator::class, ['command' => $this])->generate(); + + $this->info('CRUD files generated successfully!'); } /** @@ -113,22 +120,22 @@ class CrudMake extends Command */ public function getModelName($modelName = null) { - $modelName = is_null($modelName) ? $this->argument('name') : $modelName; - $model_name = ucfirst(class_basename($modelName)); + $modelName = is_null($modelName) ? $this->argument('name') : $modelName; + $model_name = ucfirst(class_basename($modelName)); $plural_model_name = str_plural($model_name); - $modelPath = $this->getModelPath($modelName); - $modelNamespace = $this->getModelNamespace($modelPath); + $modelPath = $this->getModelPath($modelName); + $modelNamespace = $this->getModelNamespace($modelPath); return $this->modelNames = [ - 'model_namespace' => $modelNamespace, - 'full_model_name' => $modelNamespace.'\\'.$model_name, - 'plural_model_name' => $plural_model_name, - 'model_name' => $model_name, - 'table_name' => snake_case($plural_model_name), - 'lang_name' => snake_case($model_name), + 'model_namespace' => $modelNamespace, + 'full_model_name' => $modelNamespace.'\\'.$model_name, + 'plural_model_name' => $plural_model_name, + 'model_name' => $model_name, + 'table_name' => snake_case($plural_model_name), + 'lang_name' => snake_case($model_name), 'collection_model_var_name' => camel_case($plural_model_name), - 'single_model_var_name' => camel_case($model_name), - 'model_path' => $modelPath, + 'single_model_var_name' => camel_case($model_name), + 'model_path' => $modelPath, ]; } @@ -169,4 +176,16 @@ class CrudMake extends Command app_path($this->modelNames['model_path'].'/'.$this->modelNames['model_name'].'.php') ); } + + /** + * Check for default layout view file existance + * + * @return void + */ + public function defaultLayoutNotExists() + { + return ! $this->files->exists( + resource_path('views/'.str_replace('.', '/', config('simple-crud.default_layout_view')).'.blade.php') + ); + } } diff --git a/tests/Generators/ViewsGeneratorTest.php b/tests/Generators/ViewsGeneratorTest.php index 9853e36..5ba6ec6 100644 --- a/tests/Generators/ViewsGeneratorTest.php +++ b/tests/Generators/ViewsGeneratorTest.php @@ -2,6 +2,7 @@ namespace Tests\Generators; +use Illuminate\Contracts\Console\Kernel; use Tests\TestCase; class ViewsGeneratorTest extends TestCase @@ -137,4 +138,35 @@ class ViewsGeneratorTest extends TestCase "; $this->assertEquals($formViewContent, file_get_contents($formViewPath)); } + + /** @test */ + public function it_not_gives_warning_message_if_default_layout_view_does_exists() + { + $defaultLayoutView = config('simple-crud.default_layout_view'); + $this->generateDefaultLayoutView($defaultLayoutView); + + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertNotRegExp("/{$defaultLayoutView} view does not exists./", app(Kernel::class)->output()); + } + + /** @test */ + public function it_gives_warning_message_if_default_layout_view_does_not_exists() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + $defaultLayoutView = config('simple-crud.default_layout_view'); + + $this->assertRegExp("/{$defaultLayoutView} view does not exists./", app(Kernel::class)->output()); + } + + public function generateDefaultLayoutView($defaultLayoutView) + { + $dataViewPathArray = explode('.', $defaultLayoutView); + $fileName = array_pop($dataViewPathArray); + $defaultLayoutPath = resource_path('views/'.implode('/', $dataViewPathArray)); + + $files = app('Illuminate\Filesystem\Filesystem'); + $files->makeDirectory($defaultLayoutPath); + $files->put($defaultLayoutPath.'/'.$fileName.'.blade.php', ''); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index d9364f5..0fa1166 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,12 +19,12 @@ abstract class TestCase extends BaseTestCase parent::setUp(); $this->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() @@ -43,6 +43,14 @@ abstract class TestCase extends BaseTestCase $this->removeFileOrDir(database_path('factories')); $this->removeFileOrDir(resource_path('views/'.$this->table_name)); + $defaultLayoutsFile = config('simple-crud.default_layout_view'); + + $dataViewPathArray = explode('.', $defaultLayoutsFile); + $fileName = array_pop($dataViewPathArray); + $defaultLayoutPath = resource_path('views/'.implode('/', $dataViewPathArray)); + + $this->removeFileOrDir($defaultLayoutPath); + $locale = config('app.locale'); $this->removeFileOrDir(resource_path("lang/{$locale}/app.php")); $this->removeFileOrDir(resource_path("lang/{$locale}/{$this->lang_name}.php"));