Skip to content

Commit 42004d3

Browse files
committed
feature #20651 [DependencyInjection] Added Yaml syntax shortcut for name-only tags (wouterj)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DependencyInjection] Added Yaml syntax shortcut for name-only tags | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | todo This PR adds a little shorcut for tags without any attributes. There are increasingly more name-only tags in Symfony and having to do `{ name: twig.extension }` for these seems way too verbose to me. **Before** ```yaml services: app.twig_extension: class: AppBundle\Twig\AppExtension tags: - { name: twig.extension } ``` **After** ```yaml services: app.twig_extension: class: AppBundle\Twig\AppExtension tags: [twig.extension] # or # - twig.extension ``` This of course means we introduce a new format to achieve the same goal. I believe this isn't a big problem as the decision is distinctive and simple: If you configure tag attributes, use the long format, otherwise use the short format. Backwards compatibility --- In this PR, an exception was removed to allow this new shortcut format. The BC promise doesn't cover exceptions and I think removing the exception here should cause anything to break: * Applications shouldn't rely on exceptions * If code was triggering this exception before, it would not cause any behaviour change after this PR: The service just retrieves an unused tag, which is simply ignored by the container. Commits ------- 7fa8c8a Added Yaml syntax shortcut for name-only tags
2 parents b634450 + 924ed7c commit 42004d3

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private function parseDefinition($id, $service, $file)
267267

268268
foreach ($service['tags'] as $tag) {
269269
if (!is_array($tag)) {
270-
throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
270+
$tag = array('name' => $tag);
271271
}
272272

273273
if (!isset($tag['name'])) {

Tests/Fixtures/yaml/badtag4.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

Tests/Fixtures/yaml/tag_name_only.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
- foo

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,6 @@ public function testNonArrayTagsThrowsException()
223223
}
224224
}
225225

226-
/**
227-
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
228-
* @expectedExceptionMessage A "tags" entry must be an array for service
229-
*/
230-
public function testNonArrayTagThrowsException()
231-
{
232-
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
233-
$loader->load('badtag4.yml');
234-
}
235-
236226
public function testTagWithoutNameThrowsException()
237227
{
238228
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
@@ -245,6 +235,15 @@ public function testTagWithoutNameThrowsException()
245235
}
246236
}
247237

238+
public function testNameOnlyTagsAreAllowedAsString()
239+
{
240+
$container = new ContainerBuilder();
241+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
242+
$loader->load('tag_name_only.yml');
243+
244+
$this->assertCount(1, $container->getDefinition('foo_service')->getTag('foo'));
245+
}
246+
248247
public function testTagWithAttributeArrayThrowsException()
249248
{
250249
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));

0 commit comments

Comments
 (0)