From 1af21bdd0f1f17a248d7c1ac3547e5f1abaa82fa Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 22 Aug 2017 23:10:25 +0800 Subject: [PATCH] Fixed controller generator to create correct controller names --- src/CrudMake.php | 27 ++++++++++++++++++++++++++- src/stubs/controller.model.stub | 6 +++--- tests/CrudMakeCommandTest.php | 20 ++++++++++---------- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index 115427a..869ee4b 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -76,6 +76,7 @@ class CrudMake extends Command $this->info($this->pluralModelName.'Controller generated.'); } + public function generateMigration() { $prefix = date('Y_m_d_His'); @@ -109,7 +110,8 @@ class CrudMake extends Command public function getControllerContent() { - return $this->files->get(__DIR__.'/stubs/controller.model.stub'); + $stub = $this->files->get(__DIR__.'/stubs/controller.model.stub'); + return $this->replaceDummyStrings($stub)->replaceClass($stub); } private function getMigrationContent() @@ -145,4 +147,27 @@ class CrudMake extends Command return $path; } + + protected function replaceDummyStrings(&$stub) + { + $stub = str_replace( + ['DummyNamespace', 'DummyFullModelClass', 'DummyModelVariable', 'DummyModelClass'], + [$this->getNamespace($this->modelName), 'App\\'.$this->modelName, strtolower($this->modelName), $this->modelName], + $stub + ); + + return $this; + } + + protected function replaceClass($stub) + { + $class = str_plural($this->modelName); + + return str_replace('DummyClass', $class, $stub); + } + + protected function getNamespace($name) + { + return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\'); + } } diff --git a/src/stubs/controller.model.stub b/src/stubs/controller.model.stub index c5f3a9f..51ecbc4 100644 --- a/src/stubs/controller.model.stub +++ b/src/stubs/controller.model.stub @@ -1,12 +1,12 @@ assertFileExists(app_path('Item.php')); $ctrlClassContent = "