Skip to content

[DependencyInjection] isPublic in Definition has wrong initial state #39461

@shyim

Description

@shyim

Symfony version(s) affected: v4.4.14

Description
The Definition class has a wrong initial state of $private and $public. Definitions without setPublic, setPrivate have both methods true. wtf 😂

How to reproduce

$container = new ContainerBuilder();
$container->setDefinition('foo', new Definition('SomeClass'));

var_dump($container->getDefinition('foo')->isPublic());
var_dump($container->getDefinition('foo')->isPrivate());


$container->compile();

var_dump($container->get('foo'));
bool(true)
bool(true)

Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "foo" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

Possible Solution
Set $public to false in Definition as default.

I can contribute a PR if you agree with me :)

Additional context

We have a compiler pass which checks that all event listeners needs to be public and throws an exception if not. But this happens only when the user has explict set private.

$def = $container->getDefinition($id);

            if (!$def->isPublic()) {
                throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
            }

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