bug #19811 Fixed the nullable support for php 7.1 and below (2.7, 2.8, 3.0) (iltar)

This PR was merged into the 2.7 branch.

Discussion
----------

Fixed the nullable support for php 7.1 and below (2.7, 2.8, 3.0)

| Q             | A
| ------------- | ---
| Branch?       | 2.7, 2.8, 3.0
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/19784#issuecomment-243852825
| License       | MIT
| Doc PR        | ~

Fixes the nullable support for 2.7, 2.8 and 3.0, can probably be partially merged into 3.1 but not 100% sure.

/ping @fabpot

Commits
-------

9c48756 Fixed the nullable support for php 7.1 and below
This commit is contained in:
Fabien Potencier 2016-09-14 12:08:00 -07:00
commit ce73768d0f
3 changed files with 69 additions and 1 deletions

View File

@ -27,6 +27,15 @@ class ControllerResolver implements ControllerResolverInterface
{
private $logger;
/**
* If the ...$arg functionality is available.
*
* Requires at least PHP 5.6.0 or HHVM 3.9.1
*
* @var bool
*/
private $supportsVariadic;
/**
* Constructor.
*
@ -35,6 +44,8 @@ class ControllerResolver implements ControllerResolverInterface
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
}
/**
@ -99,13 +110,20 @@ class ControllerResolver implements ControllerResolverInterface
return $this->doGetArguments($request, $controller, $r->getParameters());
}
/**
* @param Request $request
* @param callable $controller
* @param \ReflectionParameter[] $parameters
*
* @return array The arguments to use when calling the action
*/
protected function doGetArguments(Request $request, $controller, array $parameters)
{
$attributes = $request->attributes->all();
$arguments = array();
foreach ($parameters as $param) {
if (array_key_exists($param->name, $attributes)) {
if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
if ($this->supportsVariadic && $param->isVariadic() && is_array($attributes[$param->name])) {
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
} else {
$arguments[] = $attributes[$param->name];
@ -114,6 +132,8 @@ class ControllerResolver implements ControllerResolverInterface
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
$arguments[] = $param->getDefaultValue();
} elseif ($param->allowsNull()) {
$arguments[] = null;
} else {
if (is_array($controller)) {
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\HttpKernel\Tests\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
use Symfony\Component\HttpFoundation\Request;
@ -222,6 +223,34 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
$mock->getController($request);
}
/**
* @requires PHP 7.1
*/
public function testGetNullableArguments()
{
$resolver = new ControllerResolver();
$request = Request::create('/');
$request->attributes->set('foo', 'foo');
$request->attributes->set('bar', new \stdClass());
$request->attributes->set('mandatory', 'mandatory');
$controller = array(new NullableController(), 'action');
$this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), $resolver->getArguments($request, $controller));
}
/**
* @requires PHP 7.1
*/
public function testGetNullableArgumentsWithDefaults()
{
$resolver = new ControllerResolver();
$request = Request::create('/');
$request->attributes->set('mandatory', 'mandatory');
$controller = array(new NullableController(), 'action');
$this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller));
}
protected function createControllerResolver(LoggerInterface $logger = null)
{
return new ControllerResolver($logger);

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;
class NullableController
{
public function action(?string $foo, ?\stdClass $bar, ?string $baz = 'value', $mandatory)
{
}
}