Skip to content

Commit

Permalink
Merge branch '5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 15, 2016
2 parents 0751ac6 + 33edf4d commit ccf9072
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 17 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG-5.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
- Don't report exceptions inside queue worker signal handler ([#16738](https://github.com/laravel/framework/pull/16738))
- Kill timed out queue worker process ([#16746](https://github.com/laravel/framework/pull/16746))
- Removed unnecessary check in `ScheduleRunCommand::fire()` ([#16752](https://github.com/laravel/framework/pull/16752))
- Improved performance of `Str::startsWith()` ([#16761](https://github.com/laravel/framework/pull/16761))
- Improved handling of numeric values in `Str::startsWith()` ([#16770](https://github.com/laravel/framework/pull/16770))

### Fixed
- Added file existance check to `AppNameCommand::replaceIn()` to fix [#16575](https://github.com/laravel/framework/pull/16575) ([#16592](https://github.com/laravel/framework/pull/16592))
Expand Down
71 changes: 57 additions & 14 deletions src/Illuminate/Routing/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Routing\Console;

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

Expand Down Expand Up @@ -35,7 +37,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 @@ -54,29 +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.'],
];
$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)
);
}

/**
* Build the class with the given name.
*
* Remove the base controller import if we are already in base namespace.
* Get the fully-qualified model class name.
*
* @param string $name
* @param string $model
* @return string
*/
protected function buildClass($name)
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}

$model = trim(str_replace('/', '\\', $model), '\\');

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

return $model;
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
$namespace = $this->getNamespace($name);
return [
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'],

return str_replace("use {$namespace}\Controller;\n", '', parent::buildClass($name));
['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'],
];
}
}
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 DummyFullModelClass;
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 \DummyFullModelClass $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function show(DummyModelClass $DummyModelVariable)
{
//
}

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

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

/**
* Remove the specified resource from storage.
*
* @param \DummyFullModelClass $DummyModelVariable
* @return \Illuminate\Http\Response
*/
public function destroy(DummyModelClass $DummyModelVariable)
{
//
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public function substituteImplicitBindings($route)
! $route->getParameter($parameter->name) instanceof Model) {
$method = $parameter->isDefaultValueAvailable() ? 'first' : 'firstOrFail';

$model = $class->newInstance();
$model = $this->container->make($class->name);

$route->setParameter(
$parameter->name, $model->where(
Expand Down
23 changes: 23 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,25 @@ public function testImplicitBindingsWithOptionalParameter()
$router->dispatch(Request::create('bar', 'GET'))->getContent();
}

public function testImplicitBindingThroughIOC()
{
$phpunit = $this;
$container = new Container;
$router = new Router(new Dispatcher, $container);
$container->singleton(Registrar::class, function () use ($router) {
return $router;
});

$container->bind('RoutingTestUserModel', 'RoutingTestExtendedUserModel');
$router->get('foo/{bar}', [
'middleware' => SubstituteBindings::class,
'uses' => function (RoutingTestUserModel $bar) use ($phpunit) {
$phpunit->assertInstanceOf(RoutingTestExtendedUserModel::class, $bar);
},
]);
$router->dispatch(Request::create('foo/baz', 'GET'))->getContent();
}

public function testDispatchingCallableActionClasses()
{
$router = $this->getRouter();
Expand Down Expand Up @@ -1402,6 +1421,10 @@ public function firstOrFail()
}
}

class RoutingTestExtendedUserModel extends RoutingTestUserModel
{
}

class ActionStub
{
public function __invoke()
Expand Down

0 comments on commit ccf9072

Please sign in to comment.