Symfony container Definition method is not called for not injected service

Viewed 87

Before I start any discussion on Symfony's Github, I would like to clarify the situation and make sure that I really found an issue for which there is no solution.

When we have a service container definition and that definition is not injected anywhere then any of its methods configured to be called by addMethodCall within a compiler pass - won't be called. Even if we make a service to be public (which preserves service in container) - it won't change the situation.

Of course it makes sense, but according to that behaviour I cannot achieve the following implemenation:

  • There is Service A which has related compiler pass and it is injected into some other service
  • Compiler pass configures Service A definition to call someMethodA() by applying its addMethodCall() function
  • someMethod() is adding instances of Service B to a collection existing within Service A
  • Until now it works perfectly fine, but...
  • Any of Service B instances are not injected anywhere (they are only collected by Service A method within its collection)
  • Instances of Service B also contains their own compiler passes which the same as for Service A configure their definitions to call someMethodB() which are also collecting some other services
  • Unfortunately, according to the fact described at the beginning, about definitions not calling configured methods while they are not injected - makes a problem of not calling someMethodB(), so all instances of Service B have empty collections of related services, preventing the application from working correctly

Question: how can I make sure that not injected service definition will call configured method, so it contains everything what is needed (like its inner collection), when some other service will take an instance of this definition, expecting it to have its collection initialized?

Notice: I was trying to set not injected services to be public and not lazy, I also tried to register them manually in the container and running related compiler passes before removal processes to be 100% sure that they are in place (and actually they are, I don't have problem with missing service, but with the fact that definition is not calling its configured method).

Symfony 4.4.21

0 Answers
Related