Support resource key in services.yml

Created on 20 December 2018, over 5 years ago
Updated 19 February 2024, 7 months ago

Problem/Motivation

Symfony allows you to specify a resource key in your services.yml file. This allows you to search a location and create services automatically.

In Symfony it often looks like

# config/services.yaml
services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

    # ...

Whilst resource: './src/*' would create way to many services if you organise your project so that all services are in src/services then this could be useful especially when combined with πŸ“Œ Support _defaults key in service.yml files for public, tags and autowire settings Fixed .

Proposed resolution

Implement this by copying code from YamlFileLoader once πŸ“Œ Support _defaults key in service.yml files for public, tags and autowire settings Fixed has landed.

Remaining tasks

User interface changes

None

API changes

@todo

Data model changes

None

Release notes snippet

@todo

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States effulgentsia

    There's a use-case for this in πŸ“Œ Add symfony/config to core's dependencies for package_manager Closed: duplicate .

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    We have autowiring and autoconfiguration now; this would be useful so you could say

    services:
        _defaults:
            autowire: true
            autoconfigure: true
    
        Drupal\module\EventSubscriber\:
            resource: 'src/EventSubscriber'
    

    and then the container would automatically discover all event subscribers in that directory; to add a new one, you just add a new file and rebuild the container. We could even implement this sort of thing by default for modules so they don't necessarily have to write a services.yml at all!

    However, there is quite a lot of Symfony code to port over. YamlFileLoader relies on the parent FileLoader class to do a bunch of discovery work here. The two options are:

    1. Copy and refactor a number of methods from Symfony into YamlFileLoader
    2. Implement ✨ Use native Symfony YamlLoader + Config Needs work

    Symfony's container has diverged quite a lot from ours. I am leaning towards option 2 as it is simply a lot less work, and we would get all Symfony 3/4/5/6's features that we haven't implemented yet for free or almost free.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    I've created an alternative proposal to resource that'll help us advance an equivalent more suitable in the Drupal context @ ✨ Directory based automatic service creation Needs review .

    Perhaps it'll be easier to get something more Drupaly in.

Production build 0.71.5 2024