YAML template to create a Symfony tagged iterator service

Published on 2023-01-06 • Modified on 2023-01-06

This snippet shows a YAML template to create a Symfony tagged iterator service:
1. We activate autowiring and autoconfigure.
2. We declare to assign the app.email tag to all services implementing the EmailInterface.
3. We load all services implementing the EmailInterface in a given directory.
4. We inject all the tagged services app.email into the EmailCollection service.
5. Then, we can use the EmailCollection service in other services thanks to autowiring.


services:
    # 1.
    _defaults:
        autowire: true
        autoconfigure: true

    # 2.
    _instanceof:
        App\Mailer\Email\EmailInterface:
            tags: ['app.email']

    # 3.
    App\Mailer\Email\:
        resource: '../../src/Mailer/Email/'

    # 4.
    App\Mailer\EmailCollection:
        arguments:
            - !tagged_iterator app.email

    # 5.
    App\Mailer\AppMailer: ~
        #arguments:
            #- App\Mailer\EmailCollection

 More on Stackoverflow   Read the doc  Random snippet

  Work with me!