I’d like to be able to wire up services from configuration parameters so that I can use a mock implementation of a web service client in the dev environment and I can use the proper client in prod env.
Here is a snippet from my services.xml:
<parameters>
...
<parameter key="api_client.api_service.id">ic_domain_security.api_mock_service</parameter>
...
</parameters>
<services>
...
<service id="ic_domain_security.api_client"
class="%api_client.class%"
public="true">
<tag name="monolog.logger" channel="domainApiClient"/>
<argument type="service" id="%api_client.api_service.id%"/>
<call method="setLogger">
<argument type="service" id="logger" on-invalid="null"/>
</call>
</service>
<service id="ic_domain_security.api_restful_webservice"
class="IC\DomainSecurityBundle\ApiClient\ApiRestfulWebService"
public="false">
<tag name="monolog.logger" channel="domainApiRestfulWebService"/>
<argument>%ic_domain_security.service_url%</argument>
<argument type="service" id="logger" on-invalid="null"/>
</service>
<service id="ic_domain_security.api_mock_service"
class="IC\DomainSecurityBundle\Tests\Mock\ApiMockService"
public="false">
<call method="setLogger">
<argument type="service" id="logger" on-invalid="null"/>
</call>
</service>
...
</services>
The line <argument type="service" id="%api_client.api_service.id%"/> is trying to define the service to pass based on a parameter. So I’m eventually trying to push out the api_client.api_service.id parameter to configuration file(s).
Unfortunately it doesn’t appear to be possible to define service ids using parameters and I’m getting the following error:
ServiceNotFoundException: The service "ic_domain_security.api_client" has a dependency on a non-existent service "%api_client.api_service.id%".
Is there a way do what I explained above so that service ids can be passed using configuration parameters?
The best way to do what you are trying to do is to define a semantic configuration for your bundle. Anyway I don’t think that you can do this without writing some PHP code.
This means that you should write a Configuration class and a Dependency Injection Extension class. It is quite an easy process, though it is not exhaustively documented on Symfony2 docs. My advice is to take some simple bundle (e.g. I learned very much reading FOSUserBundle) and see how they define their configuration.
Here you can find an introduction to the DIC extension system.