Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Generate resource controller with type-hinted model instead of just id #16787

Merged
merged 2 commits into from Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 50 additions & 3 deletions src/Illuminate/Routing/Console/ControllerMakeCommand.php
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Routing\Console;

use Illuminate\Support\Str;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

Expand Down Expand Up @@ -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';
}

Expand All @@ -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.
*
Expand All @@ -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));
}
}
86 changes: 86 additions & 0 deletions src/Illuminate/Routing/Console/stubs/controller.model.stub
@@ -0,0 +1,86 @@
<?php

namespace DummyNamespace;

use DummyModelNamespace;
use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;

class DummyClass extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param \DummyModelNamespace $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function show(DummyModelClass $DummyModelVariable)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param \DummyModelNamespace $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function edit(DummyModelClass $DummyModelVariable)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \DummyModelNamespace $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function update(Request $request, DummyModelClass $DummyModelVariable)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param \DummyModelNamespace $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function destroy(DummyModelClass $DummyModelVariable)
{
//
}
}