Skip to content

Commit bd21a08

Browse files
committed
Merge branch '2.8' into 3.1
* 2.8: [Form] Fix typo in doc comment [Config] Handle open_basedir restrictions in FileLocator [bugfix] [Console] Set `Input::$interactive` to `false` when command is executed with `--quiet` as verbosity level Use JSON_UNESCAPED_SLASHES for lint commands output Fixed collapsed ChoiceType options attributes Fixed the nullable support for php 7.1 and below
2 parents da4cd4e + f995ff1 commit bd21a08

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

Controller/ControllerResolver.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
2727
{
2828
private $logger;
2929

30+
/**
31+
* If the ...$arg functionality is available.
32+
*
33+
* Requires at least PHP 5.6.0 or HHVM 3.9.1
34+
*
35+
* @var bool
36+
*/
37+
private $supportsVariadic;
38+
3039
/**
3140
* Constructor.
3241
*
@@ -35,6 +44,8 @@ class ControllerResolver implements ArgumentResolverInterface, ControllerResolve
3544
public function __construct(LoggerInterface $logger = null)
3645
{
3746
$this->logger = $logger;
47+
48+
$this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
3849
}
3950

4051
/**
@@ -104,6 +115,12 @@ public function getArguments(Request $request, $controller)
104115
}
105116

106117
/**
118+
* @param Request $request
119+
* @param callable $controller
120+
* @param \ReflectionParameter[] $parameters
121+
*
122+
* @return array The arguments to use when calling the action
123+
*
107124
* @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
108125
*/
109126
protected function doGetArguments(Request $request, $controller, array $parameters)
@@ -114,7 +131,7 @@ protected function doGetArguments(Request $request, $controller, array $paramete
114131
$arguments = array();
115132
foreach ($parameters as $param) {
116133
if (array_key_exists($param->name, $attributes)) {
117-
if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
134+
if ($this->supportsVariadic && $param->isVariadic() && is_array($attributes[$param->name])) {
118135
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
119136
} else {
120137
$arguments[] = $attributes[$param->name];
@@ -123,6 +140,8 @@ protected function doGetArguments(Request $request, $controller, array $paramete
123140
$arguments[] = $request;
124141
} elseif ($param->isDefaultValueAvailable()) {
125142
$arguments[] = $param->getDefaultValue();
143+
} elseif ($param->allowsNull()) {
144+
$arguments[] = null;
126145
} else {
127146
if (is_array($controller)) {
128147
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);

Tests/Controller/ControllerResolverTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
16+
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
1617
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
1718
use Symfony\Component\HttpFoundation\Request;
1819

@@ -226,6 +227,34 @@ public function testCreateControllerCanReturnAnyCallable()
226227
$mock->getController($request);
227228
}
228229

230+
/**
231+
* @requires PHP 7.1
232+
*/
233+
public function testGetNullableArguments()
234+
{
235+
$resolver = new ControllerResolver();
236+
237+
$request = Request::create('/');
238+
$request->attributes->set('foo', 'foo');
239+
$request->attributes->set('bar', new \stdClass());
240+
$request->attributes->set('mandatory', 'mandatory');
241+
$controller = array(new NullableController(), 'action');
242+
$this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), $resolver->getArguments($request, $controller));
243+
}
244+
245+
/**
246+
* @requires PHP 7.1
247+
*/
248+
public function testGetNullableArgumentsWithDefaults()
249+
{
250+
$resolver = new ControllerResolver();
251+
252+
$request = Request::create('/');
253+
$request->attributes->set('mandatory', 'mandatory');
254+
$controller = array(new NullableController(), 'action');
255+
$this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller));
256+
}
257+
229258
protected function createControllerResolver(LoggerInterface $logger = null)
230259
{
231260
return new ControllerResolver($logger);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;
13+
14+
class NullableController
15+
{
16+
public function action(?string $foo, ?\stdClass $bar, ?string $baz = 'value', $mandatory)
17+
{
18+
}
19+
}

0 commit comments

Comments
 (0)