Skip to content

[meta] Register commands as services #2225

Closed

Description

@jmolivas

This issue only applies to non-generator commands

Problem/Motivation

Register DC core commands as services on the DC container.

Why

  • Improve command discover by using code like this:
$tags = $this->container->findTaggedServiceIds('console.command');
foreach ($tags as $name => $tags) {
  $this->add($this->getContainerHelper()->get($name));
}

Solution

Remove use keyword
use Drupal\Console\Command\Command;
# or 
use Drupal\Console\Command\ContainerAwareCommand;

Add new use keyword:

use Symfony\Component\Console\Command\Command;

Update class to extend from Command

NOTE: Instead of Command or ContainerAwareCommand

Before:
class AwesomeCommand extends Command
# or
class AwesomeCommand extends ContainerAwareCommand
After
class AwesomeCommand extends Command

Add use keyword for trait

For commands previously extending Command

Add use keyword

use Drupal\Console\Command\Shared\CommandTrait;

Add use trait:

use CommandTrait; 
For commands previously extending ContainerAwareCommand

Add use keyword

use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;

Add use trait:

use ContainerAwareCommandTrait; 

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

awesome:
   class: Drupal\Console\Command\AwesomeCommand
    tags:
      - { name: console.command }

NOTE: Make sure you get latest changes from Drupal Console repository for instructions Keeping your fork up to date

Replace calls to any of the helpers on the Command class
Before After
$this->getSite(); $this->getApplication()->getSite();
$this->getChain(); $this->get('chain_queue');
$this->getStringHelper(); $this->getApplication()->getStringHelper();
$this->getValidator(); $this->getApplication()->getValidator();
$this->getDrupalHelper(); $this->get('site');
$this->getKernelHelper(); $this->getApplication()->getKernelHelper();
$this->getShowFileHelper(); $this->get('showfile');
$this->getRenderHelper() $this->getApplication()->getRenderHelper()
$this->getNestedArrayHelper() $this->getApplication()->getNestedArrayHelper()
$this->getCommandDiscoveryHelper() $this->getApplication()->getCommandDiscoveryHelper()
$this->getRemoteHelper() $this->getApplication()->getRemoteHelper()
$this->getHttpClientHelper() $this->getApplication()->getHttpClientHelper()
$this->getDrupalApi() $this->getApplication()->getDrupalApi()
$this->getContainerHelper() $this->getApplication()->getContainerHelper()

Replace any calls:

Before After
$this->getService('SERVIDE_NAME'); $this->getDrupalService('SERVIDE_NAME');
$this->hasService('SERVIDE_NAME'); $this->hasDrupalService('SERVIDE_NAME');

Completed Namespace

  • Miscellaneous without Namespace.
  • Breakpoints Namespace
  • Chain Namespace
  • Config Namespace
  • Create Namespace
  • Cron Namespace
  • Database Namespace
  • Develop Namespace
  • Exclude Namespace
  •  Generate Namespace (This will be done in another issue)
    
  • Image Namespace
  • Libraries Namespace
  • Migrate Namespace (WIP)
  • Module Namespace
  • Multisite Namespace
  • Node Namespace
  • Rest Namespace
  • Router Namespace
  • Self Namespace
  • Settings Namespace
  • Site Namespace
  • State Namespace
  • Test Namespace
  • Theme Namespace
  • Update Namespace
  • User Namespace
  • Views Namespace
  • Yaml Namespace

Activity

  1. changed the title [-]Register commands as services[/-] [+][meta] Register commands as services[/+] on May 8, 2016
  2. jmolivas commented on May 9, 2016

    @jmolivas
    MemberAuthor

    [plugin:debug] Register Command as a Service. #2227 by @omero

  3. added a commit that references this issue on May 9, 2016
  4. 14 remaining items

  5. Saphyel commented on Jun 12, 2016

    @Saphyel
    Contributor

    Theme is already merge in, and Yaml on the way..? #2417

  6. added a commit that references this issue on Jun 22, 2016
  7. jmolivas commented on Dec 2, 2016

    @jmolivas
    MemberAuthor

    Yay completed thank you all the participants on this issue

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions