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

This commit is contained in:
Javier Eguiluz 2016-02-04 15:21:55 +01:00 committed by Fabien Potencier
parent 3af65d72ec
commit e7690ba5c4
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');
}
}