@@ -125,3 +125,54 @@ via a ``container`` variable. Here's another example:
125
125
126
126
Expressions can be used in ``arguments ``, ``properties ``, as arguments with
127
127
``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') : arg(0)"
139
+ arguments :
140
+ - ' @some_other_service'
141
+
142
+ .. code-block :: xml
143
+
144
+ <!-- config/services.xml -->
145
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
146
+ <container xmlns =" http://symfony.com/schema/dic/services"
147
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
148
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
149
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
150
+
151
+ <services >
152
+ <service id =" App\Mailer" >
153
+ <factory expression =" parameter('some_param') ? service('some_service') : arg(0)" />
154
+ <argument type =" service" id =" some_other_service" />
155
+ </service >
156
+ </services >
157
+ </container >
158
+
159
+ .. code-block :: php
160
+
161
+ // config/services.php
162
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
163
+
164
+ use App\Mailer;
165
+
166
+ return function(ContainerConfigurator $configurator) {
167
+ $services = $configurator->services();
168
+
169
+ $services->set(Mailer::class)
170
+ ->factory(expr("parameter('some_param') ? service('some_service') : arg(0)"));
171
+ ->args([service('some_other_service')]);
172
+ };
173
+
174
+ In this context, you have access to the `arg ` function that allows getting the value of arguments passed to the factory.
175
+
176
+ .. versionadded :: 6.1
177
+
178
+ Using expressions as factories was introduced in Symfony 6.1.
0 commit comments