samedi 27 janvier 2018

Dependency Injection through a setter

Does Laravel have the ability to introduce dependency through a method?
For example, I have a controller that implements the DoctrineWorkable interface:

interface DoctrineWorkable {
    public function setEntityManager(EntityManager $manager);
}

trait EntityManagerTrait {
   protected $manager;

    public function setEntityManager(EntityManager $manager) {
        $this->manager = $manager;
        // Other work with $manager
    }
}

class TestController implements DoctrineWorkable {
    use EntityManagerTrait;
}

How to use the setEntityManager method in the service container? To get something like this:

if ($class instanceof DoctrineWorkable) {
    $class->setEntityManager(new EntityManager());
}

p.s. Sorry for the bad English ))

Aucun commentaire:

Enregistrer un commentaire