Finwe Finwe - 4 months ago 7
PHP Question

Symfony DependencyInjection: How to represent Closure in YAML service definitions?

I have a service which requires a closure when trying to setup it using calls: in Symfony DI YAML file.

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


I am wondering if I can enclose a service into a closure, as the library code won't let me insert anything else.

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


Is there a way I can create Closure (or, an anonymous function) in Symfony DI container YAML definition file? I guess it could be done with some compiler pass, but I wonder whether there possibly is an existing solution to this problem.

Answer

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

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

And you can pass that to the addConnector call:

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