bug #17687 Improved the error message when using "@" in a decorated service (javiereguiluz)

This PR was squashed before being merged into the 2.7 branch (closes #17687).

Discussion
----------

Improved the error message when using "@" in a decorated service

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17666
| License       | MIT
| Doc PR        | -

Commits
-------

e7690ba Improved the error message when using "@" in a decorated service
This commit is contained in:
Fabien Potencier 2016-03-03 14:15:14 +01:00
commit aab27839f9
3 changed files with 21 additions and 0 deletions

View File

@ -288,6 +288,10 @@ class YamlFileLoader extends FileLoader
}
if (isset($service['decorates'])) {
if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
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)));
}
$renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
$definition->setDecoratedService($service['decorates'], $renameId);
}

View File

@ -0,0 +1,7 @@
services:
foo:
class: stdClass
bar:
class: stdClass
decorates: "@foo"
arguments: ["@bar.inner"]

View File

@ -297,4 +297,14 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('tag_name_no_string.yml');
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @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").
*/
public function testDecoratedServicesWithWrongSyntaxThrowsException()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('bad_decorates.yml');
}
}