Symfony DependencyInjection: how to display closure in YAML service definitions?

I have a service that needs to be closed when I try to configure it using calls: in the Symfony DI YAML file.

Illuminate\Queue\QueueManager: arguments: app: "@app" calls: - [ addConnector, [ "@Illuminate\\Queue\Connector\NullConnector" ]] 

I am wondering if I can enable the service in closure, as the library code will not allow me to insert anything else.

 public function addConnector($driver, Closure $resolver) { $this->connectors[$driver] = $resolver; } 

Is there a way to create a Closure (or anonymous function) in the Symfony DI container's YAML definition file? I suppose this can be done with some compiler pass, but I wonder if an existing solution to this problem is possible.

+6
source share
1 answer

You have probably already decided this by now. But you can create a factory that returns Closure

 some_callback: public: false class: callback factory: [SomeClass\Factory, create] 

And you can pass this to the addConnector call:

 Illuminate\Queue\QueueManager: arguments: app: "@app" calls: - [ addConnector, ["@Illuminate\\Queue\Connector\NullConnector", "@some_callback"]] 
+5
source

All Articles