8000 bug #17971 Variadic controller params (NiR-, fabpot) · symfony/symfony@94a8736 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94a8736

Browse files
committed
bug #17971 Variadic controller params (NiR-, fabpot)
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.
2 parents 2a81142 + bedcb15 commit 94a8736

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ protected function doGetArguments(Request $request, $controller, array $paramete
119119
$arguments = array();
120120
foreach ($parameters as $param) {
121121
if (array_key_exists($param->name, $attributes)) {
122-
$arguments[] = $attributes[$param->name];
122+
if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
123+
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
124+
} else {
125+
$arguments[] = $attributes[$param->name];
126+
}
123127
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
124128
$arguments[] = $request;
125129
} elseif ($param->isDefaultValueAvailable()) {

src/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Tests\Controller;
1313

1414
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
15+
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
1516
use Symfony\Component\HttpFoundation\Request;
1617

1718
class ControllerResolverTest extends \PHPUnit_Framework_TestCase
@@ -196,6 +197,20 @@ public function testGetArguments()
196197
$this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
197198
}
198199

200+
/**
201+
* @requires PHP 5.6
202+
*/
203+
public function testGetVariadicArguments()
204+
{
205+
$resolver = new ControllerResolver();
206+
207+
$request = Request::create('/');
208+
$request->attributes->set('foo', 'foo');
209+
$request->attributes->set('bar', array('foo', 'bar'));
210+
$controller = array(new VariadicController(), 'action');
211+
$this->assertEquals(array('foo', 'foo', 'bar'), $resolver->getArguments($request, $controller));
212+
}
213+
199214
public function testCreateControllerCanReturnAnyCallable()
200215
{
201216
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;
4+
5+
class VariadicController
6+
{
7+
public function action($foo, ...$bar)
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)
0