Skip to content

Commit b949891

Browse files
committed
[DependencyInjection] Option to accept excluded tags in findTaggedServiceIds
1 parent 04551ad commit b949891

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Make `#[AsTaggedItem]` repeatable
88
* Support `@>` as a shorthand for `!service_closure` in yaml files
9+
* Add `ContainerBuilder::findTaggedServiceIds()` 3rd argument to return excluded services
910

1011
7.2
1112
---

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -1333,14 +1333,21 @@ private function doResolveServices(mixed $value, array &$inlineServices = [], bo
13331333
* }
13341334
* }
13351335
*
1336+
* @param bool $notExcluded Whether to include or exclude services with the "container.excluded" tag
1337+
*
13361338
* @return array<string, array> An array of tags with the tagged service as key, holding a list of attribute arrays
13371339
*/
1338-
public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false): array
1340+
public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false /*, bool $notExcluded = true */): array
13391341
{
1342+
$notExcluded = \func_num_args() > 2 ? func_get_arg(2) : true;
1343+
if (!\is_bool($notExcluded)) {
1344+
throw new \TypeError(\sprintf('Argument 3 passed to %s() must be of the type bool, %s given.', __METHOD__, get_debug_type($notExcluded)));
1345+
}
1346+
13401347
$this->usedTags[] = $name;
13411348
$tags = [];
13421349
foreach ($this->getDefinitions() as $id => $definition) {
1343-
if ($definition->hasTag($name) && !$definition->hasTag('container.excluded')) {
1350+
if ($definition->hasTag($name) && (!$notExcluded || !$definition->hasTag('container.excluded'))) {
13441351
if ($throwOnAbstract && $definition->isAbstract()) {
13451352
throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" must not be abstract.', $id, $name));
13461353
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ public function testfindTaggedServiceIds()
10671067
$builder = new ContainerBuilder();
10681068
$builder
10691069
->register('foo', 'Bar\FooClass')
1070+
->setAbstract(true)
10701071
->addTag('foo', ['foo' => 'foo'])
10711072
->addTag('bar', ['bar' => 'bar'])
10721073
->addTag('foo', ['foofoo' => 'foofoo'])
@@ -1083,6 +1084,17 @@ public function testfindTaggedServiceIds()
10831084
],
10841085
], $builder->findTaggedServiceIds('foo'), '->findTaggedServiceIds() returns an array of service ids and its tag attributes');
10851086
$this->assertEquals([], $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
1087+
$this->assertEquals([
1088+
'foo' => [
1089+
['foo' => 'foo'],
1090+
['foofoo' => 'foofoo'],
1091+
],
1092+
'bar' => [[]],
1093+
], $builder->findTaggedServiceIds('foo', false, false), '->findTaggedServiceIds() car include excluded services');
1094+
1095+
$this->expectException(InvalidArgumentException::class);
1096+
$this->expectExceptionMessage('The service "foo" tagged "foo" must not be abstract.');
1097+
$builder->findTaggedServiceIds('foo', true);
10861098
}
10871099

10881100
public function testFindUnusedTags()

0 commit comments

Comments
 (0)