Skip to content

[DependencyInjection] Add Definition::addExcludedTag() and ContainerBuilder::findExcludedServiceIds() for auto-discovering value-objects #59704

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
Feb 11, 2025
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Make `#[AsTaggedItem]` repeatable
* Support `@>` as a shorthand for `!service_closure` in yaml files
* Don't skip classes with private constructor when autodiscovering
* Add `Definition::addExcludeTag()` and `ContainerBuilder::findExcludedServiceIds()`
for auto-configuration of classes excluded from the service container

7.2
---
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,38 @@ public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false
return $tags;
}

/**
* Returns service ids for a given tag, asserting they have the "container.excluded" tag.
*
* Example:
*
* $container->register('foo')->addExcludeTag('my.tag', ['hello' => 'world'])
*
* $serviceIds = $container->findExcludedServiceIds('my.tag');
* foreach ($serviceIds as $serviceId => $tags) {
* foreach ($tags as $tag) {
* echo $tag['hello'];
* }
* }
*
* @return array<string, array> An array of tags with the tagged service as key, holding a list of attribute arrays
*/
public function findExcludedServiceIds(string $tagName): array
{
$this->usedTags[] = $tagName;
$tags = [];
foreach ($this->getDefinitions() as $id => $definition) {
if ($definition->hasTag($tagName)) {
if (!$definition->hasTag('container.excluded')) {
throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" is missing the "container.excluded" tag.', $id, $tagName));
}
$tags[$id] = $definition->getTag($tagName);
}
}

return $tags;
}

/**
* Returns all tags the defined services use.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,20 @@ public function addTag(string $name, array $attributes = []): static
return $this;
}

/**
* Adds a tag to the definition and marks it as excluded.
*
* These definitions should be processed using {@see ContainerBuilder::findExcludedServiceIds()}
*
* @return $this
*/
public function addExcludeTag(string $name, array $attributes = []): static
{
return $this->addTag($name, $attributes)
->addTag('container.excluded', ['source' => \sprintf('by tag "%s"', $name)])
->setAbstract(true);
}

/**
* Whether this definition has a tag with the given name.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,20 +1062,18 @@ public function testMergeLogicException()
$container->merge(new ContainerBuilder());
}

public function testfindTaggedServiceIds()
public function testFindTaggedServiceIds()
{
$builder = new ContainerBuilder();
$builder
->register('foo', 'Bar\FooClass')
$builder->register('foo', 'Bar\FooClass')
->setAbstract(true)
->addTag('foo', ['foo' => 'foo'])
->addTag('bar', ['bar' => 'bar'])
->addTag('foo', ['foofoo' => 'foofoo'])
;
$builder
->register('bar', 'Bar\FooClass')
->addTag('foo', ['foofoo' => 'foofoo']);
$builder->register('bar', 'Bar\FooClass')
->addTag('foo')
->addTag('container.excluded')
;
->addTag('container.excluded');

$this->assertEquals([
'foo' => [
['foo' => 'foo'],
Expand All @@ -1085,6 +1083,45 @@ public function testfindTaggedServiceIds()
$this->assertEquals([], $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
}

public function testFindTaggedServiceIdsThrowsWhenAbstract()
{
$builder = new ContainerBuilder();
$builder->register('foo', 'Bar\FooClass')
->setAbstract(true)
->addTag('foo', ['foo' => 'foo']);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The service "foo" tagged "foo" must not be abstract.');
$builder->findTaggedServiceIds('foo', true);
}

public function testFindExcludedServiceIds()
{
$builder = new ContainerBuilder();
$builder->register('myservice', 'Bar\FooClass')
->addTag('foo', ['foo' => 'foo'])
->addTag('bar', ['bar' => 'bar'])
->addTag('foo', ['foofoo' => 'foofoo'])
->addExcludeTag('container.excluded');

$expected = ['myservice' => [['foo' => 'foo'], ['foofoo' => 'foofoo']]];
$this->assertSame($expected, $builder->findExcludedServiceIds('foo'));
$this->assertSame([], $builder->findExcludedServiceIds('foofoo'));
}

public function testFindExcludedServiceIdsThrowsWhenNotExcluded()
{
$builder = new ContainerBuilder();
$builder->register('myservice', 'Bar\FooClass')
->addTag('foo', ['foo' => 'foo'])
->addTag('bar', ['bar' => 'bar'])
->addTag('foo', ['foofoo' => 'foofoo']);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The service "myservice" tagged "foo" is missing the "container.excluded" tag.');
$builder->findExcludedServiceIds('foo', true);
}

public function testFindUnusedTags()
{
$builder = new ContainerBuilder();
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ public function testTags()
], $def->getTags(), '->getTags() returns all tags');
}

public function testAddExcludeTag()
{
$def = new Definition('stdClass');
$def->addExcludeTag('foo', ['bar' => true]);

$this->assertSame([['bar' => true]], $def->getTag('foo'));
$this->assertTrue($def->isAbstract());
$this->assertSame([['source' => 'by tag "foo"']], $def->getTag('container.excluded'));
}

public function testSetArgument()
{
$def = new Definition('stdClass');
Expand Down
Loading