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