Skip to content

[DependencyInjection] Resolve container parameter used in index attribute of service tags #54664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add a `ContainerBuilder::registerChild()` shortcut method for registering child definitions
* Add support for `key-type` in `XmlFileLoader`
* Enable non-empty parameters with `ParameterBag::cannotBeEmpty()` and `ContainerBuilder::parameterCannotBeEmpty()` methods
* Resolve parameters found in index attribute of service tags

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
$tagName = $tagName->getTag();
}

$parameterBag = $container->getParameterBag();
$i = 0;
$services = [];

Expand Down Expand Up @@ -81,8 +82,9 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
}

if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
$index = $attribute[$indexAttribute];
} elseif (null === $defaultIndex && $defaultPriorityMethod && $class) {
$index = $parameterBag->resolveValue($attribute[$indexAttribute]);
}
if (null === $index && null === $defaultIndex && $defaultPriorityMethod && $class) {
$defaultIndex = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
}
$decorated = $definition->getTag('container.decorator')[0]['id'] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,34 @@ public function testTaggedItemAttributes()
$this->assertSame(array_keys($expected), array_keys($services));
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
}

public function testResolveIndexedTags()
{
$container = new ContainerBuilder();
$container->setParameter('custom_param_service1', 'bar');
$container->setParameter('custom_param_service2', 'baz');
$container->setParameter('custom_param_service2_empty', '');
$container->setParameter('custom_param_service2_null', null);
$container->register('service1')->addTag('my_custom_tag', ['foo' => '%custom_param_service1%']);

$definition = $container->register('service2', BarTagClass::class);
$definition->addTag('my_custom_tag', ['foo' => '%custom_param_service2%', 'priority' => 100]);
$definition->addTag('my_custom_tag', ['foo' => '%custom_param_service2_empty%']);
$definition->addTag('my_custom_tag', ['foo' => '%custom_param_service2_null%']);

$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();

$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar');
$expected = [
'baz' => new TypedReference('service2', BarTagClass::class),
'bar' => new Reference('service1'),
'' => new TypedReference('service2', BarTagClass::class),
'bar_tab_class_with_defaultmethod' => new TypedReference('service2', BarTagClass::class),
];
$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
$this->assertSame(array_keys($expected), array_keys($services));
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
}
}

class PriorityTaggedServiceTraitImplementation
Expand Down