From 76adc126fae69a649b2016016cd515f82c3a9c71 Mon Sep 17 00:00:00 2001 From: Alex Sofronie Date: Wed, 14 Dec 2016 00:29:08 +0200 Subject: [PATCH 1/2] added sub and option to generate resource controller with type-hinted model --- .../Routing/Console/ControllerMakeCommand.php | 53 +++++++++++- .../Console/stubs/controller.model.stub | 86 +++++++++++++++++++ 2 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/Illuminate/Routing/Console/stubs/controller.model.stub diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index d61932731451..e16666045ff0 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -3,6 +3,7 @@ namespace Illuminate\Routing\Console; use Illuminate\Console\GeneratorCommand; +use Illuminate\Support\Str; use Symfony\Component\Console\Input\InputOption; class ControllerMakeCommand extends GeneratorCommand @@ -35,7 +36,9 @@ class ControllerMakeCommand extends GeneratorCommand */ protected function getStub() { - if ($this->option('resource')) { + if ($this->option('model')) { + return __DIR__ . '/stubs/controller.model.stub'; + } elseif ($this->option('resource')) { return __DIR__.'/stubs/controller.stub'; } @@ -62,9 +65,40 @@ protected function getOptions() { return [ ['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'], + ['model', 'm', InputOption::VALUE_OPTIONAL, 'Use specified model.'], ]; } + /** + * Parses the model namespace + * + * @param string $modelNamespace + * @return string + */ + protected function parseModel($modelNamespace) + { + if (preg_match('([^A-Za-z0-9_/\\\\])', $modelNamespace)) { + $this->line(""); + $this->error(" "); + $this->error(" Model name contains invalid characters "); + $this->error(" "); + exit(1); + } + + if (Str::contains($modelNamespace, '/')) { + $modelNamespace = str_replace('/', '\\', $modelNamespace); + } + + $modelNamespace = trim($modelNamespace, '\\'); + $rootNamespace = $this->laravel->getNamespace(); + + if (!Str::startsWith($modelNamespace, $rootNamespace)) { + $modelNamespace = $rootNamespace . $modelNamespace; + } + + return $modelNamespace; + } + /** * Build the class with the given name. * @@ -75,8 +109,21 @@ protected function getOptions() */ protected function buildClass($name) { - $namespace = $this->getNamespace($name); + $controllerNamespace = $this->getNamespace($name); + + $replace = []; + if ($this->option('model')) { + $modelNamespace = $this->parseModel($this->option('model')); + $modelClass = last(explode('\\', $modelNamespace)); + $replace = [ + 'DummyModelNamespace' => $modelNamespace, + 'DummyModelClass' => $modelClass, + 'DummyModelVariable' => lcfirst($modelClass), + ]; + } + + $replace["use {$controllerNamespace}\Controller;\n"] = ''; - return str_replace("use {$namespace}\Controller;\n", '', parent::buildClass($name)); + return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name)); } } diff --git a/src/Illuminate/Routing/Console/stubs/controller.model.stub b/src/Illuminate/Routing/Console/stubs/controller.model.stub new file mode 100644 index 000000000000..8dda3cf7d6fd --- /dev/null +++ b/src/Illuminate/Routing/Console/stubs/controller.model.stub @@ -0,0 +1,86 @@ + Date: Wed, 14 Dec 2016 00:39:34 +0200 Subject: [PATCH 2/2] fix style ci --- .../Routing/Console/ControllerMakeCommand.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index e16666045ff0..5eb92e1ca183 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -2,8 +2,8 @@ namespace Illuminate\Routing\Console; -use Illuminate\Console\GeneratorCommand; use Illuminate\Support\Str; +use Illuminate\Console\GeneratorCommand; use Symfony\Component\Console\Input\InputOption; class ControllerMakeCommand extends GeneratorCommand @@ -37,7 +37,7 @@ class ControllerMakeCommand extends GeneratorCommand protected function getStub() { if ($this->option('model')) { - return __DIR__ . '/stubs/controller.model.stub'; + return __DIR__.'/stubs/controller.model.stub'; } elseif ($this->option('resource')) { return __DIR__.'/stubs/controller.stub'; } @@ -70,7 +70,7 @@ protected function getOptions() } /** - * Parses the model namespace + * Parses the model namespace. * * @param string $modelNamespace * @return string @@ -78,10 +78,10 @@ protected function getOptions() protected function parseModel($modelNamespace) { if (preg_match('([^A-Za-z0-9_/\\\\])', $modelNamespace)) { - $this->line(""); - $this->error(" "); - $this->error(" Model name contains invalid characters "); - $this->error(" "); + $this->line(''); + $this->error(' '); + $this->error(' Model name contains invalid characters '); + $this->error(' '); exit(1); } @@ -92,8 +92,8 @@ protected function parseModel($modelNamespace) $modelNamespace = trim($modelNamespace, '\\'); $rootNamespace = $this->laravel->getNamespace(); - if (!Str::startsWith($modelNamespace, $rootNamespace)) { - $modelNamespace = $rootNamespace . $modelNamespace; + if (! Str::startsWith($modelNamespace, $rootNamespace)) { + $modelNamespace = $rootNamespace.$modelNamespace; } return $modelNamespace;