@@ -125,3 +125,50 @@ via a ``container`` variable. Here's another example:
125125
126126 Expressions can be used in ``arguments ``, ``properties ``, as arguments with
127127``configurator `` and as arguments to ``calls `` (method calls).
128+
129+ You can also use expressions as service factories:
130+
131+ ..configuration-block ::
132+
133+ ..code-block ::yaml
134+
135+ # config/services.yaml
136+ services :
137+ App\Mailer :
138+ factory :" @=parameter('some_param') ? service('some_service') : service('some_other_service')"
139+
140+ ..code-block ::xml
141+
142+ <!-- config/services.xml-->
143+ <?xml version =" 1.0" encoding =" UTF-8" ?>
144+ <container xmlns =" http://symfony.com/schema/dic/services"
145+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
146+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
147+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
148+
149+ <services >
150+ <service id =" App\Mailer" >
151+ <factory expression =" parameter('some_param') ? service('some_service') : service('some_other_service')" />
152+ </service >
153+ </services >
154+ </container >
155+
156+ ..code-block ::php
157+
158+ // config/services.php
159+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
160+
161+ use App\Mailer;
162+
163+ return function(ContainerConfigurator $configurator) {
164+ $services = $configurator->services();
165+
166+ $services->set(Mailer::class)
167+ ->factory(expr("parameter('some_param') ? service('some_service') : service('some_other_service')"));
168+ };
169+
170+ In this context, you have access to the `arg ` function that allows getting the value of arguments passed to the factory.
171+
172+ ..versionadded ::6.1
173+
174+ Using expressions as factories was introduced in Symfony 6.1.