Benutzerdefinierte Drush Commands für Drush 10 anpassen (Command is not defined)

Mit der aktuellen Drush 10 Version ist es nicht mehr möglich, Custom Commands auszuführen die noch mittels hook_drush_command() definiert wurden. Diese findet Drush schlicht nicht mehr, weshalb sie auch nicht mehr ausgeführt werden können.

Command "custom-command" is not defined.

Seit Drush 9 können eigene Kommandos nur noch über eine eigene Service Klasse definiert werden. Die bisherige my_module.drush.inc Datei ist obsolete.

Drush stellt ein eigenes Kommando bereit, um das Grundgerüst zu generieren: drush generate drush-command-file.

Grundsätzlich werden drei Dinge benötigt:

  1. composer.json
  2. drush.services.yml
  3. die Command Klasse

composer.json

snippet.json
{
    "name": "drupal/my_module",
    "type": "drupal-drush",
    "extra": {
        "drush": {
            "services": {
                "drush.services.yml": "^10"
            }
        }
    }
}

drush.services.yml

snippet.yml
services:
  my_module.commands:
    class: \Drupal\my_module\Commands\CustomCommands
    tags:
      - { name: drush.command }

Command Klasse

snippet.php
<?php
 
namespace Drupal\my_module\Commands;
 
use Drush\Commands\DrushCommands;
 
/**
 * A Drush commandfile.
 *
 * In addition to this file, you need a drush.services.yml
 * in root of your module, and a composer.json file that provides the name
 * of the services file to use.
 *
 * See these files for an example of injecting Drupal services:
 *   - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
 *   - http://cgit.drupalcode.org/devel/tree/drush.services.yml
 */
class CustomCommands extends DrushCommands {
 
  /**
   * My custom command.
   *
   * @command custom-command
   */
  public function custom() {
	  $this->output()->writeln('Hello World!');
  }
 
}