Skip to content

Commit 2709bc2

Browse files
committed
bug #33335 [DependencyInjection] Fixed the getServiceIds implementation to always return aliases (pdommelen)
This PR was squashed before being merged into the 3.4 branch (closes #33335). Discussion ---------- [DependencyInjection] Fixed the `getServiceIds` implementation to always return aliases | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | maybe | Deprecations? | no | Tests pass? | yes | Fixed tickets | #33307 | License | MIT | Doc PR | - Changed the getServiceIds implementation in the Container base class to include aliases. Modified existing tests. Added test which uses the PhpDumper. Fixes symfony/symfony#33307 Without this patch the implementations of the container are inconsistent in whether or not they return aliases (see issue). Fixing this could be considered a BC break for the affected Container class. As an alternative to keep the behaviour in Container unchanged, the dumped container could be patched instead. And then only apply this version of the patch to master. This however keeps the inconsistency between Container and ContainerBuilder. Commits ------- 834d5cbce2 [DependencyInjection] Fixed the `getServiceIds` implementation to always return aliases
2 parents bec0a6f + 255ef9f commit 2709bc2

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public function getServiceIds()
405405
}
406406
$ids[] = 'service_container';
407407

408-
return array_map('strval', array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->fileMap), array_keys($this->services))));
408+
return array_map('strval', array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->fileMap), array_keys($this->aliases), array_keys($this->services))));
409409
}
410410

411411
/**

Tests/ContainerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testGetServiceIds()
156156

157157
$sc = new ProjectServiceContainer();
158158
$sc->set('foo', $obj = new \stdClass());
159-
$this->assertEquals(['service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
159+
$this->assertEquals(['service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'alias', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
160160
}
161161

162162
/**
@@ -168,7 +168,7 @@ public function testGetLegacyServiceIds()
168168
$sc = new LegacyProjectServiceContainer();
169169
$sc->set('foo', $obj = new \stdClass());
170170

171-
$this->assertEquals(['internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
171+
$this->assertEquals(['internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'alias', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
172172
}
173173

174174
public function testSet()

Tests/Dumper/PhpDumperTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,30 @@ public function testScalarService()
11391139
$this->assertTrue($container->has('foo'));
11401140
$this->assertSame('some value', $container->get('foo'));
11411141
}
1142+
1143+
public function testAliasCanBeFoundInTheDumpedContainerWhenBothTheAliasAndTheServiceArePublic()
1144+
{
1145+
$container = new ContainerBuilder();
1146+
1147+
$container->register('foo', 'stdClass')->setPublic(true);
1148+
$container->setAlias('bar', 'foo')->setPublic(true);
1149+
1150+
$container->compile();
1151+
1152+
// Bar is found in the compiled container
1153+
$service_ids = $container->getServiceIds();
1154+
$this->assertContains('bar', $service_ids);
1155+
1156+
$dumper = new PhpDumper($container);
1157+
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_AliasesCanBeFoundInTheDumpedContainer']);
1158+
eval('?>'.$dump);
1159+
1160+
$container = new \Symfony_DI_PhpDumper_AliasesCanBeFoundInTheDumpedContainer();
1161+
1162+
// Bar should still be found in the compiled container
1163+
$service_ids = $container->getServiceIds();
1164+
$this->assertContains('bar', $service_ids);
1165+
}
11421166
}
11431167

11441168
class Rot13EnvVarProcessor implements EnvVarProcessorInterface

0 commit comments

Comments
 (0)