Skip to content

Consider removing DI questions from generators #176

Description

@Chi-teck

For most services, plugin and controllers DCG asks a user which services he would like to inject.

DCG service question

That allows to save a user form typing a lot of boilerplate code. However times have changed. PHP improved the type system and implemented property promotions. Drupal added support for service auto-wiring.

Here is example of a service in Drupal 8 style. Imagine how it would look if it had say 5 dependencies.

example:
  class: Drupal\example\Example
  arguments: ['@entity_type.manager'
<?php

namespace Drupal\example;

use Drupal\Core\Entity\EntityTypeManagerInterface

/**
 * The example service.
 */
class Example implements ExampleInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The object constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

}

Here is the same service in Drupal 11 style.

<?php

namespace Drupal\example;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * The example service.
 */
final class Example implements ExampleInterface {

  /**
   * The object constructor.
   */
  public function __construct(private EntityTypeManagerInterface $entity_type_manager) {  }

}

From the above example seems that asking a user to provide dependencies for a service does not make much sense this times. It can easily add them to the generated code later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions