Finwe Finwe - 4 years ago 244
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.

app: "@app"
- [ 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 Source

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

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

And you can pass that to the addConnector call:

    app: "@app"
    - [ addConnector, ["@Illuminate\\Queue\Connector\NullConnector", "@some_callback"]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download