@@ -195,6 +195,78 @@ the service id and the method name:
195
195
# old syntax
196
196
configurator : ['@App\Mail\EmailConfigurator', configure]
197
197
198
+ .. _configurators-invokable :
199
+
200
+ .. versionadded :: 4.3
201
+
202
+ Invokable configurators for services were introduced in Symfony 4.3.
203
+
204
+ Services can be configured via invokable configurators (replacing the
205
+ ``configure() `` method with ``__invoke() ``) by omitting the method name, just as
206
+ route definitions can reference :ref: `invokable
207
+ controllers <controller-service-invoke>`.
208
+
209
+ .. code-block :: yaml
210
+
211
+ # app/config/services.yml
212
+ services :
213
+ # ...
214
+
215
+ # Registers all 4 classes as services, including AppBundle\Mail\EmailConfigurator
216
+ AppBundle\ :
217
+ resource : ' ../../src/AppBundle/*'
218
+ # ...
219
+
220
+ # override the services to set the configurator
221
+ AppBundle\Mail\NewsletterManager :
222
+ configurator : ' @AppBundle\Mail\EmailConfigurator'
223
+
224
+ AppBundle\Mail\GreetingCardManager :
225
+ configurator : ' @AppBundle\Mail\EmailConfigurator'
226
+
227
+ .. code-block :: xml
228
+
229
+ <!-- app/config/services.xml -->
230
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
231
+ <container xmlns =" http://symfony.com/schema/dic/services"
232
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
233
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
234
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
235
+
236
+ <services >
237
+ <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/*" />
238
+
239
+ <service id =" AppBundle\Mail\NewsletterManager" >
240
+ <configurator service =" AppBundle\Mail\EmailConfigurator" />
241
+ </service >
242
+
243
+ <service id =" AppBundle\Mail\GreetingCardManager" >
244
+ <configurator service =" AppBundle\Mail\EmailConfigurator" />
245
+ </service >
246
+ </services >
247
+ </container >
248
+
249
+ .. code-block :: php
250
+
251
+ // app/config/services.php
252
+ use AppBundle\Mail\GreetingCardManager;
253
+ use AppBundle\Mail\NewsletterManager;
254
+ use Symfony\Component\DependencyInjection\Definition;
255
+ use Symfony\Component\DependencyInjection\Reference;
256
+
257
+ // Same as before
258
+ $definition = new Definition();
259
+
260
+ $definition->setAutowired(true);
261
+
262
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*');
263
+
264
+ $container->getDefinition(NewsletterManager::class)
265
+ ->setConfigurator(new Reference(EmailConfigurator::class));
266
+
267
+ $container->getDefinition(GreetingCardManager::class)
268
+ ->setConfigurator(new Reference(EmailConfigurator::class));
269
+
198
270
That's it! When requesting the ``App\Mail\NewsletterManager `` or
199
271
``App\Mail\GreetingCardManager `` service, the created instance will first be
200
272
passed to the ``EmailConfigurator::configure() `` method.
0 commit comments