Bypass auto wiring using services.yml symfony

64 Views Asked by At

I have a quick question regarding auto wiring on symfony. I am currently migrating my symfony 2.8 app to symfony 5.4. I have my BaseManager class which is used by several manager of my application. Several managers therefore use this class and pass it 2 arguments as parameters which are EntityManager and $class.

Of course, the $class variable is different for each manager and therefore cannot be managed by auto wiring.

So I have my managers that are declared as a service. However these declarations are not recognized by the auto wiring because I use an alias for these services. Is there a way to link my service alias to my manager without having to recreate several statements with the full name of the manager in the services.yml? this is a bit redundant, especially since I have about forty managers to transfer.

Thank you

This is my custom services.yml

# Employee Manager
WORD\EmployeeBundle\Service\EmployeeManager:
    arguments:
        - '@doctrine.orm.entity_manager'
        - '%word.employee.model.employee.class%'
    public: true
    
# Employee Manager
word.employee.manager.employee:
    class: 'WORD\EmployeeBundle\Service\EmployeeManager'
    arguments:
        - '@doctrine.orm.entity_manager'
        - '%word.employee.model.employee.class%'
    public: true

This is my custom baseManager who expect $em and $class

abstract class BaseManager{

protected $em;


protected $repository;


protected $class;

public function __construct(EntityManagerInterface $em, string $class)
{
    $this->em = $em;
    $this->repository = $em->getRepository($class);

    $metadata = $em->getClassMetadata($class);
    $this->class = $metadata->name;
}
0

There are 0 best solutions below