Skip to content

Commit 43bc95c

Browse files
BreyndotEchsefabpot
authored andcommitted
[DependencyInjection] Resolve container parameter used in index attribute of service tags
1 parent 8e08191 commit 43bc95c

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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
---

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;

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)