Skip to content

Commit

Permalink
bug #41768 [DependencyInjection] Fix binding "iterable $foo" when usi…
Browse files Browse the repository at this point in the history
…ng the PHP-DSL (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[DependencyInjection] Fix binding "iterable $foo" when using the PHP-DSL

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #41689
| License       | MIT
| Doc PR        | -

Commits
-------

8451a14 [DependencyInjection] Fix binding "iterable $foo" when using the PHP-DSL
  • Loading branch information
nicolas-grekas committed Jun 23, 2021
2 parents 52ca881 + 8451a14 commit 08b1682
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function processValue($value, $isRoot = false)
}

if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, "%s", "%s", "%s" or ServiceLocatorArgument, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, \gettype($bindingValue)));
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected "%s", "%s", "%s", "%s" or null, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, ServiceLocatorArgument::class, \gettype($bindingValue)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait BindTrait
final public function bind(string $nameOrFqcn, $valueOrRef): self
{
$valueOrRef = static::processValue($valueOrRef, true);
if (!preg_match('/^(?:(?:array|bool|float|int|string)[ \t]*+)?\$/', $nameOrFqcn) && !$valueOrRef instanceof Reference) {
if (!preg_match('/^(?:(?:array|bool|float|int|string|iterable)[ \t]*+)?\$/', $nameOrFqcn) && !$valueOrRef instanceof Reference) {
throw new InvalidArgumentException(sprintf('Invalid binding for service "%s": named arguments must start with a "$", and FQCN must map to references. Neither applies to binding "%s".', $this->id, $nameOrFqcn));
}
$bindings = $this->definition->getBindings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Foo implements FooInterface, Sub\BarInterface
{
public function __construct($bar = null)
public function __construct($bar = null, iterable $foo)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ services:
- { name: t, a: b }
autowire: true
autoconfigure: true
arguments: ['@bar']
arguments: ['@bar', !tagged_iterator foo]
bar:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
public: true
tags:
- { name: t, a: b }
autowire: true
arguments: [null, !tagged_iterator foo]
calls:
- [setFoo, ['@bar']]

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
->autowire()
->tag('t', ['a' => 'b'])
->bind(Foo::class, ref('bar'))
->bind('iterable $foo', tagged_iterator('foo'))
->public();

$s->set(Foo::class)->args([ref('bar')])->public();
Expand Down

0 comments on commit 08b1682

Please sign in to comment.