Symfony2 conditional service declaration Symfony2 conditional service declaration symfony symfony

Symfony2 conditional service declaration


I finally found a solution that looks solid to me. As of Symfony 2.4 you can use the expression syntax: Using the Expression Language

So I configured my service this way.

service.ymlparameters:  httpDriver.class:       HTTP\Driver\Driver  httpMockDriver.class:   HTTP\Driver\MockDriver  myAwesomeService.class: My\Awesome\Serviceservice:  myAwesomeService:    class:        "%myAwesomeService.class%"    arguments:          - "@=service('service_container').get('kernel.environment') == 'test'? service('httpMockDriver) : service('httpDriver)"

This works for me.