Add support for automatically wiring dependencies without implementing ContainerInjectionInterface

Created on 19 December 2023, over 1 year ago
Updated 13 March 2024, about 1 year ago

Problem/Motivation

In πŸ“Œ Allow controller service wiring via constructor parameter attributes Fixed added a AutowireTrait for automatically wiring dependencies from the container. Now in most cases the only declared method in Drupal\Core\DependencyInjection\ContainerInjectionInterface will be implemented in a trait, doesn't look very good.

Proposed resolution

Add support for automatically wiring dependencies in Drupal\Core\DependencyInjection\ClassResolver::getInstanceFromDefinition() if the definition does not implement Drupal\Core\DependencyInjection\ContainerInjectionInterface

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated 34 minutes ago

Created by

πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

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

Merge Requests

Comments & Activities

  • Issue created by @chesn0k
  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa
  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

    chesn0k β†’ changed the visibility of the branch 3409787-add-support-for to hidden.

  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

    chesn0k β†’ changed the visibility of the branch 3409787-add-support-for to active.

  • Pipeline finished with Failed
    over 1 year ago
    Total: 179s
    #65942
  • Pipeline finished with Failed
    over 1 year ago
    Total: 635s
    #65945
  • Pipeline finished with Success
    over 1 year ago
    Total: 874s
    #66036
  • Pipeline finished with Success
    over 1 year ago
    Total: 617s
    #66039
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa
  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for posting the MR.

    Will need test coverage to ensure nothing breaks it in the future.

  • Pipeline finished with Success
    over 1 year ago
    #75228
  • Pipeline finished with Failed
    over 1 year ago
    Total: 180s
    #75258
  • Pipeline finished with Success
    over 1 year ago
    #75730
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks! Change looks good to me but probably need to get reviewed by those more failure with the auto-wiring.

  • Status changed to Needs work about 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    This looks like a nice feature but I think we should reuse (or refactor) AutowireTrait here if we can.

  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

    Also, taking another look at the code, I noticed that there is no support for the union and intersection types. Should I put this in a separate issue?

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    What do you suggest instead of the trait? A service specifically for handling autowiring?

    Yes, please open a new issue for union/intersection types. I can see a use for union types, not sure about intersection types.

  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

    service specifically for handling autowiring

    Good option, although I thought that this was the responsibility of the class resolver.

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Plugins should also be autowireable, see πŸ“Œ Allow plugin service wiring via constructor parameter attributes Needs work , hopefully we can unify some of the code there as well?

  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

    Plugins should also be autowireable, see #3294266: Allow plugin service wiring via constructor parameter attributes, hopefully we can unify some of the code there as well?

    Yes, this seems like a good reason to move autowire into a separate service.

    This should also mean we can drop AutowireTrait from ControllerBase?

    Yes.

  • Pipeline finished with Failed
    about 1 year ago
    Total: 179s
    #99293
  • Pipeline finished with Failed
    about 1 year ago
    Total: 476s
    #99710
  • Pipeline finished with Success
    about 1 year ago
    Total: 519s
    #99738
  • Status changed to Needs review about 1 year ago
  • πŸ‡ΊπŸ‡¦Ukraine chesn0k Ukraine, Odessa

    And so the current implementation of autowiring allows create a plugin instance in this way:

    $instance = \Drupal::service(DependencyAutowire::class)->autowireClass(
      new \ReflectionClass($plugin_class),
      [
        'configuration' => $configuration,
        'plugin_id' => $plugin_id,
        'plugin_definition' => $plugin_definition,
      ],
    );
    

    Plugin __construct:

    public function __construct(
      array $configuration,
      string $plugin_id,
      array $plugin_definition,
      protected readonly RouteMatchInterface $routeMatch,
    ) {
      parent::__construct($configuration, $plugin_id, $plugin_definition);
    }
    

    This is not bad, but I think the __construct method signature is too strict, plugins that not required to extends the base plugin could use property promotion:

    public function __construct(
      protected array $configuration,
      protected string $pluginId,
      protected array $pluginDefinition,
      protected readonly RouteMatchInterface $routeMatch,
    ) {}
    

    I think we should search for arguments using snake_case and camelCase styles this can be implemented inside the dependency autowiring service.

    Also should I remove all uses of AutowireTrait in current branch and mark as deprecated?

  • Status changed to Needs work about 1 year ago
  • The Needs Review Queue Bot β†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.71.5 2024