5

I would like to detect from code the current running drush command. I have some hooks that will be run when I exec some drush commands and want to detect which command is triggering the hook.

I manage to get the info from input() but I want to know if there is a more appropriate way:

 if (PHP_SAPI === 'cli') { $command = \Drush\Drush::input()->getArguments() } 

I would like to have something similar to this:

 if (PHP_SAPI === 'cli') { $command = \Drush\Drush::...getCurentCommand(); } 

Similar to Drupal::routeMatch()->getRouteName() in someway.

Thank you

1

1 Answer 1

5

A more appropriate way would be to use the object data you get as arguments. Most hooks receive $commandData from where you can get the first argument (the command):

$input = $commandData->input(); $command_name = $input->getFirstArgument(); 

Similar to the route name of the matched route you could get the full command name of the matched command via the annotations:

$full_command_name = $commandData->annotationData()->get('command'); 

If you need to use static code then get the command name as the first argument like in the question and convert it to the full command name:

$command_name = \Drush\Drush::input()->getFirstArgument(); $full_command_name = \Drush\Drush::getApplication()->find($command_name)->getName(); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.