Skip to content

Commit

Permalink
Feature to conditionally hide commands
Browse files Browse the repository at this point in the history
If a commands class has a `public string|array $hiddenCommands` property then any command listed in the property will not be added to Robo

Closes consolidation#1013
  • Loading branch information
RyanNerd committed Jul 4, 2021
1 parent 85efe28 commit 5693962
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,34 @@ public function registerCommandClass($app, $commandClass)
$container = Robo::getContainer();
$roboCommandFileInstance = $this->instantiateCommandClass($commandClass);
if (!$roboCommandFileInstance) {
return;
return null;
}

// Register commands for all of the public methods in the RoboFile.
$commandFactory = $container->get('commandFactory');
$commandList = $commandFactory->createCommandsFromClass($roboCommandFileInstance);

// Get all of the hidden commands if there is a $hiddenCommands property in $roboCommandFileInstance
$hiddenCommands = $roboCommandFileInstance->hiddenCommands ?? [];
// If $hiddenCommands is a string then convert it to an array
if (is_string($hiddenCommands)) {
$hiddenCommands = [$hiddenCommands];
}
// If $hiddenCommands isn't an array at this point then make it an empty array
if (!is_array($hiddenCommands)) {
$hiddenCommands = [];
}

foreach ($commandList as $command) {
// If the command is in the $hiddenCommands array then don't add the command
if (in_array($command->getName(), $hiddenCommands)) {
continue;
}
// If the command has a @hidden annotation then don't add the command
$annotations = $command->getAnnotationData();
if ($annotations && $annotations->has('hidden')) {
continue;
}
$app->add($command);
}
return $roboCommandFileInstance;
Expand Down

0 comments on commit 5693962

Please sign in to comment.