Skip to content

[meta] Register commands in the Drupal container. #2593

Closed

Description

@jmolivas

Problem/Motivation

Register Drupal Console commands as services on the Drupal container.

Why

  • Improve command discover and take advantage of dependency injection.

Solution

Extend from Symfony\Component\Console\Command\Command

Add use keyword if not present

use Symfony\Component\Console\Command\Command;

Update class to extend from Command

class AwesomeCommand extends Command

Add CommandTrait

Add use keyword if not present

use Drupal\Console\Command\Shared\CommandTrait;

If command is using

Drupal\Console\Command\Shared\ContainerAwareCommandTrait;

Update to

Drupal\Console\Command\Shared\CommandTrait;

Add use trait:

use CommandTrait; 

Register command class as a service at config/services/NAMESPACE.yml

console.awesome:
   class: Drupal\Console\Command\AwesomeCommand
   arguments: ['@service_id', '@other_service_id', '@another_service_id']
   tags:
      - { name: drupal.command }

NOTE: If command already registered make sure you remove comment definition and confirm service name is prepended with console..

How to know which services to inject

Any call to:

$this->getService('SERVICE_NAME')
$this->getDrupalService('SERVICE_NAME')
$this->get('SERVICE_NAME')
\Drupal::service('SERVICE_NAME')
\Drupal::getContainer()->get('SERVICE_NAME')
$this->getApplication()->getContainerHelper()->get('SERVICE_NAME')

Should be added as argument: ['@SERVICE_NAME'] on the service definition.

Inject service add use, property and constructor method.

...
use Vendor\Package\ServiceName;
...

    /**
     * @var ServiceName
     */
    protected $serviceName; 

    /**
     * AwesomeCommand constructor.
     * @param ServiceName $serviceName
     */
    public function __construct(ServiceName $serviceName) {
        $this->serviceName = $serviceName;
        parent::__construct();
    }

NOTES:

  • You can find the Vendor\Package\ServiceName of your service by executing from your Drupal 8 site drupal container:debug SERVICE_NAME.
  • Make sure you call the parent::__construct(); on the added __constructmethod.

Some methods and classes has been relocated to new service and maybe renamed, this is the table mapping those changes.

Current New Service name New Method name
$this->get('site')->loadLegacyFile console.site loadLegacyFile

How to test

Read setup instructions at https://gist.github.com/jmolivas/97bbd07f328217be3564a434c5bd2618

Related documentation

Related issues:

Completed Namespace

  • Miscellaneous without Namespace.
  • Breakpoints Namespace
  • Cache Namespace
  • Chain Namespace
  • Config Namespace
  • Create Namespace
  • Cron Namespace
  • Database Namespace
  • Develop Namespace (@langelhc)
  • Entity Namespace
  • Exclude Namespace
  • Exec Namespace (relocated to drupal-core)
  • File Namespace
  • Generate Namespace (This will be done at the issue #2679)
  • Image Namespace
  • Libraries Namespace
  • Locale Namespace (@novia713)
  • Migrate Namespace
  • Module Namespace (@enzolutions)
  • Multisite Namespace
  • Node Namespace
  • Queue Namespace
  • Rest Namespace
  • Router Namespace
  • Self Namespace (relocated to drupal-launcher)
  • Shared Namespace (traits will be updated as required)
  • Site Namespace (@jmolivas)
  • State Namespace
  • Test Namespace
  • Theme Namespace
  • Update Namespace
  • User Namespace
  • Views Namespace
  • Yaml Namespace
  • Make sure you add a comment on the namespace you take to work to avoid working on same namespaces.
  • Make sure you get latest changes from Drupal Console repository for instructions Keeping your fork up to date

Activity

  1. jmolivas commented on Aug 18, 2016

    @jmolivas
    MemberAuthor

    I already take care of:

    • Miscellaneous without Namespace.
    • Breakpoints Namespace
  2. jmolivas commented on Aug 18, 2016

    @jmolivas
    MemberAuthor

    Woking on Cache Namespace.

  3. enzolutions commented on Aug 19, 2016

    @enzolutions
    Contributor

    Working in Chain

  4. enzolutions commented on Aug 20, 2016

    @enzolutions
    Contributor

    Working in Config

  5. added a commit that references this issue on Aug 20, 2016
  6. jmolivas commented on Aug 20, 2016

    @jmolivas
    MemberAuthor

    Working on Create

  7. added 7 commits that reference this issue on Aug 21, 2016
  8. jmolivas commented on Aug 22, 2016

    @jmolivas
    MemberAuthor

    Working on Cron

  9. added a commit that references this issue on Aug 22, 2016
  10. 172 remaining items

  11. removed this from the 1.0.0-rc5 milestone on Oct 6, 2016
  12. modified the milestones: 1.0.0-rc6, 1.0.0-rc7 on Oct 17, 2016
  13. novia713 commented on Oct 18, 2016

    @novia713
    Contributor

    PR for translation commands in Develop at #2830

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions