Lets assume I have the following code:
class Barman {
private function bar($foobar) {
// ...
foreach($foobar as $bar) {
$realBar = new $bar(); // how to get this via dependency injection?
$realBar->doStuff();
}
// ...
}
}
$bar
is an instantiable and registered class which always implements BarInterface and therefore has a doStuff()
method (checked via Reflection). The problem is that $bar
looks like the following:
class MyByInterface implements BarInterface {
public function __construct(Translator $translator, FancyService $fancyService, AnotherService $anotherService) {
$this->translator = $translator;
$this->fancyService = $fancyService;
$this->anotherService = $anotherService;
}
public function doStuff($stuff): bool {
$this->fancyService->exists($stuff);
}
}
So $bar
is usually created by Symfonys dependency injection container and it will not be easy to get all those constructor parameters needed in a dynamic loop.
Long story short: What is the correct way to get a usually dependency injected object in such a dynamic loop (with Symfony V4 / V5)?
Aucun commentaire:
Enregistrer un commentaire