-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed
Description
Symfony version(s) affected: 3.4 to master
Description
The dumped container created through the PhpDumper
does not return aliases using getServiceIds
when both the alias and its target service are marked as public.
This is an inconstency/bug because in two other cases aliases are returned:
- when the
ContainerBuilder
is compiled but not dumped; - when the target service is not marked as public.
How to reproduce
The following test illustrates the issue:
public function testAliasCanBeFoundInTheDumpedContainerWhenBothTheAliasAndTheServiceArePublic() {
$container = new ContainerBuilder();
$container->register(Foo::class)->setPublic(true);
$container->setAlias('Bar', Foo::class)->setPublic(true);
$container->compile();
// Bar is found in the compiled container
$service_ids = $container->getServiceIds();
$this->assertContains("Bar", $service_ids);
$dumper = new PhpDumper($container);
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_AliasesCanBeFoundInTheDumpedContainer']);
eval('?>'.$dump);
/** @var Container $container */
$container = new \Symfony_DI_PhpDumper_AliasesCanBeFoundInTheDumpedContainer();
// Bar should still be found in the compiled container
$service_ids = $container->getServiceIds();
$this->assertContains("Bar", $service_ids);
}
Possible Solution
The inconsistency can be resolved by changing the implementation of getServiceIds
in the parent Container
class to include the aliases.
return array_map('strval', array_unique(array_merge(['service_container'], array_keys($this->fileMap), array_keys($this->methodMap), array_keys($this->services))));
to
return array_map('strval', array_unique(array_merge(['service_container'], array_keys($this->fileMap), array_keys($this->methodMap), array_keys($this->aliases), array_keys($this->services))));
However, this results in the test ContainerTest::testGetServiceIds
failing, which does not expect the alias to be returned.