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

Commit 9c48756

Browse filesBrowse files
author
Iltar van der Berg
committed
Fixed the nullable support for php 7.1 and below
1 parent 09e4e49 commit 9c48756
Copy full SHA for 9c48756

File tree

Expand file treeCollapse file tree

3 files changed

+69
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+69
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ class ControllerResolver implements ControllerResolverInterface
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 ControllerResolverInterface
3544
public function __construct(LoggerInterface $logger = null)
3645
{
3746
$this->logger = $logger;
47+
48+
$this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
3849
}
3950

4051
/**
@@ -99,13 +110,20 @@ public function getArguments(Request $request, $controller)
99110
return $this->doGetArguments($request, $controller, $r->getParameters());
100111
}
101112

113+
/**
114+
* @param Request $request
115+
* @param callable $controller
116+
* @param \ReflectionParameter[] $parameters
117+
*
118+
* @return array The arguments to use when calling the action
119+
*/
102120
protected function doGetArguments(Request $request, $controller, array $parameters)
103121
{
104122
$attributes = $request->attributes->all();
105123
$arguments = array();
106124
foreach ($parameters as $param) {
107125
if (array_key_exists($param->name, $attributes)) {
108-
if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
126+
if ($this->supportsVariadic && $param->isVariadic() && is_array($attributes[$param->name])) {
109127
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
110128
} else {
111129
$arguments[] = $attributes[$param->name];
@@ -114,6 +132,8 @@ protected function doGetArguments(Request $request, $controller, array $paramete
114132
$arguments[] = $request;
115133
} elseif ($param->isDefaultValueAvailable()) {
116134
$arguments[] = $param->getDefaultValue();
135+
} elseif ($param->allowsNull()) {
136+
$arguments[] = null;
117137
} else {
118138
if (is_array($controller)) {
119139
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php
+29Lines 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

@@ -222,6 +223,34 @@ public function testCreateControllerCanReturnAnyCallable()
222223
$mock->getController($request);
223224
}
224225

226+
/**
227+
* @requires PHP 7.1
228+
*/
229+
public function testGetNullableArguments()
230+
{
231+
$resolver = new ControllerResolver();
232+
233+
$request = Request::create('/');
234+
$request->attributes->set('foo', 'foo');
235+
$request->attributes->set('bar', new \stdClass());
236+
$request->attributes->set('mandatory', 'mandatory');
237+
$controller = array(new NullableController(), 'action');
238+
$this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), $resolver->getArguments($request, $controller));
239+
}
240+
241+
/**
242+
* @requires PHP 7.1
243+
*/
244+
public function testGetNullableArgumentsWithDefaults()
245+
{
246+
$resolver = new ControllerResolver();
247+
248+
$request = Request::create('/');
249+
$request->attributes->set('mandatory', 'mandatory');
250+
$controller = array(new NullableController(), 'action');
251+
$this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller));
252+
}
253+
225254
protected function createControllerResolver(LoggerInterface $logger = null)
226255
{
227256
return new ControllerResolver($logger);
+19Lines 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 <fabien@symfony.com>
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.