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
-------
2055a558fe
[DependencyInjection] Allow binding iterable and tagged services
This commit is contained in:
commit
92ef476487
@ -9,6 +9,7 @@ CHANGELOG
|
||||
* deprecated support for short factories and short configurators in Yaml
|
||||
* deprecated `tagged` in favor of `tagged_iterator`
|
||||
* deprecated passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition`
|
||||
* added support for binding iterable and tagged services
|
||||
|
||||
4.3.0
|
||||
-----
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
@ -120,8 +121,8 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
||||
continue;
|
||||
}
|
||||
|
||||
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
|
||||
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)));
|
||||
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument) {
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
|
||||
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
@ -31,7 +32,10 @@ class ResolveBindingsPassTest extends TestCase
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$bindings = [CaseSensitiveClass::class => new BoundArgument(new Reference('foo'))];
|
||||
$bindings = [
|
||||
CaseSensitiveClass::class => new BoundArgument(new Reference('foo')),
|
||||
'iterable $objects' => new BoundArgument(new TaggedIteratorArgument('tag.name'), true, BoundArgument::INSTANCEOF_BINDING),
|
||||
];
|
||||
|
||||
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
|
||||
$definition->setArguments([1 => '123']);
|
||||
@ -44,7 +48,7 @@ class ResolveBindingsPassTest extends TestCase
|
||||
$pass = new ResolveBindingsPass();
|
||||
$pass->process($container);
|
||||
|
||||
$this->assertEquals([new Reference('foo'), '123'], $definition->getArguments());
|
||||
$this->assertEquals([0 => new Reference('foo'), 1 => '123', 4 => new TaggedIteratorArgument('tag.name')], $definition->getArguments());
|
||||
$this->assertEquals([['setSensitiveClass', [new Reference('foo')]]], $definition->getMethodCalls());
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ use Psr\Container\ContainerInterface;
|
||||
*/
|
||||
class NamedArgumentsDummy
|
||||
{
|
||||
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $interface)
|
||||
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $interface, iterable $objects)
|
||||
{
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user