Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Boot/DrupalBoot8.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public function bootstrapDrupalFull()
// The upshot is that the list of console commands is not available
// until after $kernel->boot() is called.
$container = \Drupal::getContainer();

// Set the command info alterers.
if ($container->has(DrushServiceModifier::DRUSH_COMMAND_INFO_ALTERER_SERVICES)) {
$serviceCommandInfoAltererlist = $container->get(DrushServiceModifier::DRUSH_COMMAND_INFO_ALTERER_SERVICES);
$commandFactory = Drush::commandFactory();
foreach ($serviceCommandInfoAltererlist->getCommandList() as $altererHandler) {
$commandFactory->addCommandInfoAlterer($altererHandler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably write a debug log message if a module adds one of these.

}
}

$serviceCommandlist = $container->get(DrushServiceModifier::DRUSH_CONSOLE_SERVICES);
if ($container->has(DrushServiceModifier::DRUSH_CONSOLE_SERVICES)) {
foreach ($serviceCommandlist->getCommandList() as $command) {
Expand Down
5 changes: 5 additions & 0 deletions src/Drupal/DrushServiceModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DrushServiceModifier implements ServiceModifierInterface
const DRUSH_CONSOLE_SERVICES = 'drush.console.services';
// Holds list of command classes implemented with annotated commands
const DRUSH_COMMAND_SERVICES = 'drush.command.services';
// Holds list of command info alterer classes.
const DRUSH_COMMAND_INFO_ALTERER_SERVICES = 'drush.command_info_alterer.services';
// Holds list of classes implementing Drupal Code Generator classes
const DRUSH_GENERATOR_SERVICES = 'drush.generator.services';

Expand All @@ -26,6 +28,8 @@ public function alter(ContainerBuilder $container)
$container->addCompilerPass(new FindCommandsCompilerPass(self::DRUSH_CONSOLE_SERVICES, 'console.command'));
$container->register(self::DRUSH_COMMAND_SERVICES, 'Drush\Command\ServiceCommandlist');
$container->addCompilerPass(new FindCommandsCompilerPass(self::DRUSH_COMMAND_SERVICES, 'drush.command'));
$container->register(self::DRUSH_COMMAND_INFO_ALTERER_SERVICES, 'Drush\Command\ServiceCommandlist');
$container->addCompilerPass(new FindCommandsCompilerPass(self::DRUSH_COMMAND_INFO_ALTERER_SERVICES, 'drush.command_info_alterer'));
$container->register(self::DRUSH_GENERATOR_SERVICES, 'Drush\Command\ServiceCommandlist');
$container->addCompilerPass(new FindCommandsCompilerPass(self::DRUSH_GENERATOR_SERVICES, 'drush.generator'));
}
Expand All @@ -42,6 +46,7 @@ public function check($container_definition)
return
isset($container_definition['services'][self::DRUSH_CONSOLE_SERVICES]) &&
isset($container_definition['services'][self::DRUSH_COMMAND_SERVICES]) &&
isset($container_definition['services'][self::DRUSH_COMMAND_INFO_ALTERER_SERVICES]) &&
isset($container_definition['services'][self::DRUSH_GENERATOR_SERVICES]);
}
}
32 changes: 32 additions & 0 deletions tests/CommandInfoAlterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Unish;

use Webmozart\PathUtil\Path;

/**
* @group commands
*
*/
class CommandInfoAlterTest extends CommandUnishTestCase
{
use TestModuleHelperTrait;

/**
* Tests command info alter.
*/
public function testCommandInfoAlter()
{
$this->setUpDrupal(1, true);
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
$this->drush('pm-enable', ['woot']);
$this->drush('woot:altered', [], ['help' => true]);
$this->assertNotContains('woot-initial-alias', $this->getOutput());
$this->assertContains('woot-new-alias', $this->getOutput());

// Try to run the command with the initial alias.
$this->drush('woot-initial-alias', [], [], null, null, self::EXIT_ERROR);
// Run the command with the altered alias.
$this->drush('woot-new-alias');
}
}
4 changes: 4 additions & 0 deletions tests/resources/modules/d8/woot/drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ services:
arguments: ['@module_handler']
tags:
- { name: drush.generator }
woot.command_info_alter:
class: Drupal\woot\WootCommandInfoAlterer
tags:
- { name: drush.command_info_alterer }
10 changes: 10 additions & 0 deletions tests/resources/modules/d8/woot/src/Commands/WootCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ public function tryFormatters($options = ['format' => 'table', 'fields' => ''])
];
return new RowsOfFields($outputData);
}

/**
* This command info is altered.
*
* @command woot:altered
* @aliases woot-initial-alias
*/
public function wootAltered()
{
}
}
16 changes: 16 additions & 0 deletions tests/resources/modules/d8/woot/src/WootCommandInfoAlterer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Drupal\woot;

use Consolidation\AnnotatedCommand\CommandInfoAltererInterface;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;

class WootCommandInfoAlterer implements CommandInfoAltererInterface
{
public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance)
{
if ($commandInfo->getName() === 'woot:altered') {
$commandInfo->setAliases('woot-new-alias');
}
}
}