diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php index a96f981d07..cd6bb6fe8c 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php @@ -68,15 +68,17 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass throw new InvalidArgumentException(sprintf('Invalid service "%s": the value of argument "%s" of method "%s()" must be null, an instance of %s or an instance of %s, %s given.', $this->currentId, $key, $class !== $this->currentId ? $class.'::'.$method : $method, Reference::class, Definition::class, gettype($argument))); } + $typeFound = false; foreach ($parameters as $j => $p) { - if (ProxyHelper::getTypeHint($r, $p, true) === $key) { + if (!array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) { $resolvedArguments[$j] = $argument; - - continue 2; + $typeFound = true; } } - throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument type-hinted as "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key)); + if (!$typeFound) { + throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument type-hinted as "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key)); + } } if ($resolvedArguments !== $call[1]) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php index fac05f070b..fe681b41df 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass; use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy; +use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy; /** * @author Kévin Dunglas @@ -125,6 +126,32 @@ class ResolveNamedArgumentsPassTest extends TestCase $this->assertEquals(array(new Reference('foo'), '123'), $definition->getArguments()); } + + public function testResolvesMultipleArgumentsOfTheSameType() + { + $container = new ContainerBuilder(); + + $definition = $container->register(SimilarArgumentsDummy::class, SimilarArgumentsDummy::class); + $definition->setArguments(array(CaseSensitiveClass::class => new Reference('foo'), '$token' => 'qwerty')); + + $pass = new ResolveNamedArgumentsPass(); + $pass->process($container); + + $this->assertEquals(array(new Reference('foo'), 'qwerty', new Reference('foo')), $definition->getArguments()); + } + + public function testResolvePrioritizeNamedOverType() + { + $container = new ContainerBuilder(); + + $definition = $container->register(SimilarArgumentsDummy::class, SimilarArgumentsDummy::class); + $definition->setArguments(array(CaseSensitiveClass::class => new Reference('foo'), '$token' => 'qwerty', '$class1' => new Reference('bar'))); + + $pass = new ResolveNamedArgumentsPass(); + $pass->process($container); + + $this->assertEquals(array(new Reference('bar'), 'qwerty', new Reference('foo')), $definition->getArguments()); + } } class NoConstructor diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index ac73138a9d..d3adfe7e69 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -32,6 +32,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\DependencyInjection\Loader\ClosureLoader; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy; use Symfony\Component\DependencyInjection\TypedReference; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; @@ -1270,6 +1271,30 @@ class ContainerBuilderTest extends TestCase $this->assertSame('bar', $container->get('foo')->foo); } + + public function testArgumentsHaveHigherPriorityThanBindings() + { + $container = new ContainerBuilder(); + $container->register('class.via.bindings', CaseSensitiveClass::class)->setArguments(array( + 'via-bindings', + )); + $container->register('class.via.argument', CaseSensitiveClass::class)->setArguments(array( + 'via-argument', + )); + $container->register('foo', SimilarArgumentsDummy::class)->setPublic(true)->setBindings(array( + CaseSensitiveClass::class => new Reference('class.via.bindings'), + '$token' => '1234', + ))->setArguments(array( + '$class1' => new Reference('class.via.argument'), + )); + + $this->assertSame(array('service_container', 'class.via.bindings', 'class.via.argument', 'foo', 'Psr\Container\ContainerInterface', 'Symfony\Component\DependencyInjection\ContainerInterface'), $container->getServiceIds()); + + $container->compile(); + + $this->assertSame('via-argument', $container->get('foo')->class1->identifier); + $this->assertSame('via-bindings', $container->get('foo')->class2->identifier); + } } class FooClass diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php index 87b258429e..7c6f0ff4cb 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php @@ -13,4 +13,10 @@ namespace Symfony\Component\DependencyInjection\Tests\Fixtures; class CaseSensitiveClass { + public $identifier; + + public function __construct($identifier = null) + { + $this->identifier = $identifier; + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/SimilarArgumentsDummy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/SimilarArgumentsDummy.php new file mode 100644 index 0000000000..9be58cbb79 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/SimilarArgumentsDummy.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +class SimilarArgumentsDummy +{ + public $class1; + public $class2; + + public function __construct(CaseSensitiveClass $class1, string $token, CaseSensitiveClass $class2) + { + $this->class1 = $class1; + $this->class2 = $class2; + } +}