[DI] Trigger exception when using '@id' name in parent option

This commit is contained in:
Sébastien ALFAIATE 2018-08-30 18:24:21 +07:00 committed by Fabien Potencier
parent 8651758fc1
commit 1f67db626e
3 changed files with 20 additions and 0 deletions

View File

@ -378,6 +378,10 @@ class YamlFileLoader extends FileLoader
}
}
if ('' !== $service['parent'] && '@' === $service['parent'][0]) {
throw new InvalidArgumentException(sprintf('The value of the "parent" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['parent'], substr($service['parent'], 1)));
}
$definition = new ChildDefinition($service['parent']);
} else {
$definition = new Definition();

View File

@ -0,0 +1,6 @@
services:
foo:
class: stdClass
bar:
class: stdClass
parent: "@foo"

View File

@ -511,6 +511,16 @@ class YamlFileLoaderTest extends TestCase
$container->compile();
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage The value of the "parent" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
*/
public function testChildDefinitionWithWrongSyntaxThrowsException()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('bad_parent.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").