[policy] Service names should use class names and use autowiring

Created on 22 August 2024, 5 months ago
Updated 27 August 2024, 5 months ago

Problem/Motivation

Typically when we add a service to a module's services.yml file we currently do something like:

  entity_type.manager:
    class: Drupal\Core\Entity\EntityTypeManager
    arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@string_translation', '@class_resolver', '@entity.last_installed_schema.repository', '@service_container']
    tags:
      - { name: plugin_manager_cache_clear }
  Drupal\Core\Entity\EntityTypeManagerInterface: '@entity_type.manager'
  cache_contexts_manager:
    class: Drupal\Core\Cache\Context\CacheContextsManager
    arguments: ['@service_container', '%cache_contexts%' ]
  Drupal\Core\Cache\Context\CacheContextsManager: '@cache_contexts_manager'

In future new services being added to services.yml should use the interface name or class name (if there is no interface) and autowiring.

  _defaults:
    autowire: true
  Drupal\Core\Entity\EntityTypeManagerInterface:
    class: Drupal\Core\Entity\EntityTypeManager
    tags:
      - { name: plugin_manager_cache_clear }
  Drupal\Core\Cache\Context\CacheContextsManager: ~

Remaining tasks

Discuss and update/create relevant docs.

User interface changes

None

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

@tbd

🌱 Plan
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 11 hours ago

Created by

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

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

Comments & Activities

  • Issue created by @alexpott
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    +1 - unless we have a good reason to have a friendly alias name for a service, it should just be named after the class it is implemented in, because naming things is hard - we are very inconsistent in our service naming at present - and so this removes the decision entirely.

    πŸ“Œ Autowire core modules that do not require explicit configuration Postponed proposes making the autowiring change to existing core modules and πŸ“Œ Use autowiring for core modules and services Needs work to most core services.

    If this is accepted I think we need a sister issue to rename core module services (with deprecated aliases for the old names), and eventually, one for core services too.

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

    I’ve pretty much adopted this as a pattern for the last year or so, it’s great

    In case it’s not obvious, you can also access services by class name, since the proposed convention is simply making the service id the same as the backing class/interface.

    \Drupal::service(Drupal\Core\Entity\EntityTypeManagerInterface::class)

Production build 0.71.5 2024