Skip to content

Commit

Permalink
bug #17971 Variadic controller params (NiR-, fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

Variadic controller params

| Q             | A
| ------------- | ---
| Branch        | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15777
| License       | MIT

Commits
-------

bedcb15 simplified code
f39afc8 Allow variadic controller parameters to be resolved.
  • Loading branch information
fabpot committed Mar 1, 2016
2 parents 2a81142 + bedcb15 commit 94a8736
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Expand Up @@ -119,7 +119,11 @@ protected function doGetArguments(Request $request, $controller, array $paramete
$arguments = array();
foreach ($parameters as $param) {
if (array_key_exists($param->name, $attributes)) {
$arguments[] = $attributes[$param->name];
if (PHP_VERSION_ID >= 50600 && $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)) {
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\Controller;

use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
use Symfony\Component\HttpFoundation\Request;

class ControllerResolverTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -196,6 +197,20 @@ public function testGetArguments()
$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('/');
$request->attributes->set('foo', 'foo');
$request->attributes->set('bar', array('foo', 'bar'));
$controller = array(new VariadicController(), 'action');
$this->assertEquals(array('foo', 'foo', 'bar'), $resolver->getArguments($request, $controller));
}

public function testCreateControllerCanReturnAnyCallable()
{
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
Expand Down
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;

class VariadicController
{
public function action($foo, ...$bar)
{
}
}

0 comments on commit 94a8736

Please sign in to comment.