Problem/Motivation

"Autowiring" service parameters to ContainerInjectionInterface and ContainerFactoryPluginInterface classes has been supported since #3394870: Allow controller service wiring via constructor parameter attributes and #3452852: Add create() factory method with autowired parameters to PluginBase.

One feature of making service definitions autowired is that setter injection methods with the #[Symfony\Contracts\Service\Attribute\Required] attribute are automatically called on service instantiation.

This functionality could be useful to be extended to ContainerInjectionInterface and ContainerFactoryPluginInterface classes via AutowireTrait/AutowiredInstanceTrait. For example, if there is a base plugin class like this:

abstract class ExamplePluginBase extends PluginBase {

  public function __construct(
    protected readonly Service1Interface $service1,
    protected readonly Service2Interface $service2,
    protected readonly Service3Interface $service3,
  ) {}
  ...
}

then currently, a plugin class extending ExamplePluginBase would need to inject additional services by appending parameters to the constructor:

abstract class ExamplePlugin1 extends ExamplePluginBase {

  public function __construct(
    Service1Interface $service1,
    Service2Interface $service2,
    Service3Interface $service3,
    protected readonly SubclassOnlyServiceInterface $subclassOnlyService,
  ) {
    parent::__construct($service1, $service2, $service3);
  }
  ...
}

Introducing the use of #Required could make it look like this instead:

abstract class ExamplePlugin1 extends ExamplePluginBase {

  #[Required]
  public function setSubclassOnlyService(SubclassOnlyServiceInterface $subclassOnlyService) : void {
    $this->$subclassOnlyService = $subclassOnlyService
  }
  ...
}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3558306

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

godotislate created an issue. See original summary.

godotislate’s picture

Issue summary: View changes

longwave made their first commit to this issue’s fork.

longwave’s picture

Status: Active » Needs review

Added an initial attempt at this.

Disclaimer: parts of this MR were written by Google Gemini but have been manually reviewed and tweaked

longwave’s picture

Issue tags: +Needs change record
longwave’s picture

Issue tags: -Needs change record

Added a CR.

claudiu.cristea’s picture

Nice!

  • In the example from IS the function returns the current instance (return $this;) but this is not happening in the CR example. Is method chaining a better pattern in this case, should we recommend it in CR?
  • What if we need to inject 2 (or more) additional services? Reading the code, it seems possible with a single method. But I think we should make it clear by improving the example from CR
  • The only thing we lose is the constructor property promotion for the new protected properties. But it seems there's nothing we can do about it.
godotislate’s picture

Is method chaining a better pattern in this case, should we recommend it in CR?

I prefer allowing method chaining in setters, even when it's not obvious what the use case would be, but I think this might be a personal preference thing otherwise.

What if we need to inject 2 (or more) additional services? Reading the code, it seems possible with a single method.

Yes, similar to using #[Required] in service classes, you can add as many required methods as you want, and they will all run. Or you can have one method that does all the things.

longwave’s picture

To me method chaining isn't important to show here because we don't want to encourage users to call these methods manually, they are only meant to wire services at instantiation time (and similar to the property promotion problem mentioned, we can't use readonly properties either because they can only be set from the constructor).

godotislate’s picture

Another thing to consider going forward is that if we use introduce the use of these in parent classes, it's possible that child classes already implement ::create() without autowiring. In those cases, maybe getters with a \Drupal::service() fallback value would be needed.

godotislate’s picture

MR and CR look good, though I had one comment on the MR about the exception message.

godotislate’s picture

Version: 11.x-dev » main

Applied my own suggestion, since it's just an exception message. Though I noticed that the MR is somehow still targeted against 11.x, so that will need be updated to main.

godotislate’s picture

Status: Needs review » Needs work

NW for #13.

longwave’s picture

Status: Needs work » Needs review

I don't have a better suggestion either. Maybe we could split the exception message for the two cases, but maybe it doesn't matter that much in practice.

Rebased on to main and edited the MR to match.

godotislate’s picture

Status: Needs review » Needs work

A couple kernel test failures bc of the changed exception message:
core/tests/Drupal/KernelTests/Core/Block/AutowireBlockTest.php
core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php

I think the FJ test failure is unrelated.

longwave’s picture

Regarding method chaining, I just saw that in Symfony returning static has special meaning: https://symfony.com/doc/current/service_container/injection_types.html#i... - we don't otherwise support or use this pattern so I think we should avoid it in core until we know we want it.

longwave’s picture

Status: Needs work » Needs review

Fixed the tests.

godotislate’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Edited the example in the IS to return void instead of static.

lgtm.

  • catch committed b16bc871 on 11.x
    feat: #3558306 Support automatic setter injection using the #[Required]...

  • catch committed 40419648 on main
    feat: #3558306 Support automatic setter injection using the #[Required]...
catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Comitted/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

longwave’s picture

Thanks, also published the CR.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

liam morland’s picture

Post removed.