feature #28315 [DI] Trigger exception when using '@id' name in parent option (Seb33300)

This PR was squashed before being merged into the 4.2-dev branch (closes #28315).

Discussion
----------

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

Same exception [already triggered](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php#L505) on the `decorates` option.

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

Note sure if I should submit this change for master or previous branches...

Commits
-------

1f67db626e [DI] Trigger exception when using '@id' name in parent option
This commit is contained in:
Fabien Potencier 2018-09-04 08:40:05 +02:00
commit 68a910f528
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").