Skip to content

Commit

Permalink
Update CommandInfoAlterers docs for Drush 12. Also update Woot's Comm…
Browse files Browse the repository at this point in the history
…andInfoAlterer. (#5695)

* Update command info alterer docs

* Also update Woot's CommandInfoAlterer
  • Loading branch information
weitzman committed Jul 8, 2023
1 parent ddc733f commit 3d0d136
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
11 changes: 6 additions & 5 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ The following are both valid ways to declare a command:
- [See Attributes provided by Drush core](https://www.drush.org/api/Drush/Attributes.html). Custom code can add additional attributes.

## Altering Command Info
Drush command info (annotations/attributes) can be altered from other modules. This is done by creating and registering 'command info alterers'. Alterers are class services that are able to intercept and manipulate an existing command annotation.
Drush command info (annotations/attributes) can be altered from other modules. This is done by creating and registering _command info alterers_. Alterers are classes that are able to intercept and manipulate an existing command annotation.

In order to alter an existing command info, follow the steps below:
In the module that wants to alter a command info, add a class that:

1. In the module that wants to alter a command info, add a service class that implements the `\Consolidation\AnnotatedCommand\CommandInfoAltererInterface`.
1. In the module `drush.services.yml` declare a service pointing to this class and tag the service with the `drush.command_info_alterer` tag.
1. In that class, implement the alteration logic in the `alterCommandInfo()` method.
1. The generator class namespace, relative to base namespace, should be `Drupal\<module-name>\Drush\CommandInfoAlterers` and the class file should be located under the `src/Drush/CommandInfoAlterers` directory.
1. The filename must have a name like FooCommandInfoAlterer.php. The prefix `Foo` can be whatever string you want. The file must end in `CommandInfoAlterer.php`.
1. The class must implement the `\Consolidation\AnnotatedCommand\CommandInfoAltererInterface`.
1. Implement the alteration logic in the `alterCommandInfo()` method.
1. Along with the alter code, it's strongly recommended to log a debug message explaining what exactly was altered. This makes things easier on others who may need to debug the interaction of the alter code with other modules. Also it's a good practice to inject the the logger in the class constructor.

For an example, see [WootCommandInfoAlterer](https://github.com/drush-ops/drush/blob/12.x/sut/modules/unish/woot/src/WootCommandInfoAlterer.php) provided by the testing 'woot' module.
Expand Down
5 changes: 0 additions & 5 deletions sut/modules/unish/woot/drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,3 @@ services:
class: Drupal\woot\Commands\AnnotatedGreetCommand
tags:
- { name: console.command }
woot.command_info_alter:
class: Drupal\woot\WootCommandInfoAlterer
arguments: ['@logger.factory']
tags:
- { name: drush.command_info_alterer }
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

declare(strict_types=1);

namespace Drupal\woot;
namespace Drupal\woot\Drush\CommandInfoAlterers;

use Drupal\Core\Logger\LoggerChannelInterface;
use Consolidation\AnnotatedCommand\CommandInfoAltererInterface;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class WootCommandInfoAlterer implements CommandInfoAltererInterface
{
Expand All @@ -18,6 +19,15 @@ public function __construct(LoggerChannelFactoryInterface $loggerFactory)
$this->logger = $loggerFactory->get('drush');
}

public static function create(ContainerInterface $container): self
{
$commandHandler = new static(
$container->get('logger.factory')
);

return $commandHandler;
}

public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance)
{
if ($commandInfo->getName() === 'woot:altered') {
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/CommandInfoAlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function testCommandInfoAlter()
$this->assertStringContainsString('woot-new-alias', $this->getOutput());

// Check the debug messages.
$this->assertStringContainsString('[debug] Commands are potentially altered in Drupal\woot\WootCommandInfoAlterer.', $this->getErrorOutput());
$this->assertStringContainsString("[debug] Module 'woot' changed the alias of 'woot:altered' command into 'woot-new-alias' in Drupal\woot\WootCommandInfoAlterer::alterCommandInfo().", $this->getErrorOutput());
$this->assertStringContainsString('[debug] Commands are potentially altered in Drupal\woot\Drush\CommandInfoAlterers\WootCommandInfoAlterer.', $this->getErrorOutput());
$this->assertStringContainsString("[debug] Module 'woot' changed the alias of 'woot:altered' command into 'woot-new-alias' in Drupal\woot\Drush\CommandInfoAlterers\WootCommandInfoAlterer::alterCommandInfo().", $this->getErrorOutput());

// Try to run the command with the initial alias.
$this->drush('woot-initial-alias', [], [], null, null, self::EXIT_ERROR);
Expand Down

0 comments on commit 3d0d136

Please sign in to comment.