symfony: getSubscribedServices() does not support all valid container service names

Symfony version(s) affected: 3.4 and up

Description
When implementing the Symfony\Component\DependencyInjection\ServiceSubscriberInterface the method getSubscribedServices() is supposed to return all services that the class can get from the container.

The problem is that if the service name has a . in it it will not be allowed. Not all services are defined as a fully qualified class name, some (may be for legacy reasons or other reasons) may still be defined in the container with . in it.

How to reproduce
Crate a service in the dependency container called f.ex: “my.test.service”.

Then create another service App\MyService that implements Symfony\Component\DependencyInjection\ServiceSubscriberInterface and is trying to use this service. The method getSubscribedServices() could look like this

public static function getSubscribedServices()
{
    return ['key' => 'my.test.service'];
}

Then you get the error:

"App\MyService::getSubscribedServices()" must return valid PHP types for service "App\MyService" key "key", "my.test.service" returned.

Possible Solution
The regex in code/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php line 71 should be removed or relaxed so that all possible container service names and aliases are allowed.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

@TerjeBr I think this should be doable with a binding?

Like

App\MyServiceSubscriber:
    bind:
        App\MyService: '@my.test.service'
public static function getSubscribedServices()
{
    return ['key' => App\MyService::class];
}