Skip to content

Commit d8527f3

Browse files
committed
[DependencyInjection] Allow binding iterable and tagged services
1 parent 2b39ff3 commit d8527f3

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* deprecated support for short factories and short configurators in Yaml
1010
* deprecated `tagged` in favor of `tagged_iterator`
1111
* deprecated passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition`
12+
* added support for binding iterable and tagged services
1213

1314
4.3.0
1415
-----

Compiler/ResolveBindingsPass.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
15+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -120,8 +121,8 @@ protected function processValue($value, $isRoot = false)
120121
continue;
121122
}
122123

123-
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
124-
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, an instance of %s or an instance of %s, %s given.', $key, $this->currentId, Reference::class, Definition::class, \gettype($bindingValue)));
124+
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument) {
125+
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, an instance of %s or an instance of %s or an instance of %s, %s given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, \gettype($bindingValue)));
125126
}
126127
}
127128

Tests/Compiler/ResolveBindingsPassTest.php

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

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
16+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1617
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
1718
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -31,7 +32,10 @@ public function testProcess()
3132
{
3233
$container = new ContainerBuilder();
3334

34-
$bindings = [CaseSensitiveClass::class => new BoundArgument(new Reference('foo'))];
35+
$bindings = [
36+
CaseSensitiveClass::class => new BoundArgument(new Reference('foo')),
37+
'iterable $objects' => new BoundArgument(new TaggedIteratorArgument('tag.name'), true, BoundArgument::INSTANCEOF_BINDING),
38+
];
3539

3640
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
3741
$definition->setArguments([1 => '123']);
@@ -44,7 +48,7 @@ public function testProcess()
4448
$pass = new ResolveBindingsPass();
4549
$pass->process($container);
4650

47-
$this->assertEquals([new Reference('foo'), '123'], $definition->getArguments());
51+
$this->assertEquals([0 => new Reference('foo'), 1 => '123', 4 => new TaggedIteratorArgument('tag.name')], $definition->getArguments());
4852
$this->assertEquals([['setSensitiveClass', [new Reference('foo')]]], $definition->getMethodCalls());
4953
}
5054

Tests/Fixtures/NamedArgumentsDummy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class NamedArgumentsDummy
1111
{
12-
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $interface)
12+
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $interface, iterable $objects)
1313
{
1414
}
1515

0 commit comments

Comments
 (0)