Skip to content

Commit e8b9e25

Browse files
committed
bug symfony#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 | symfony#23483 | License | MIT | Doc PR | Commits ------- 8289ca6 non-conflicting anonymous service ids across files
2 parents d1cfec1 + 8289ca6 commit e8b9e25

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ public function load($resource, $type = null)
121121
// parameters
122122
if (isset($content['parameters'])) {
123123
if (!is_array($content['parameters'])) {
124-
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource));
124+
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $path));
125125
}
126126

127127
foreach ($content['parameters'] as $key => $value) {
128-
$this->container->setParameter($key, $this->resolveServices($value, $resource, true));
128+
$this->container->setParameter($key, $this->resolveServices($value, $path, true));
129129
}
130130
}
131131

@@ -136,7 +136,7 @@ public function load($resource, $type = null)
136136
$this->anonymousServicesCount = 0;
137137
$this->setCurrentDir(dirname($path));
138138
try {
139-
$this->parseDefinitions($content, $resource);
139+
$this->parseDefinitions($content, $path);
140140
} finally {
141141
$this->instanceof = array();
142142
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
AppBundle\Foo:
3+
arguments:
4+
- !service {class: AppBundle\Bar }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
AppBundle\Hello:
3+
arguments:
4+
- !service {class: AppBundle\World}

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public function testDecoratedServicesWithWrongSyntaxThrowsException()
501501

502502
/**
503503
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
504-
* @expectedExceptionMessage Parameter "tags" must be an array for service "Foo\Bar" in services31_invalid_tags.yml. Check your YAML syntax.
504+
* @expectedExceptionMessageRegExp /Parameter "tags" must be an array for service "Foo\\Bar" in .+services31_invalid_tags\.yml\. Check your YAML syntax./
505505
*/
506506
public function testInvalidTagsWithDefaults()
507507
{
@@ -534,7 +534,7 @@ public function testAnonymousServices()
534534
$this->assertCount(1, $args);
535535
$this->assertInstanceOf(Reference::class, $args[0]);
536536
$this->assertTrue($container->has((string) $args[0]));
537-
$this->assertStringStartsWith('2', (string) $args[0]);
537+
$this->assertRegExp('/^\d+_[A-Za-z0-9]{64}$/', (string) $args[0]);
538538

539539
$anonymous = $container->getDefinition((string) $args[0]);
540540
$this->assertEquals('Bar', $anonymous->getClass());
@@ -546,7 +546,7 @@ public function testAnonymousServices()
546546
$this->assertInternalType('array', $factory);
547547
$this->assertInstanceOf(Reference::class, $factory[0]);
548548
$this->assertTrue($container->has((string) $factory[0]));
549-
$this->assertStringStartsWith('1', (string) $factory[0]);
549+
$this->assertRegExp('/^\d+_[A-Za-z0-9]{64}$/', (string) $factory[0]);
550550
$this->assertEquals('constructFoo', $factory[1]);
551551

552552
$anonymous = $container->getDefinition((string) $factory[0]);
@@ -555,6 +555,19 @@ public function testAnonymousServices()
555555
$this->assertFalse($anonymous->isAutowired());
556556
}
557557

558+
public function testAnonymousServicesInDifferentFilesWithSameNameDoNotConflict()
559+
{
560+
$container = new ContainerBuilder();
561+
562+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml/foo'));
563+
$loader->load('services.yml');
564+
565+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml/bar'));
566+
$loader->load('services.yml');
567+
568+
$this->assertCount(5, $container->getDefinitions());
569+
}
570+
558571
public function testAnonymousServicesInInstanceof()
559572
{
560573
$container = new ContainerBuilder();
@@ -582,7 +595,7 @@ public function testAnonymousServicesInInstanceof()
582595

583596
/**
584597
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
585-
* @expectedExceptionMessage Creating an alias using the tag "!service" is not allowed in "anonymous_services_alias.yml".
598+
* @expectedExceptionMessageRegExp /Creating an alias using the tag "!service" is not allowed in ".+anonymous_services_alias\.yml"\./
586599
*/
587600
public function testAnonymousServicesWithAliases()
588601
{
@@ -593,7 +606,7 @@ public function testAnonymousServicesWithAliases()
593606

594607
/**
595608
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
596-
* @expectedExceptionMessage Using an anonymous service in a parameter is not allowed in "anonymous_services_in_parameters.yml".
609+
* @expectedExceptionMessageRegExp /Using an anonymous service in a parameter is not allowed in ".+anonymous_services_in_parameters\.yml"\./
597610
*/
598611
public function testAnonymousServicesInParameters()
599612
{
@@ -614,7 +627,7 @@ public function testAutoConfigureInstanceof()
614627

615628
/**
616629
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
617-
* @expectedExceptionMessage Service "_defaults" key must be an array, "NULL" given in "bad_empty_defaults.yml".
630+
* @expectedExceptionMessageRegExp /Service "_defaults" key must be an array, "NULL" given in ".+bad_empty_defaults\.yml"\./
618631
*/
619632
public function testEmptyDefaultsThrowsClearException()
620633
{
@@ -625,7 +638,7 @@ public function testEmptyDefaultsThrowsClearException()
625638

626639
/**
627640
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
628-
* @expectedExceptionMessage Service "_instanceof" key must be an array, "NULL" given in "bad_empty_instanceof.yml".
641+
* @expectedExceptionMessageRegExp /Service "_instanceof" key must be an array, "NULL" given in ".+bad_empty_instanceof\.yml"\./
629642
*/
630643
public function testEmptyInstanceofThrowsClearException()
631644
{

0 commit comments

Comments
 (0)