[DependencyInjection] Fixed variadic method parameter in autowired classes
This commit is contained in:
parent
e3dcde7b8a
commit
a7f63de414
|
@ -338,10 +338,11 @@ class AutowirePass implements CompilerPassInterface
|
||||||
$class = false;
|
$class = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$isVariadic = method_exists($parameter, 'isVariadic') && $parameter->isVariadic();
|
||||||
$methodArgumentsMetadata[] = array(
|
$methodArgumentsMetadata[] = array(
|
||||||
'class' => $class,
|
'class' => $class,
|
||||||
'isOptional' => $parameter->isOptional(),
|
'isOptional' => $parameter->isOptional(),
|
||||||
'defaultValue' => $parameter->isOptional() ? $parameter->getDefaultValue() : null,
|
'defaultValue' => ($parameter->isOptional() && !$isVariadic) ? $parameter->getDefaultValue() : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
|
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
|
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||||
|
@ -35,6 +36,23 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals('foo', (string) $container->getDefinition('bar')->getArgument(0));
|
$this->assertEquals('foo', (string) $container->getDefinition('bar')->getArgument(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 5.6
|
||||||
|
*/
|
||||||
|
public function testProcessVariadic()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->register('foo', Foo::class);
|
||||||
|
$definition = $container->register('fooVariadic', FooVariadic::class);
|
||||||
|
$definition->setAutowired(true);
|
||||||
|
|
||||||
|
$pass = new AutowirePass();
|
||||||
|
$pass->process($container);
|
||||||
|
|
||||||
|
$this->assertCount(1, $container->getDefinition('fooVariadic')->getArguments());
|
||||||
|
$this->assertEquals('foo', (string) $container->getDefinition('fooVariadic')->getArgument(0));
|
||||||
|
}
|
||||||
|
|
||||||
public function testProcessAutowireParent()
|
public function testProcessAutowireParent()
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\includes;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
|
||||||
|
|
||||||
|
class FooVariadic
|
||||||
|
{
|
||||||
|
public function __construct(Foo $foo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function bar(...$arguments)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue