Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cedb37

Browse files
committed
Rename to Definition::addExcludeTag() and ContainerBuilder::findExcludedServiceIds()
1 parent 03c92d8 commit 6cedb37

File tree

5 files changed

+61
-33
lines changed

5 files changed

+61
-33
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ CHANGELOG
77
* Make `#[AsTaggedItem]` repeatable
88
* Support `@>` as a shorthand for `!service_closure` in yaml files
99
* Don't skip classes with private constructor when autodiscovering
10-
* Add `ContainerBuilder::findTaggedValueClasses()` for auto-discovering value-object classes
10+
* Add `Definition::addExcludeTag()` and `ContainerBuilder::findExcludedServiceIds()`
11+
for auto-configuration of classes excluded from the service container.
1112

1213
7.2
1314
---

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,21 +1352,31 @@ public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false
13521352
}
13531353

13541354
/**
1355-
* Finds definitions of value-objects by tag.
1355+
* Returns service ids for a given tag, asserting they have the "container.excluded" tag.
13561356
*
1357-
* @return array<string, array> An array of tags with the tagged class as key, holding a list of attribute arrays
1357+
* Example:
1358+
*
1359+
* $container->register('foo')->addExcludeTag('my.tag', ['hello' => 'world'])
1360+
*
1361+
* $serviceIds = $container->findExcludedServiceIds('my.tag');
1362+
* foreach ($serviceIds as $serviceId => $tags) {
1363+
* foreach ($tags as $tag) {
1364+
* echo $tag['hello'];
1365+
* }
1366+
* }
1367+
*
1368+
* @return array<string, array> An array of tags with the tagged service as key, holding a list of attribute arrays
13581369
*/
1359-
public function findTaggedValueObjects(string $name, bool $autoExclude = true): array
1370+
public function findExcludedServiceIds(string $tagName): array
13601371
{
1361-
$this->usedTags[] = $name;
1372+
$this->usedTags[] = $tagName;
13621373
$tags = [];
13631374
foreach ($this->getDefinitions() as $id => $definition) {
1364-
if ($definition->hasTag($name)) {
1365-
if ($autoExclude && !$definition->hasTag('container.excluded')) {
1366-
$definition->setAbstract(true)
1367-
->addTag('container.excluded', ['source' => \sprintf('by tag "%s"', $name)]);
1375+
if ($definition->hasTag($tagName)) {
1376+
if (!$definition->hasTag('container.excluded')) {
1377+
throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" must have the "container.excluded" tag.', $id, $tagName));
13681378
}
1369-
$tags[$id] = $definition->getTag($name);
1379+
$tags[$id] = $definition->getTag($tagName);
13701380
}
13711381
}
13721382

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,13 @@ public function addTag(string $name, array $attributes = []): static
456456
}
457457

458458
/**
459-
* Adds a tag to the definition and marks it as a value object.
459+
* Adds a tag to the definition and marks it as excluded.
460+
*
461+
* This definitions must be processed using {@see ContainerBuilder::findExcludedServiceIds()}
460462
*
461463
* @return $this
462464
*/
463-
public function addValueObjectTag(string $name, array $attributes = []): static
465+
public function addExcludeTag(string $name, array $attributes = []): static
464466
{
465467
return $this->addTag($name, $attributes)
466468
->addTag('container.excluded', ['source' => \sprintf('by tag "%s"', $name)])

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,47 +1065,61 @@ public function testMergeLogicException()
10651065
public function testFindTaggedServiceIds()
10661066
{
10671067
$builder = new ContainerBuilder();
1068-
$builder
1069-
->register('foo', 'Bar\FooClass')
1068+
$builder->register('foo', 'Bar\FooClass')
10701069
->setAbstract(true)
10711070
->addTag('foo', ['foo' => 'foo'])
10721071
->addTag('bar', ['bar' => 'bar'])
1073-
->addTag('foo', ['foofoo' => 'foofoo'])
1074-
;
1075-
$builder
1076-
->register('bar', 'Bar\FooClass')
1072+
->addTag('foo', ['foofoo' => 'foofoo']);
1073+
$builder->register('bar', 'Bar\FooClass')
10771074
->addTag('foo')
1078-
->addTag('container.excluded')
1079-
;
1075+
->addTag('container.excluded');
1076+
10801077
$this->assertEquals([
10811078
'foo' => [
10821079
['foo' => 'foo'],
10831080
['foofoo' => 'foofoo'],
10841081
],
10851082
], $builder->findTaggedServiceIds('foo'), '->findTaggedServiceIds() returns an array of service ids and its tag attributes');
1083+
}
1084+
1085+
public function testFindTaggedServiceIdsThrowsWhenAbstract()
1086+
{
1087+
$builder = new ContainerBuilder();
1088+
$builder->register('foo', 'Bar\FooClass')
1089+
->setAbstract(true)
1090+
->addTag('foo', ['foo' => 'foo']);
1091+
10861092
$this->assertEquals([], $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
10871093
$this->expectException(InvalidArgumentException::class);
10881094
$this->expectExceptionMessage('The service "foo" tagged "foo" must not be abstract.');
10891095
$builder->findTaggedServiceIds('foo', true);
10901096
}
10911097

1092-
public function testFindTaggedValueObjects()
1098+
public function testFindExcludedServiceIds()
10931099
{
10941100
$builder = new ContainerBuilder();
1095-
$builder
1096-
->register('foo', 'Bar\FooClass')
1101+
$builder->register('myservice', 'Bar\FooClass')
10971102
->addTag('foo', ['foo' => 'foo'])
10981103
->addTag('bar', ['bar' => 'bar'])
10991104
->addTag('foo', ['foofoo' => 'foofoo'])
1100-
;
1105+
->addExcludeTag('container.excluded');
1106+
1107+
$expected = ['myservice' => [['foo' => 'foo'], ['foofoo' => 'foofoo']]];
1108+
$this->assertSame($expected, $builder->findExcludedServiceIds('foo'));
1109+
$this->assertSame([], $builder->findExcludedServiceIds('foofoo'));
1110+
}
11011111

1102-
$expected = ['foo' => [['foo' => 'foo'], ['foofoo' => 'foofoo']]];
1103-
$this->assertSame($expected, $builder->findTaggedValueObjects('foo', false));
1104-
$this->assertFalse($builder->getDefinition('foo')->hasTag('container.excluded'));
1105-
$this->assertFalse($builder->getDefinition('foo')->isAbstract());
1106-
$this->assertSame($expected, $builder->findTaggedValueObjects('foo'));
1107-
$this->assertTrue($builder->getDefinition('foo')->hasTag('container.excluded'));
1108-
$this->assertTrue($builder->getDefinition('foo')->isAbstract());
1112+
public function testFindExcludedServiceIdsThrowsWhenNotExcluded()
1113+
{
1114+
$builder = new ContainerBuilder();
1115+
$builder->register('myservice', 'Bar\FooClass')
1116+
->addTag('foo', ['foo' => 'foo'])
1117+
->addTag('bar', ['bar' => 'bar'])
1118+
->addTag('foo', ['foofoo' => 'foofoo']);
1119+
1120+
$this->expectException(InvalidArgumentException::class);
1121+
$this->expectExceptionMessage('The service "myservice" tagged "foo" must have the "container.excluded" tag.');
1122+
$builder->findExcludedServiceIds('foo', true);
11091123
}
11101124

11111125
public function testFindUnusedTags()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,11 @@ public function testTags()
258258
], $def->getTags(), '->getTags() returns all tags');
259259
}
260260

261-
public function testAddValueObjectTag()
261+
public function testAddExcludeTag()
262262
{
263263
$def = new Definition('stdClass');
264-
$def->addValueObjectTag('foo', ['bar' => true]);
264+
$def->addExcludeTag('foo', ['bar' => true]);
265+
265266
$this->assertSame([['bar' => true]], $def->getTag('foo'));
266267
$this->assertTrue($def->isAbstract());
267268
$this->assertSame([['source' => 'by tag "foo"']], $def->getTag('container.excluded'));

0 commit comments

Comments
 (0)