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
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:
- 3558306-support-automatic-setter
changes, plain diff MR !14260
Comments
Comment #2
godotislateComment #5
longwaveAdded an initial attempt at this.
Disclaimer: parts of this MR were written by Google Gemini but have been manually reviewed and tweaked
Comment #6
longwaveComment #7
longwaveAdded a CR.
Comment #8
claudiu.cristeaNice!
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?Comment #9
godotislateI 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.
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.Comment #10
longwaveTo 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).
Comment #11
godotislateAnother 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.Comment #12
godotislateMR and CR look good, though I had one comment on the MR about the exception message.
Comment #13
godotislateApplied 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.
Comment #14
godotislateNW for #13.
Comment #15
longwaveI 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.
Comment #16
godotislateA 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.
Comment #17
longwaveRegarding 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.
Comment #18
longwaveFixed the tests.
Comment #19
godotislateEdited the example in the IS to return void instead of static.
lgtm.
Comment #22
catchComitted/pushed to main and 11.x, thanks!
Comment #25
longwaveThanks, also published the CR.
Comment #27
liam morlandPost removed.