In my application I have an SMS service. This service is a simple POPO that takes an instance to a driver to do the actual SMS functionality.
Imagine I have two drivers, mock_driver
and gateway_driver
which are defined as something like this in the services
section:
mock_driver:
class: MyApp\Service\Sms\MockDriver
gateway_driver:
class: MyApp\Service\Sms\GatewayDriver
calls:
- [setConfig, ["%gateway_user%", "%gateway_password%", "%gateway_endpoint%"]]
And the SMS service is defined like this:
service_sms:
class: MyApp\Service\SmsService
calls:
- [setDriver, ["%service_sms_driver%"]]
The problem I'm facing is that I want to pass an "instance" of one of the two drivers into the setDriver
method of my service. Which driver this is should be defined in my parameters.yml
, something such as:
service_sms_driver: ["@mock_driver"]
However, I'm stuck on the syntax to make this work correctly. I think the gist of it is correct except for the syntax on the service_sms_driver
value in my parameters.yml
and the setDriver
method call on the actual service.
Any help appreciated.
Edit: Just as a clarification, both drivers implement the same interface. However, every driver might need different ways of configuration which might not be captured in an interface. If I was simply passing class names it would work fine but I'm trying to inject instances instead. Hope this makes sense.
Since your services construct differently, the idea of aliasing seems to be a correct idea.
To do so, just create your two driver services and your manager. (Note the usage of
@driver
)services.yml
Then, you can edit this alias into your
AcmeFooExtension
fileAcme/FooBundle/DependencyInjection/AcmeFooExtension.php
This will take the
service_sms_driver
parameter and create an alias of this service.Example of debugging:
config_dev.yml
Running
Results in
config_prod.yml
Running
Results in
config_test.yml
Running
Results in