Service arguments by name or type in *.services.yml with autowiring

Created on 26 January 2023, about 2 years ago

Problem/Motivation

In Drupal, all the service definitions I find in *.services.yml files have explicit complete argument lists, like so:

services:
  [..]
  system.manager:
    class: Drupal\system\SystemManager
    arguments: ['@module_handler', '@request_stack', '@menu.link_tree', '@menu.active_trail']

The symfony dependency injection component also allows an advanced syntax, that is useful in combination with autowiring.
https://symfony.com/doc/current/service_container.html#choose-a-specific...
This allows to specify only _some_ of the arguments, where autowiring does not work.

A preview of this in Drupal can already be seen here:
πŸ“Œ Use autowiring for core modules and services Needs work
https://git.drupalcode.org/project/drupal/-/merge_requests/2508/diffs

services:
  block_content.uuid_lookup:
    class: \Drupal\block_content\BlockContentUuidLookup
    autowire: true
    arguments:
      $cache: '@cache.bootstrap'

While that issue is for Drupal 10, I found that the autowire already works for Drupal 9.5.
This can be useful for custom or contrib modules that already want to go ahead and use autowired services.

However:

Steps to reproduce

Use Drupal 9.5 or up.
Create custom module with autowired services and advanced arguments syntax.
Open in PhpStorm -> inspection warning.
Rebuild the container -> no problem, seems to work just fine.

Proposed resolution

Add a test case to confirm that arguments binding already works.
Backport to 9.5.x.

Make a PR at schemastore, or ship our own schema for services.yml that IDEs like PhpStorm can find and understand.

Remaining tasks

User interface changes

none

API changes

none

Data model changes

none

Release notes snippet

The Drupal dependency injection system supports binding arguments by name or type as documented in https://symfony.com/doc/current/service_container.html#choose-a-specific....
This already worked before, but now it is official.

Follow-up

We should also support the 'bind' key in '_defaults', and "Binding arguments by Name or Type"
https://symfony.com/doc/current/service_container.html#binding-arguments....
See [3295542-3]

πŸ“Œ Task
Status

Active

Version

9.5

Component
BaseΒ  β†’

Last updated about 7 hours ago

Created by

πŸ‡©πŸ‡ͺGermany donquixote

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

Comments & Activities

Production build 0.71.5 2024