Skip to content

Commit 83beaba

Browse files
committed
feature symfony#54664 [DependencyInjection] Resolve container parameter used in index attribute of service tags (Marvin Feldmann)
This PR was merged into the 7.2 branch. Discussion ---------- [DependencyInjection] Resolve container parameter used in index attribute of service tags | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT Resolve tags' index attribute to allow the use of container parameters. YAML config example: ```yaml parameters: foo.bar_reference1: !php/const:App\Service\Foo::EXAMPLE_REFERENCE1 foo.bar_reference2: !php/const:App\Service\Foo::EXAMPLE_REFERENCE2 services: App\Service\Foo: tags: - { name: 'bar.reference', key: '%foo.bar_reference1%' } - { name: 'bar.reference', key: '%foo.bar_reference2%' } App\Service\Bar: arguments: - !tagged_locator { tag: 'bar.reference', index_by: 'key' } ``` XML config example: ```xml <?xml version="1.0" encoding="UTF-8" ?> <container [...]> <parameters> <parameter key="foo.bar_reference1" type="constant">App\Service\Foo::EXAMPLE_REFERENCE1</parameter> <parameter key="foo.bar_reference2" type="constant">App\Service\Foo::EXAMPLE_REFERENCE2</parameter> </parameters> <services> <service id="App\Service\Foo"> <tag name="bar.reference" key="%foo.bar_reference1%" /> <tag name="bar.reference" key="%foo.bar_reference2%" /> </service> <service id="App\Service\Bar"> <argument type="tagged_locator" tag="bar.reference" index-by="key" /> </service> </services> </container> ``` Commits ------- b37e3ee [DependencyInjection] Resolve container parameter used in index attribute of service tags
2 parents 307ad5f + b37e3ee commit 83beaba

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add a `ContainerBuilder::registerChild()` shortcut method for registering child definitions
99
* Add support for `key-type` in `XmlFileLoader`
1010
* Enable non-empty parameters with `ParameterBag::cannotBeEmpty()` and `ContainerBuilder::parameterCannotBeEmpty()` methods
11+
* Resolve parameters found in index attribute of service tags
1112

1213
7.1
1314
---

src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
5050
$tagName = $tagName->getTag();
5151
}
5252

53+
$parameterBag = $container->getParameterBag();
5354
$i = 0;
5455
$services = [];
5556

@@ -81,8 +82,9 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
8182
}
8283

8384
if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
84-
$index = $attribute[$indexAttribute];
85-
} elseif (null === $defaultIndex && $defaultPriorityMethod && $class) {
85+
$index = $parameterBag->resolveValue($attribute[$indexAttribute]);
86+
}
87+
if (null === $index && null === $defaultIndex && $defaultPriorityMethod && $class) {
8688
$defaultIndex = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
8789
}
8890
$decorated = $definition->getTag('container.decorator')[0]['id'] ?? null;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ public function testTaggedItemAttributes()
233233
$this->assertSame(array_keys($expected), array_keys($services));
234234
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
235235
}
236+
237+
public function testResolveIndexedTags()
238+
{
239+
$container = new ContainerBuilder();
240+
$container->setParameter('custom_param_service1', 'bar');
241+
$container->setParameter('custom_param_service2', 'baz');
242+
$container->setParameter('custom_param_service2_empty', '');
243+
$container->setParameter('custom_param_service2_null', null);
244+
$container->register('service1')->addTag('my_custom_tag', ['foo' => '%custom_param_service1%']);
245+
246+
$definition = $container->register('service2', BarTagClass::class);
247+
$definition->addTag('my_custom_tag', ['foo' => '%custom_param_service2%', 'priority' => 100]);
248+
$definition->addTag('my_custom_tag', ['foo' => '%custom_param_service2_empty%']);
249+
$definition->addTag('my_custom_tag', ['foo' => '%custom_param_service2_null%']);
250+
251+
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
252+
253+
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar');
254+
$expected = [
255+
'baz' => new TypedReference('service2', BarTagClass::class),
256+
'bar' => new Reference('service1'),
257+
'' => new TypedReference('service2', BarTagClass::class),
258+
'bar_tab_class_with_defaultmethod' => new TypedReference('service2', BarTagClass::class),
259+
];
260+
$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
261+
$this->assertSame(array_keys($expected), array_keys($services));
262+
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
263+
}
236264
}
237265

238266
class PriorityTaggedServiceTraitImplementation

0 commit comments

Comments
 (0)