@@ -198,22 +198,28 @@ with the ``doctrine.event_listener`` tag:
198198 .. code-block :: php
199199
200200 // config/services.php
201+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
202+
201203 use App\EventListener\SearchIndexer;
202204
203- // listeners are applied by default to all Doctrine connections
204- $container->autowire(SearchIndexer::class)
205- ->addTag('doctrine.event_listener', [
206- // this is the only required option for the lifecycle listener tag
207- 'event' => 'postPersist',
205+ return static function (ContainerConfigurator $container) {
206+ $services = $configurator->services();
207+
208+ // listeners are applied by default to all Doctrine connections
209+ $services->set(SearchIndexer::class)
210+ ->tag('doctrine.event_listener', [
211+ // this is the only required option for the lifecycle listener tag
212+ 'event' => 'postPersist',
208213
209- // listeners can define their priority in case multiple listeners are associated
210- // to the same event (default priority = 0; higher numbers = listener is run earlier)
211- 'priority' => 500,
214+ // listeners can define their priority in case multiple listeners are associated
215+ // to the same event (default priority = 0; higher numbers = listener is run earlier)
216+ 'priority' => 500,
212217
213- # you can also restrict listeners to a specific Doctrine connection
214- 'connection' => 'default',
215- ])
216- ;
218+ # you can also restrict listeners to a specific Doctrine connection
219+ 'connection' => 'default',
220+ ])
221+ ;
222+ };
217223
218224 .. tip ::
219225
@@ -314,29 +320,35 @@ with the ``doctrine.orm.entity_listener`` tag:
314320 .. code-block :: php
315321
316322 // config/services.php
323+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
324+
317325 use App\Entity\User;
318326 use App\EventListener\UserChangedNotifier;
319327
320- $container->autowire(UserChangedNotifier::class)
321- ->addTag('doctrine.orm.entity_listener', [
322- // These are the options required to define the entity listener:
323- 'event' => 'postUpdate',
324- 'entity' => User::class,
328+ return static function (ContainerConfigurator $container) {
329+ $services = $configurator->services();
330+
331+ $services->set(UserChangedNotifier::class)
332+ ->tag('doctrine.orm.entity_listener', [
333+ // These are the options required to define the entity listener:
334+ 'event' => 'postUpdate',
335+ 'entity' => User::class,
325336
326- // These are other options that you may define if needed:
337+ // These are other options that you may define if needed:
327338
328- // set the 'lazy' option to TRUE to only instantiate listeners when they are used
329- // 'lazy' => true,
339+ // set the 'lazy' option to TRUE to only instantiate listeners when they are used
340+ // 'lazy' => true,
330341
331- // set the 'entity_manager' option if the listener is not associated to the default manager
332- // 'entity_manager' => 'custom',
342+ // set the 'entity_manager' option if the listener is not associated to the default manager
343+ // 'entity_manager' => 'custom',
333344
334- // by default, Symfony looks for a method called after the event (e.g. postUpdate())
335- // if it doesn't exist, it tries to execute the '__invoke()' method, but you can
336- // configure a custom method name with the 'method' option
337- // 'method' => 'checkUserChanges',
338- ])
339- ;
345+ // by default, Symfony looks for a method called after the event (e.g. postUpdate())
346+ // if it doesn't exist, it tries to execute the '__invoke()' method, but you can
347+ // configure a custom method name with the 'method' option
348+ // 'method' => 'checkUserChanges',
349+ ])
350+ ;
351+ };
340352
341353 Doctrine Lifecycle Subscribers
342354------------------------------
@@ -434,11 +446,17 @@ with the ``doctrine.event_subscriber`` tag:
434446 .. code-block :: php
435447
436448 // config/services.php
449+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
450+
437451 use App\EventListener\DatabaseActivitySubscriber;
438452
439- $container->autowire(DatabaseActivitySubscriber::class)
440- ->addTag('doctrine.event_subscriber')
441- ;
453+ return static function (ContainerConfigurator $container) {
454+ $services = $configurator->services();
455+
456+ $services->set(DatabaseActivitySubscriber::class)
457+ ->tag('doctrine.event_subscriber')
458+ ;
459+ };
442460
443461 If you need to associate the subscriber with a specific Doctrine connection, you
444462can do it in the service configuration:
@@ -473,11 +491,17 @@ can do it in the service configuration:
473491 .. code-block :: php
474492
475493 // config/services.php
494+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
495+
476496 use App\EventListener\DatabaseActivitySubscriber;
477497
478- $container->autowire(DatabaseActivitySubscriber::class)
479- ->addTag('doctrine.event_subscriber', ['connection' => 'default'])
480- ;
498+ return static function (ContainerConfigurator $container) {
499+ $services = $configurator->services();
500+
501+ $services->set(DatabaseActivitySubscriber::class)
502+ ->tag('doctrine.event_subscriber', ['connection' => 'default'])
503+ ;
504+ };
481505
482506 .. tip ::
483507
0 commit comments