bug #23490 [DependencyInjection] non-conflicting anonymous service ids across files (xabbuh)

This PR was merged into the 3.3 branch.

Discussion
----------

[DependencyInjection] non-conflicting anonymous service ids across files

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

Commits
-------

8289ca6d1a non-conflicting anonymous service ids across files
This commit is contained in:
Fabien Potencier 2017-07-13 00:39:22 +02:00
commit e8b9e253e8
4 changed files with 31 additions and 10 deletions

View File

@ -121,11 +121,11 @@ class YamlFileLoader extends FileLoader
// parameters
if (isset($content['parameters'])) {
if (!is_array($content['parameters'])) {
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource));
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $path));
}
foreach ($content['parameters'] as $key => $value) {
$this->container->setParameter($key, $this->resolveServices($value, $resource, true));
$this->container->setParameter($key, $this->resolveServices($value, $path, true));
}
}
@ -136,7 +136,7 @@ class YamlFileLoader extends FileLoader
$this->anonymousServicesCount = 0;
$this->setCurrentDir(dirname($path));
try {
$this->parseDefinitions($content, $resource);
$this->parseDefinitions($content, $path);
} finally {
$this->instanceof = array();
}

View File

@ -0,0 +1,4 @@
services:
AppBundle\Foo:
arguments:
- !service {class: AppBundle\Bar }

View File

@ -0,0 +1,4 @@
services:
AppBundle\Hello:
arguments:
- !service {class: AppBundle\World}

View File

@ -501,7 +501,7 @@ class YamlFileLoaderTest extends TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Parameter "tags" must be an array for service "Foo\Bar" in services31_invalid_tags.yml. Check your YAML syntax.
* @expectedExceptionMessageRegExp /Parameter "tags" must be an array for service "Foo\\Bar" in .+services31_invalid_tags\.yml\. Check your YAML syntax./
*/
public function testInvalidTagsWithDefaults()
{
@ -534,7 +534,7 @@ class YamlFileLoaderTest extends TestCase
$this->assertCount(1, $args);
$this->assertInstanceOf(Reference::class, $args[0]);
$this->assertTrue($container->has((string) $args[0]));
$this->assertStringStartsWith('2', (string) $args[0]);
$this->assertRegExp('/^\d+_[A-Za-z0-9]{64}$/', (string) $args[0]);
$anonymous = $container->getDefinition((string) $args[0]);
$this->assertEquals('Bar', $anonymous->getClass());
@ -546,7 +546,7 @@ class YamlFileLoaderTest extends TestCase
$this->assertInternalType('array', $factory);
$this->assertInstanceOf(Reference::class, $factory[0]);
$this->assertTrue($container->has((string) $factory[0]));
$this->assertStringStartsWith('1', (string) $factory[0]);
$this->assertRegExp('/^\d+_[A-Za-z0-9]{64}$/', (string) $factory[0]);
$this->assertEquals('constructFoo', $factory[1]);
$anonymous = $container->getDefinition((string) $factory[0]);
@ -555,6 +555,19 @@ class YamlFileLoaderTest extends TestCase
$this->assertFalse($anonymous->isAutowired());
}
public function testAnonymousServicesInDifferentFilesWithSameNameDoNotConflict()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml/foo'));
$loader->load('services.yml');
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml/bar'));
$loader->load('services.yml');
$this->assertCount(5, $container->getDefinitions());
}
public function testAnonymousServicesInInstanceof()
{
$container = new ContainerBuilder();
@ -582,7 +595,7 @@ class YamlFileLoaderTest extends TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Creating an alias using the tag "!service" is not allowed in "anonymous_services_alias.yml".
* @expectedExceptionMessageRegExp /Creating an alias using the tag "!service" is not allowed in ".+anonymous_services_alias\.yml"\./
*/
public function testAnonymousServicesWithAliases()
{
@ -593,7 +606,7 @@ class YamlFileLoaderTest extends TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Using an anonymous service in a parameter is not allowed in "anonymous_services_in_parameters.yml".
* @expectedExceptionMessageRegExp /Using an anonymous service in a parameter is not allowed in ".+anonymous_services_in_parameters\.yml"\./
*/
public function testAnonymousServicesInParameters()
{
@ -614,7 +627,7 @@ class YamlFileLoaderTest extends TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Service "_defaults" key must be an array, "NULL" given in "bad_empty_defaults.yml".
* @expectedExceptionMessageRegExp /Service "_defaults" key must be an array, "NULL" given in ".+bad_empty_defaults\.yml"\./
*/
public function testEmptyDefaultsThrowsClearException()
{
@ -625,7 +638,7 @@ class YamlFileLoaderTest extends TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Service "_instanceof" key must be an array, "NULL" given in "bad_empty_instanceof.yml".
* @expectedExceptionMessageRegExp /Service "_instanceof" key must be an array, "NULL" given in ".+bad_empty_instanceof\.yml"\./
*/
public function testEmptyInstanceofThrowsClearException()
{