From 81608d0ee85eef48769ddcc95e9c2b3140145f5a Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 31 Mar 2017 15:57:10 -0500 Subject: [PATCH] add ability to create nested model controllers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - new stub for nested controllers - ‘parent’ option for make command - if we want a parent, determine the replacement values based on the model name --- .../Routing/Console/ControllerMakeCommand.php | 26 ++++- .../Console/stubs/controller.nested.stub | 94 +++++++++++++++++++ 2 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/Illuminate/Routing/Console/stubs/controller.nested.stub diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index ba5dc3b5b2d4..6cf075e147e0 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -37,7 +37,9 @@ class ControllerMakeCommand extends GeneratorCommand */ protected function getStub() { - if ($this->option('model')) { + if ($this->option('parent')) { + return __DIR__.'/stubs/controller.nested.stub'; + } elseif ($this->option('model')) { return __DIR__.'/stubs/controller.model.stub'; } elseif ($this->option('resource')) { return __DIR__.'/stubs/controller.stub'; @@ -71,6 +73,22 @@ protected function buildClass($name) $replace = []; + if ($this->option('parent')) { + $parentModelClass = $this->parseModel($this->option('parent')); + + if (! class_exists($parentModelClass)) { + if ($this->confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", true)) { + $this->call('make:model', ['name' => $parentModelClass]); + } + } + + $replace = [ + 'ParentDummyFullModelClass' => $parentModelClass, + 'ParentDummyModelClass' => class_basename($parentModelClass), + 'ParentDummyModelVariable' => lcfirst(class_basename($parentModelClass)), + ]; + } + if ($this->option('model')) { $modelClass = $this->parseModel($this->option('model')); @@ -80,11 +98,11 @@ protected function buildClass($name) } } - $replace = [ + $replace = array_merge($replace, [ 'DummyFullModelClass' => $modelClass, 'DummyModelClass' => class_basename($modelClass), 'DummyModelVariable' => lcfirst(class_basename($modelClass)), - ]; + ]); } $replace["use {$controllerNamespace}\Controller;\n"] = ''; @@ -126,6 +144,8 @@ protected function getOptions() ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'], ['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'], + + ['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class.'], ]; } } diff --git a/src/Illuminate/Routing/Console/stubs/controller.nested.stub b/src/Illuminate/Routing/Console/stubs/controller.nested.stub new file mode 100644 index 000000000000..878244a9cba6 --- /dev/null +++ b/src/Illuminate/Routing/Console/stubs/controller.nested.stub @@ -0,0 +1,94 @@ +