Skip to content

Commit 5d79741

Browse files
javiereguiluzfabpot
authored andcommitted
Improved the error message when using "@" in a decorated service
1 parent ad97d11 commit 5d79741

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Loader/YamlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ private function parseDefinition($id, $service, $file)
288288
}
289289

290290
if (isset($service['decorates'])) {
291+
if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
292+
throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1)));
293+
}
294+
291295
$renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
292296
$definition->setDecoratedService($service['decorates'], $renameId);
293297
}

Tests/Fixtures/yaml/bad_decorates.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
foo:
3+
class: stdClass
4+
bar:
5+
class: stdClass
6+
decorates: "@foo"
7+
arguments: ["@bar.inner"]

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,14 @@ public function testTagWithNonStringNameThrowsException()
297297
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
298298
$loader->load('tag_name_no_string.yml');
299299
}
300+
301+
/**
302+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
303+
* @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
304+
*/
305+
public function testDecoratedServicesWithWrongSyntaxThrowsException()
306+
{
307+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
308+
$loader->load('bad_decorates.yml');
309+
}
300310
}

0 commit comments

Comments
 (0)