Skip to content

Commit 1b3d41a

Browse files
committed
Rename to addValueObjectTag and add $autoExclude
1 parent f5fb1ee commit 1b3d41a

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* Make `#[AsTaggedItem]` repeatable
88
* Support `@>` as a shorthand for `!service_closure` in yaml files
9-
* Add `Definition::tagAsValueObject()` and `ContainerBuilder::findTaggedValueObjects()` for auto-discovering value-objects
9+
* Add `Definition::addValueObjectTag()` and `ContainerBuilder::findTaggedValueObjects()` for auto-discovering value-objects
1010

1111
7.2
1212
---

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,12 +1356,16 @@ public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false
13561356
*
13571357
* @return array<string, array> An array of tags with the tagged class as key, holding a list of attribute arrays
13581358
*/
1359-
public function findTaggedValueObject(string $name): array
1359+
public function findTaggedValueObject(string $name, bool $autoExclude = true): array
13601360
{
13611361
$this->usedTags[] = $name;
13621362
$tags = [];
13631363
foreach ($this->getDefinitions() as $id => $definition) {
13641364
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)]);
1368+
}
13651369
$tags[$id] = $definition->getTag($name);
13661370
}
13671371
}

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public function addTag(string $name, array $attributes = []): static
460460
*
461461
* @return $this
462462
*/
463-
public function tagAsValueObject(string $name, array $attributes = []): static
463+
public function addValueObjectTag(string $name, array $attributes = []): static
464464
{
465465
return $this->addTag($name, $attributes)
466466
->addTag('container.excluded', ['source' => \sprintf('by tag "%s"', $name)])

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,13 +1094,18 @@ public function testFindTaggedValueObject()
10941094
$builder = new ContainerBuilder();
10951095
$builder
10961096
->register('foo', 'Bar\FooClass')
1097-
->tagAsValueObject('foo', ['foo' => 'foo'])
1097+
->addTag('foo', ['foo' => 'foo'])
10981098
->addTag('bar', ['bar' => 'bar'])
10991099
->addTag('foo', ['foofoo' => 'foofoo'])
11001100
;
11011101

11021102
$expected = ['foo' => [['foo' => 'foo'], ['foofoo' => 'foofoo']]];
1103+
$this->assertSame($expected, $builder->findTaggedValueObject('foo', false));
1104+
$this->assertFalse($builder->getDefinition('foo')->hasTag('container.excluded'));
1105+
$this->assertFalse($builder->getDefinition('foo')->isAbstract());
11031106
$this->assertSame($expected, $builder->findTaggedValueObject('foo'));
1107+
$this->assertTrue($builder->getDefinition('foo')->hasTag('container.excluded'));
1108+
$this->assertTrue($builder->getDefinition('foo')->isAbstract());
11041109
}
11051110

11061111
public function testFindUnusedTags()

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

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

261-
public function testTagAsValueObject()
261+
public function testAddValueObjectTag()
262262
{
263263
$def = new Definition('stdClass');
264-
$def->tagAsValueObject('foo', ['bar' => true]);
264+
$def->addValueObjectTag('foo', ['bar' => true]);
265265
$this->assertSame([['bar' => true]], $def->getTag('foo'));
266266
$this->assertTrue($def->isAbstract());
267267
$this->assertSame([['source' => 'by tag "foo"']], $def->getTag('container.excluded'));

0 commit comments

Comments
 (0)