bug #22564 Fixing problem where _defaults set to null was seen as a service (weaverryan)

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

Discussion
----------

Fixing problem where _defaults set to null was seen as a service

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

```yml
services:
    _defaults:
```

If you leave `_defaults` empty (i.e. null), you got a bad error before. Now it's better :)

Before:
>The definition for "_defaults" has no class. If you intend to inject this service dynamicall
  y at runtime, please mark it as synthetic=true. If this is an abstract definition solely use
  d by child definitions, please add abstract=true, otherwise specify a class to get rid of th
  is error.

After:
> Service "_defaults" key must be an array, "NULL" given in "/path/to/services.yml"

Commits
-------

4b7e148a9b Fixing problem where _defaults set to null was seen as a service
This commit is contained in:
Christophe Coevoet 2017-04-29 12:32:06 +02:00
commit ceabf11b64
4 changed files with 28 additions and 2 deletions

View File

@ -205,7 +205,7 @@ class YamlFileLoader extends FileLoader
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
if (isset($content['services']['_instanceof'])) {
if (array_key_exists('_instanceof', $content['services'])) {
$instanceof = $content['services']['_instanceof'];
unset($content['services']['_instanceof']);
@ -242,7 +242,7 @@ class YamlFileLoader extends FileLoader
*/
private function parseDefaults(array &$content, $file)
{
if (!isset($content['services']['_defaults'])) {
if (!array_key_exists('_defaults', $content['services'])) {
return array();
}
$defaults = $content['services']['_defaults'];

View File

@ -0,0 +1,2 @@
services:
_defaults:

View File

@ -0,0 +1,2 @@
services:
_instanceof:

View File

@ -577,6 +577,28 @@ class YamlFileLoaderTest extends TestCase
$this->assertTrue($container->getDefinition('use_defaults_settings')->isAutoconfigured());
$this->assertFalse($container->getDefinition('override_defaults_settings_to_false')->isAutoconfigured());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Service "_defaults" key must be an array, "NULL" given in "bad_empty_defaults.yml".
*/
public function testEmptyDefaultsThrowsClearException()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('bad_empty_defaults.yml');
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Service "_instanceof" key must be an array, "NULL" given in "bad_empty_instanceof.yml".
*/
public function testEmptyInstanceofThrowsClearException()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('bad_empty_instanceof.yml');
}
}
interface FooInterface