Skip to content

Commit

Permalink
Merge branch 'ctrl-res-model' of https://github.com/alsofronie/framework
Browse files Browse the repository at this point in the history
 into alsofronie-ctrl-res-model
  • Loading branch information
taylorotwell committed Dec 15, 2016
2 parents 6e2f206 + 592e25f commit 5353926
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/Illuminate/Routing/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
{
//
}
}

0 comments on commit 5353926

Please sign in to comment.