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 #3021898: Support _defaults key in service.yml files for public, tags and autowire settings.

Proposed resolution

Implement this by copying code from YamlFileLoader once #3021898: Support _defaults key in service.yml files for public, tags and autowire settings has landed.

Remaining tasks

User interface changes

None

API changes

@todo

Data model changes

None

Release notes snippet

@todo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

alexpott’s picture

Status: Active » Postponed

I don't think this is that useful without autowiring and defaults

[Edit: @Wim Leers is right]

wim leers’s picture

I don't think this is that useful with autowiring and defaults

without, I think?

fgm’s picture

Probably:

  • AIUI without autowiring, the discovered services will not have a name so won't be usable
  • Without a public:false default, the discovered services could litter the container
alexpott’s picture

@fgm yep that's why the issue summary says:

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 and #3021803: Document and add tests for service autowiring and #3021898: Support _defaults key in service.yml files for public, tags and autowire settings

Also it's not just making things private - there are other optimisation passes Symfony are doing that we probably should take a look at.

Didn't know what AIUI stands for - according to the Internet it might be "As I Understand it" - so going on that - they would get names based on their class name even without auto-wiring. But still I'm not sure that supporting this is worth it without #3021803: Document and add tests for service autowiring as already commented because it's the automatically getting everything in the container + auto-wiring that saves all the boilerplate.

alexpott’s picture

Note that adding support for this is not simple because in Symfony land this ability depends on code in symfony/config and we've avoided including that. See #1632930: Add Symfony/Component/Config despite Symfony potentially releasing BC-breaking updates after 2.3. for some discussion. Note we can go our own way here we just need code to do that.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jelle_s’s picture

RE #7: I'm not very much in favor of reinventing the wheel, when the code that provides us with this functionality already exists in Symfony. Based on my questions, and Lynn's answers in https://github.com/symfony/symfony/issues/32847, maybe we should reconsider using the symfony config component? Lynn made some very good points in that issue.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

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 #3111008: Use native Symfony YamlFileLoader

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.

dpi’s picture

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

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

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.