Repository navigation
Closed
Milestone
Description
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\ServiceNameof your service by executing from your Drupal 8 sitedrupal 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:
- [meta] Register commands as services [meta] Register commands as services #2225
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
Metadata
Metadata
Assignees
Labels
No labels
Activity
jmolivas commentedon Aug 18, 2016
I already take care of:
jmolivas commentedon Aug 18, 2016
Woking on
Cache Namespace.enzolutions commentedon Aug 19, 2016
Working in Chain
enzolutions commentedon Aug 20, 2016
Working in Config
[hechoendrupal#2593] Register cache namespace as services.
jmolivas commentedon Aug 20, 2016
Working on Create
[create:vocabularies] hechoendrupal#2593 - Register on drupal container.
2593 register create services (#2598)
[hechoendrupal#2593] Add ExtensionManager class.
[hechoendrupal#2593] Add ExtensionManager class.
[console] Add extension manager service. (#2600)
[hechoendrupal#2593] Update ExtensionManager class.
[#2593] Update ExtensionManager class. (#2601)
jmolivas commentedon Aug 22, 2016
Working on Cron
[hechoendrupal#2593] Register cron:debug command.
172 remaining items
novia713 commentedon Oct 18, 2016
PR for
translationcommands in Develop at #2830