Skip to content

Commit 684ca9f

Browse files
[DI] fix support for "!tagged_locator foo"
1 parent d3ad14b commit 684ca9f

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

Loader/Configurator/ContainerConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function tagged(string $tag, string $indexAttribute = null, string $defaultIndex
124124
/**
125125
* Creates a service locator by tag name.
126126
*/
127-
function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null): ServiceLocatorArgument
127+
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): ServiceLocatorArgument
128128
{
129129
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true));
130130
}

Loader/YamlFileLoader.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -726,25 +726,23 @@ private function resolveServices($value, $file, $isParameter = false)
726726
if (\in_array($value->getTag(), ['tagged', 'tagged_locator'], true)) {
727727
$forLocator = 'tagged_locator' === $value->getTag();
728728

729-
if (\is_string($argument) && $argument) {
730-
return new TaggedIteratorArgument($argument, null, null, $forLocator);
731-
}
732-
733729
if (\is_array($argument) && isset($argument['tag']) && $argument['tag']) {
734730
if ($diff = array_diff(array_keys($argument), ['tag', 'index_by', 'default_index_method'])) {
735731
throw new InvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by" and "default_index_method".', $value->getTag(), implode('"", "', $diff)));
736732
}
737733

738734
$argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator);
735+
} elseif (\is_string($argument) && $argument) {
736+
$argument = new TaggedIteratorArgument($argument, null, null, $forLocator);
737+
} else {
738+
throw new InvalidArgumentException(sprintf('"!%s" tags only accept a non empty string or an array with a key "tag" in "%s".', $value->getTag(), $file));
739+
}
739740

740-
if ($forLocator) {
741-
$argument = new ServiceLocatorArgument($argument);
742-
}
743-
744-
return $argument;
741+
if ($forLocator) {
742+
$argument = new ServiceLocatorArgument($argument);
745743
}
746744

747-
throw new InvalidArgumentException(sprintf('"!%s" tags only accept a non empty string or an array with a key "tag" in "%s".', $value->getTag(), $file));
745+
return $argument;
748746
}
749747
if ('service' === $value->getTag()) {
750748
if ($isParameter) {

Tests/Dumper/YamlDumperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public function testTaggedArguments()
104104
$container->register('foo_service', 'Foo')->addTag('foo');
105105
$container->register('foo_service_tagged_iterator', 'Bar')->addArgument($taggedIterator);
106106
$container->register('foo_service_tagged_locator', 'Bar')->addArgument(new ServiceLocatorArgument($taggedIterator));
107+
$container->register('bar_service_tagged_locator', 'Bar')->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo')));
107108

108109
$dumper = new YamlDumper($container);
109110
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_tagged_argument.yml', $dumper->dump());

Tests/Fixtures/yaml/services_with_tagged_argument.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ services:
1414
foo_service_tagged_locator:
1515
class: Bar
1616
arguments: [!tagged_locator { tag: foo, index_by: barfoo, default_index_method: foobar }]
17+
bar_service_tagged_locator:
18+
class: Bar
19+
arguments: [!tagged_locator foo]
1720
Psr\Container\ContainerInterface:
1821
alias: service_container
1922
public: false

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ public function testTaggedArgumentsWithIndex()
305305

306306
$taggedIterator = new TaggedIteratorArgument('foo', 'barfoo', 'foobar', true);
307307
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_service_tagged_locator')->getArgument(0));
308+
309+
$taggedIterator = new TaggedIteratorArgument('foo', null, null, true);
310+
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('bar_service_tagged_locator')->getArgument(0));
308311
}
309312

310313
public function testNameOnlyTagsAreAllowedAsString()

0 commit comments

Comments
 (0)