Skip to content

Commit

Permalink
cleaning and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 15, 2016
1 parent 5353926 commit 59a1ce2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
88 changes: 42 additions & 46 deletions src/Illuminate/Routing/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Routing\Console;

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

Expand Down Expand Up @@ -57,73 +58,68 @@ protected function getDefaultNamespace($rootNamespace)
}

/**
* Get the console command options.
* Build the class with the given name.
*
* @return array
* Remove the base controller import if we are already in base namespace.
*
* @param string $name
* @return string
*/
protected function getOptions()
protected function buildClass($name)
{
return [
['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'],
['model', 'm', InputOption::VALUE_OPTIONAL, 'Use specified model.'],
];
$controllerNamespace = $this->getNamespace($name);

$replace = [];

if ($this->option('model')) {
$modelClass = $this->parseModel($this->option('model'));

$replace = [
'DummyFullModelClass' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
];
}

$replace["use {$controllerNamespace}\Controller;\n"] = '';

return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);
}

/**
* Parses the model namespace.
* Get the fully-qualified model class name.
*
* @param string $modelNamespace
* @param string $model
* @return string
*/
protected function parseModel($modelNamespace)
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $modelNamespace)) {
$this->line('');
$this->error(' ');
$this->error(' Model name contains invalid characters ');
$this->error(' ');
exit(1);
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}

if (Str::contains($modelNamespace, '/')) {
$modelNamespace = str_replace('/', '\\', $modelNamespace);
}

$modelNamespace = trim($modelNamespace, '\\');
$rootNamespace = $this->laravel->getNamespace();
$model = trim(str_replace('/', '\\', $model), '\\');

if (! Str::startsWith($modelNamespace, $rootNamespace)) {
$modelNamespace = $rootNamespace.$modelNamespace;
if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) {
$model = $rootNamespace.$model;
}

return $modelNamespace;
return $model;
}

/**
* Build the class with the given name.
*
* Remove the base controller import if we are already in base namespace.
* Get the console command options.
*
* @param string $name
* @return string
* @return array
*/
protected function buildClass($name)
protected function getOptions()
{
$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 [
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'],

return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name));
['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'],
];
}
}
10 changes: 5 additions & 5 deletions src/Illuminate/Routing/Console/stubs/controller.model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DummyNamespace;

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

Expand Down Expand Up @@ -42,7 +42,7 @@ class DummyClass extends Controller
/**
* Display the specified resource.
*
* @param \DummyModelNamespace $DummyModelVariable
* @param \DummyFullModelClass $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function show(DummyModelClass $DummyModelVariable)
Expand All @@ -53,7 +53,7 @@ class DummyClass extends Controller
/**
* Show the form for editing the specified resource.
*
* @param \DummyModelNamespace $DummyModelVariable
* @param \DummyFullModelClass $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function edit(DummyModelClass $DummyModelVariable)
Expand All @@ -65,7 +65,7 @@ class DummyClass extends Controller
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \DummyModelNamespace $DummyModelVariable
* @param \DummyFullModelClass $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function update(Request $request, DummyModelClass $DummyModelVariable)
Expand All @@ -76,7 +76,7 @@ class DummyClass extends Controller
/**
* Remove the specified resource from storage.
*
* @param \DummyModelNamespace $DummyModelVariable
* @param \DummyFullModelClass $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function destroy(DummyModelClass $DummyModelVariable)
Expand Down

0 comments on commit 59a1ce2

Please sign in to comment.