Skip to content

[DI] allow loading and dumping tags with an attribute named "name" #36586

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 3, 2020
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CHANGELOG
configure them explicitly instead
* added class `Symfony\Component\DependencyInjection\Dumper\Preloader` to help with preloading on PHP 7.4+
* added tags `container.preload`/`.no_preload` to declare extra classes to preload/services to not preload
* allowed loading and dumping tags with an attribute named "name"
* deprecated `Definition::getDeprecationMessage()`, use `Definition::getDeprecation()` instead
* deprecated `Alias::getDeprecationMessage()`, use `Alias::getDeprecation()` instead
* deprecated PHP-DSL's `inline()` function, use `service()` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
$tag = $this->document->createElement('tag');
$tag->setAttribute('name', $name);
if (!\array_key_exists('name', $attributes)) {
$tag->setAttribute('name', $name);
} else {
$tag->appendChild($this->document->createTextNode($name));
}
foreach ($attributes as $key => $value) {
$tag->setAttribute($key, $value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ private function addService(string $id, Definition $definition): string
foreach ($attributes as $key => $value) {
$att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
}
$att = $att ? ', '.implode(', ', $att) : '';
$att = $att ? ': { '.implode(', ', $att).' }' : '';

$tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att);
$tagsCode .= sprintf(" - %s%s\n", $this->dumper->dump($name), $att);
}
}
if ($tagsCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ private function parseDefinition(\DOMElement $service, string $file, Definition

foreach ($tags as $tag) {
$parameters = [];
$tagName = $tag->nodeValue;
foreach ($tag->attributes as $name => $node) {
if ('name' === $name) {
if ('name' === $name && '' === $tagName) {
continue;
}

Expand All @@ -328,11 +329,11 @@ private function parseDefinition(\DOMElement $service, string $file, Definition
$parameters[$name] = XmlUtils::phpize($node->nodeValue);
}

if ('' === $tag->getAttribute('name')) {
if ('' === $tagName && '' === $tagName = $tag->getAttribute('name')) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', (string) $service->getAttribute('id'), $file));
}

$definition->addTag($tag->getAttribute('name'), $parameters);
$definition->addTag($tagName, $parameters);
}

$definition->setTags(array_merge_recursive($definition->getTags(), $defaults->getTags()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,16 @@ private function parseDefaults(array &$content, string $file): array
$tag = ['name' => $tag];
}

if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".', $file));
if (1 === \count($tag) && \is_array(current($tag))) {
$name = key($tag);
$tag = current($tag);
} else {
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".', $file));
}
$name = $tag['name'];
unset($tag['name']);
}
$name = $tag['name'];
unset($tag['name']);

if (!\is_string($name) || '' === $name) {
throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in "%s".', $file));
Expand Down Expand Up @@ -568,11 +573,16 @@ private function parseDefinition(string $id, $service, string $file, array $defa
$tag = ['name' => $tag];
}

if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".', $id, $file));
if (1 === \count($tag) && \is_array(current($tag))) {
$name = key($tag);
$tag = current($tag);
} else {
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".', $id, $file));
}
$name = $tag['name'];
unset($tag['name']);
}
$name = $tag['name'];
unset($tag['name']);

if (!\is_string($name) || '' === $name) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', $id, $file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@
</xsd:complexType>

<xsd:complexType name="tag">
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:anyAttribute namespace="##any" processContents="lax" />
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:anyAttribute namespace="##any" processContents="lax" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>

<xsd:complexType name="deprecated">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
class: stdClass
public: false
tags:
- { name: listener }
- listener
decorated:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator
public: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
public: true
tags:
- { name: t, a: b }
- t: { a: b }
autowire: true
autoconfigure: true
arguments: ['@bar']
bar:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
public: true
tags:
- { name: t, a: b }
- t: { a: b }
autowire: true
calls:
- [setFoo, ['@bar']]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
public: true
tags:
- { name: tag, k: v }
- tag: { k: v }
lazy: true
properties: { p: 1 }
calls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ services:
class: stdClass
public: true
tags:
- { name: proxy, interface: SomeInterface }
- proxy: { interface: SomeInterface }
lazy: true
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
public: true
tags:
- { name: foo }
- { name: baz }
- foo
- baz
deprecated:
package: vendor/package
version: '1.1'
Expand All @@ -20,8 +20,8 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar
public: true
tags:
- { name: foo }
- { name: baz }
- foo
- baz
deprecated:
package: vendor/package
version: '1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo
public: true
tags:
- { name: foo }
- { name: baz }
- foo
- baz
deprecated:
package: vendor/package
version: '1.1'
Expand All @@ -20,8 +20,8 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar
public: true
tags:
- { name: foo }
- { name: baz }
- foo
- baz
deprecated:
package: vendor/package
version: '1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
->class(FooClass::class)
->tag('foo', ['foo' => 'foo'])
->tag('foo', ['bar' => 'bar', 'baz' => 'baz'])
->tag('foo', ['name' => 'bar', 'baz' => 'baz'])
->factory([FooClass::class, 'getInstance'])
->property('foo', 'bar')
->property('moo', ref('foo.baz'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
->register('foo', '\Bar\FooClass')
->addTag('foo', ['foo' => 'foo'])
->addTag('foo', ['bar' => 'bar', 'baz' => 'baz'])
->addTag('foo', ['name' => 'bar', 'baz' => 'baz'])
->setFactory(['Bar\\FooClass', 'getInstance'])
->setArguments(['foo', new Reference('foo.baz'), ['%foo%' => 'foo is %foo%', 'foobar' => '%foo%'], true, new Reference('service_container')])
->setProperties(['foo' => 'bar', 'moo' => new Reference('foo.baz'), 'qux' => ['%foo%' => 'foo is %foo%', 'foobar' => '%foo%']])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<service id="foo" class="Bar\FooClass" public="true">
<tag name="foo" foo="foo"/>
<tag name="foo" bar="bar" baz="baz"/>
<tag name="bar" baz="baz">foo</tag>
<argument>foo</argument>
<argument type="service" id="foo.baz"/>
<argument type="collection">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ services:
public: true

tags:
- { name: foo_tag, tag_option: from_service }
- foo_tag: { tag_option: from_service }
# these 2 are from instanceof
- { name: foo_tag, tag_option: from_instanceof }
- { name: bar_tag }
- { name: from_defaults }
- foo_tag: { tag_option: from_instanceof }
- bar_tag
- from_defaults
# calls from instanceof are kept, but this comes later
calls:
# first call is from instanceof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ services:
foo:
class: Bar\FooClass
tags:
- { name: foo, foo: foo }
- { name: foo, bar: bar, baz: baz }
- foo: { foo: foo }
- foo: { bar: bar, baz: baz }
- foo: { name: bar, baz: baz }
arguments: [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container']
properties: { foo: bar, moo: '@foo.baz', qux: { '%foo%': 'foo is %foo%', foobar: '%foo%' } }
calls:
Expand Down Expand Up @@ -158,7 +159,7 @@ services:
tagged_iterator_foo:
class: Bar
tags:
- { name: foo }
- foo
public: false
tagged_iterator:
class: Bar
Expand Down Expand Up @@ -194,6 +195,6 @@ services:
preload_sidekick:
class: stdClass
tags:
- {name: container.preload, class: 'Some\Sidekick1'}
- {name: container.preload, class: 'Some\Sidekick2'}
- container.preload: { class: 'Some\Sidekick1' }
- container.preload: { class: 'Some\Sidekick2' }
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
foo_service:
class: Foo
tags:
- { name: foo }
- foo
foo_service_tagged_iterator:
class: Bar
arguments: [!tagged_iterator { tag: foo, index_by: barfoo, default_index_method: foobar, default_priority_method: getPriority }]
Expand Down