Skip to content

[DependencyInjection] Dumped container does not return aliases in getServiceIds when both alias and its target are public #33307

@pvandommelen

Description

@pvandommelen

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions