Allow variadic controller parameters to be resolved.
This commit is contained in:
parent
2a811427c0
commit
f39afc85db
@ -117,9 +117,14 @@ class ControllerResolver implements ControllerResolverInterface
|
|||||||
{
|
{
|
||||||
$attributes = $request->attributes->all();
|
$attributes = $request->attributes->all();
|
||||||
$arguments = array();
|
$arguments = array();
|
||||||
|
$variadicAvailable = method_exists('\ReflectionMethod', 'isVariadic');
|
||||||
foreach ($parameters as $param) {
|
foreach ($parameters as $param) {
|
||||||
if (array_key_exists($param->name, $attributes)) {
|
if (array_key_exists($param->name, $attributes)) {
|
||||||
$arguments[] = $attributes[$param->name];
|
if ($variadicAvailable && $param->isVariadic() && is_array($attributes[$param->name])) {
|
||||||
|
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
|
||||||
|
} else {
|
||||||
|
$arguments[] = $attributes[$param->name];
|
||||||
|
}
|
||||||
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
|
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
|
||||||
$arguments[] = $request;
|
$arguments[] = $request;
|
||||||
} elseif ($param->isDefaultValueAvailable()) {
|
} elseif ($param->isDefaultValueAvailable()) {
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\HttpKernel\Tests\Controller;
|
namespace Symfony\Component\HttpKernel\Tests\Controller;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
|
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
|
||||||
|
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
||||||
@ -196,6 +197,22 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
|
$this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 5.6
|
||||||
|
*/
|
||||||
|
public function testGetVariadicArguments()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
|
$param1 = new \stdClass();
|
||||||
|
$param2 = new \stdClass();
|
||||||
|
$request->attributes->set('foo', 'foo');
|
||||||
|
$request->attributes->set('bar', array($param1, $param2));
|
||||||
|
$controller = array(new VariadicController(), 'action');
|
||||||
|
$this->assertEquals(array('foo', $param1, $param2), $resolver->getArguments($request, $controller));
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateControllerCanReturnAnyCallable()
|
public function testCreateControllerCanReturnAnyCallable()
|
||||||
{
|
{
|
||||||
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
|
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;
|
||||||
|
|
||||||
|
class VariadicController
|
||||||
|
{
|
||||||
|
public function action($foo,...$bar)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user