Member-only story
Auto discovery of global commands in Drush
This blog post is about the auto-discovery of global Drush commands. To be honest, I have no idea what a global command is or means and how it differentiates between a site command. However, I find the global-command discovery feature fascinating and a streamlined developer experience when writing commands for your Drupal site. Note: this feature is available in Drush 10.5+ or 11.0+.
Some Drush commands can already be auto-discovered. Those are site-wide commands. Site-wide Drush commands exist in your Drupal codebase’s drush/Commands directory and use the Drush\Commands
namespace.
I am working on a new Drupal book and dove into a section that discussed writing Drush commands for your Drupal module. While Drush provides a command to generate Drush commands, I wanted to brush up on the manual steps and help explain the generated code. That’s when I saw the section on auto-discovered commands.
Drush global commands can be auto-discovered if they meet the following conditions:
- The command is in a class that is PSR-4 auto-loadable
- The namespace for the command class contains
Drush\Commands
. SoApp\Drush\Commands
would be a viable namespace. - The command class ends with
DrushCommands
. So the filename would beAppDrushCommands
orFooDrushCommands
, etc.
Personal note: I find the file naming requirement a bit redundant since we already have a specific namespace, and the class is inspected to…