Skip to content

Commit 92ef476

Browse files
feature #33623 [DependencyInjection] Allow binding iterable and tagged services (lyrixx)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection] Allow binding iterable and tagged services | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | This will allow: ```yaml services: _defaults: bind: iterable $rules: !tagged_iterator app.foo.rule _instanceof: App\Foo\Rule\RuleInterface: tags: ['app.foo.rule'] ``` Commits ------- 2055a55 [DependencyInjection] Allow binding iterable and tagged services
2 parents 1efae63 + 2055a55 commit 92ef476

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

src/Symfony/Component/DependencyInjection/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
-----

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy