Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 602
Finwe Avatar asked Jul 09 '15 18:07

Finwe


1 Answers

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"]]
like image 122
tlenss Avatar answered Oct 21 '22 04:10

tlenss