Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Variadic controller params #17971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow variadic controller parameters to be resolved.
  • Loading branch information
Albin Kerouaton authored and fabpot committed Mar 1, 2016
commit f39afc85db19a1599a203056a6b0b0f31058da4b
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ protected function doGetArguments(Request $request, $controller, array $paramete
{
$attributes = $request->attributes->all();
$arguments = array();
$variadicAvailable = method_exists('\ReflectionMethod', 'isVariadic');
foreach ($parameters as $param) {
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)) {
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
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,22 @@ 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('/');
$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()
{
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

class VariadicController
{
public function action($foo,...$bar)
{
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.