Lets suppose I have an interface ThingInterface and two implementations Thing and MockThing. Now assuming I am using dependency injection and I have programmed against ThingInterface then in symfony I can tell it which implementation to use, as follows:
services:
...
ThingInterface: '@Thing'
...
Is it possible to pick and implementation to use based on a parameter? E.g:
parameters:
useMock: 'true'
services:
...
# ThingInterface maps to ThingMock or Thing based on useMock parameter
ThingInterface: '@ThingMock'
...
I'm using symfony 3.4.
The context here is that I'll have a number of services, and I want to easily switch between an actual implementation and mocked version. I can do this individually for each service, but I was hoping I can do this through a parameter 'toggle'.
I found this which gives this example:
services:
AppBundle\Mailer:
arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
But I have been unable to make it work for mapping interfaces to implementations:
parameters:
useMock: 'true'
services:
...
# ThingInterface maps to ThingMock or Thing based on useMock parameter
ThingInterface: '@ThingMock'
ThingInterface: ["@=container.getParameter('useMock') ? service('ThingMock') : service('Thing')"]
...