Skip to content

[DependencyInjection] Add excludeSelf option to dumpers #50351

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
May 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ private function convertParameters(array $parameters, string $type, \DOMElement
}
}
}
if (!$tag->excludeSelf()) {
$element->setAttribute('exclude-self', 'false');
}
} elseif ($value instanceof IteratorArgument) {
$element->setAttribute('type', 'iterator');
$this->convertParameters($value->getValues(), $type, $element, 'key');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ private function dumpValue(mixed $value): mixed
}
$content['exclude'] = 1 === \count($excludes) ? $excludes[0] : $excludes;
}
if (!$tag->excludeSelf()) {
$content['exclude_self'] = false;
}

return new TaggedValue($value instanceof TaggedIteratorArgument ? 'tagged_iterator' : 'tagged_locator', $content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ function iterator(array $values): IteratorArgument
/**
* Creates a lazy iterator by tag name.
*/
function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null, string|array $exclude = []): TaggedIteratorArgument
function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null, string|array $exclude = [], bool $excludeSelf = true): TaggedIteratorArgument
{
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod, (array) $exclude);
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod, (array) $exclude, $excludeSelf);
}

/**
* Creates a service locator by tag name.
*/
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null, string|array $exclude = []): ServiceLocatorArgument
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null, string|array $exclude = [], bool $excludeSelf = true): ServiceLocatorArgument
{
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod, (array) $exclude));
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod, (array) $exclude, $excludeSelf));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function testTaggedArguments()
{
$taggedIterator = new TaggedIteratorArgument('foo_tag', 'barfoo', 'foobar', false, 'getPriority');
$taggedIterator2 = new TaggedIteratorArgument('foo_tag', null, null, false, null, ['baz']);
$taggedIterator3 = new TaggedIteratorArgument('foo_tag', null, null, false, null, ['baz', 'qux']);
$taggedIterator3 = new TaggedIteratorArgument('foo_tag', null, null, false, null, ['baz', 'qux'], false);

$container = new ContainerBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testTaggedArguments()
{
$taggedIterator = new TaggedIteratorArgument('foo', 'barfoo', 'foobar', false, 'getPriority');
$taggedIterator2 = new TaggedIteratorArgument('foo', null, null, false, null, ['baz']);
$taggedIterator3 = new TaggedIteratorArgument('foo', null, null, false, null, ['baz', 'qux']);
$taggedIterator3 = new TaggedIteratorArgument('foo', null, null, false, null, ['baz', 'qux'], false);

$container = new ContainerBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<argument type="tagged_iterator" tag="foo_tag" exclude="baz"/>
</service>
<service id="foo3_tagged_iterator" class="Bar" public="true">
<argument type="tagged_iterator" tag="foo_tag">
<argument type="tagged_iterator" tag="foo_tag" exclude-self="false">
<exclude>baz</exclude>
<exclude>qux</exclude>
</argument>
Expand All @@ -30,7 +30,7 @@
<argument type="tagged_locator" tag="foo_tag" exclude="baz"/>
</service>
<service id="foo3_tagged_locator" class="Bar" public="true">
<argument type="tagged_locator" tag="foo_tag">
<argument type="tagged_locator" tag="foo_tag" exclude-self="false">
<exclude>baz</exclude>
<exclude>qux</exclude>
</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
arguments: [!tagged_iterator { tag: foo, exclude: baz }]
foo3_service_tagged_iterator:
class: Bar
arguments: [!tagged_iterator { tag: foo, exclude: [baz, qux] }]
arguments: [!tagged_iterator { tag: foo, exclude: [baz, qux], exclude_self: false }]
foo_service_tagged_locator:
class: Bar
arguments: [!tagged_locator { tag: foo, index_by: barfoo, default_index_method: foobar, default_priority_method: getPriority }]
Expand All @@ -33,7 +33,7 @@ services:
arguments: [!tagged_locator { tag: foo, exclude: baz }]
foo3_service_tagged_locator:
class: Bar
arguments: [!tagged_locator { tag: foo, exclude: [baz, qux] }]
arguments: [!tagged_locator { tag: foo, exclude: [baz, qux], exclude_self: false }]
bar_service_tagged_locator:
class: Bar
arguments: [!tagged_locator foo]
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,14 @@ public function testParseTaggedArgumentsWithIndexBy()
$this->assertEquals($taggedIterator, $container->getDefinition('foo_tagged_iterator')->getArgument(0));
$taggedIterator2 = new TaggedIteratorArgument('foo_tag', null, null, false, null, ['baz']);
$this->assertEquals($taggedIterator2, $container->getDefinition('foo2_tagged_iterator')->getArgument(0));
$taggedIterator3 = new TaggedIteratorArgument('foo_tag', null, null, false, null, ['baz', 'qux']);
$taggedIterator3 = new TaggedIteratorArgument('foo_tag', null, null, false, null, ['baz', 'qux'], false);
$this->assertEquals($taggedIterator3, $container->getDefinition('foo3_tagged_iterator')->getArgument(0));

$taggedIterator = new TaggedIteratorArgument('foo_tag', 'barfoo', 'foobar', true, 'getPriority');
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_tagged_locator')->getArgument(0));
$taggedIterator2 = new TaggedIteratorArgument('foo_tag', 'foo_tag', 'getDefaultFooTagName', true, 'getDefaultFooTagPriority', ['baz']);
$this->assertEquals(new ServiceLocatorArgument($taggedIterator2), $container->getDefinition('foo2_tagged_locator')->getArgument(0));
$taggedIterator3 = new TaggedIteratorArgument('foo_tag', 'foo_tag', 'getDefaultFooTagName', true, 'getDefaultFooTagPriority', ['baz', 'qux']);
$taggedIterator3 = new TaggedIteratorArgument('foo_tag', 'foo_tag', 'getDefaultFooTagName', true, 'getDefaultFooTagPriority', ['baz', 'qux'], false);
$this->assertEquals(new ServiceLocatorArgument($taggedIterator3), $container->getDefinition('foo3_tagged_locator')->getArgument(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ public function testTaggedArgumentsWithIndex()
$this->assertEquals($taggedIterator, $container->getDefinition('foo_service_tagged_iterator')->getArgument(0));
$taggedIterator2 = new TaggedIteratorArgument('foo', null, null, false, null, ['baz']);
$this->assertEquals($taggedIterator2, $container->getDefinition('foo2_service_tagged_iterator')->getArgument(0));
$taggedIterator3 = new TaggedIteratorArgument('foo', null, null, false, null, ['baz', 'qux']);
$taggedIterator3 = new TaggedIteratorArgument('foo', null, null, false, null, ['baz', 'qux'], false);
$this->assertEquals($taggedIterator3, $container->getDefinition('foo3_service_tagged_iterator')->getArgument(0));

$taggedIterator = new TaggedIteratorArgument('foo', 'barfoo', 'foobar', true, 'getPriority');
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_service_tagged_locator')->getArgument(0));
$taggedIterator2 = new TaggedIteratorArgument('foo', 'foo', 'getDefaultFooName', true, 'getDefaultFooPriority', ['baz']);
$this->assertEquals(new ServiceLocatorArgument($taggedIterator2), $container->getDefinition('foo2_service_tagged_locator')->getArgument(0));
$taggedIterator3 = new TaggedIteratorArgument('foo', 'foo', 'getDefaultFooName', true, 'getDefaultFooPriority', ['baz', 'qux']);
$taggedIterator3 = new TaggedIteratorArgument('foo', 'foo', 'getDefaultFooName', true, 'getDefaultFooPriority', ['baz', 'qux'], false);
$this->assertEquals(new ServiceLocatorArgument($taggedIterator3), $container->getDefinition('foo3_service_tagged_locator')->getArgument(0));

$taggedIterator = new TaggedIteratorArgument('foo', null, null, true);
Expand Down