Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18d5af7

Browse files
[DependencyInjection] Fix binding tagged services to containers
1 parent b0eb6f3 commit 18d5af7

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

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\ServiceLocatorArgument;
1516
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Definition;
@@ -126,8 +127,8 @@ protected function processValue($value, $isRoot = false)
126127
continue;
127128
}
128129

129-
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument) {
130-
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)));
130+
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
131+
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)));
131132
}
132133
}
133134

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 9 additions & 1 deletion
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\ServiceLocatorArgument;
1617
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1718
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
1819
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
@@ -35,6 +36,7 @@ public function testProcess()
3536

3637
$bindings = [
3738
CaseSensitiveClass::class => new BoundArgument(new Reference('foo')),
39+
'Psr\Container\ContainerInterface $container' => new BoundArgument(new ServiceLocatorArgument([]), true, BoundArgument::INSTANCEOF_BINDING),
3840
'iterable $objects' => new BoundArgument(new TaggedIteratorArgument('tag.name'), true, BoundArgument::INSTANCEOF_BINDING),
3941
];
4042

@@ -49,7 +51,13 @@ public function testProcess()
4951
$pass = new ResolveBindingsPass();
5052
$pass->process($container);
5153

52-
$this->assertEquals([0 => new Reference('foo'), 1 => '123', 4 => new TaggedIteratorArgument('tag.name')], $definition->getArguments());
54+
$expected = [
55+
0 => new Reference('foo'),
56+
1 => '123',
57+
3 => new ServiceLocatorArgument([]),
58+
4 => new TaggedIteratorArgument('tag.name'),
59+
];
60+
$this->assertEquals($expected, $definition->getArguments());
5361
$this->assertEquals([['setSensitiveClass', [new Reference('foo')]]], $definition->getMethodCalls());
5462
}
5563

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, iterable $objects)
12+
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $container, iterable $objects)
1313
{
1414
}
1515

0 commit comments

Comments
 (0)