Skip to content

Commit f39afc8

Browse files
Albin Kerouatonfabpot
authored andcommitted
Allow variadic controller parameters to be resolved.
1 parent 2a81142 commit f39afc8

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,14 @@ protected function doGetArguments(Request $request, $controller, array $paramete
117117
{
118118
$attributes = $request->attributes->all();
119119
$arguments = array();
120+
$variadicAvailable = method_exists('\ReflectionMethod', 'isVariadic');
120121
foreach ($parameters as $param) {
121122
if (array_key_exists($param->name, $attributes)) {
122-
$arguments[] = $attributes[$param->name];
123+
if ($variadicAvailable && $param->isVariadic() && is_array($attributes[$param->name])) {
124+
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
125+
} else {
126+
$arguments[] = $attributes[$param->name];
127+
}
123128
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
124129
$arguments[] = $request;
125130
} elseif ($param->isDefaultValueAvailable()) {

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

Lines changed: 17 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,22 @@ 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+
$param1 = new \stdClass();
209+
$param2 = new \stdClass();
210+
$request->attributes->set('foo', 'foo');
211+
$request->attributes->set('bar', array($param1, $param2));
212+
$controller = array(new VariadicController(), 'action');
213+
$this->assertEquals(array('foo', $param1, $param2), $resolver->getArguments($request, $controller));
214+
}
215+
199216
public function testCreateControllerCanReturnAnyCallable()
200217
{
201218
$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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy