Definine a synthetic service in Symfony 4.1 doesn't work from within a bundle

Viewed 812

I am creating a Symfony Bundle that defines a synthetic service:

my_alias:
    public: true
    synthetic: true

After installing the bundle, the application's console command cache:clear always fails with the error:

[Symfony\Component\DependencyInjection\Exception\RuntimeException] Invalid service "my_alias": class "" does not exist.

If I create the same alias inside the actual framework, this error does not turn up.

Is this a bug, or am I doing something wrong?

1 Answers

When you clear the cache the kernel is recompiled and the container will not figure out your service that is not injected to the container, that's why you need to inject it under your Kernel class:

if (!$container->hasDefinition('my_alias')) {
    $container->set('my_alias', new SyntheticService());
}
Related