Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Implement alias handling in CommandCollection #682

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 26 additions & 11 deletions Classes/Mvc/Cli/CommandCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class CommandCollection implements CommandLoaderInterface
*/
private $commands;

/**
* @var string[]
*/
private $aliases;

/**
* @var array
*/
Expand Down Expand Up @@ -85,12 +90,15 @@ public function __construct(RunLevel $runLevel, PackageManager $packageManager)
*/
public function get($name): BaseCommand
{
if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
}
if (!isset($this->commands[$name]) || !$this->isCommandAvailable($name)) {
throw new CommandNotFoundException(sprintf('A command with name "%s" could not be found.', $name), 1518812618);
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name), [], 1518812618);
}
$command = $this->commands[$name]['closure']();
if (isset($commandDefinition['alias'])) {
$command->setAliases([$this->commands[$name]['alias']]);
if (isset($this->commands[$name]['aliases'])) {
$command->setAliases($this->commands[$name]['aliases']);
}
return $command;
}
Expand All @@ -101,6 +109,9 @@ public function get($name): BaseCommand
*/
public function has($name): bool
{
if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
}
return isset($this->commands[$name]) && $this->isCommandAvailable($name);
}

Expand All @@ -109,7 +120,7 @@ public function has($name): bool
*/
public function getNames(): array
{
return array_keys($this->commands);
return array_merge(array_keys($this->commands), array_keys($this->aliases));
}

/**
Expand Down Expand Up @@ -184,7 +195,12 @@ private function populateCommandControllerCommands(CommandManager $commandManage
$closure = function () use ($commandName, $commandDefinition) {
return GeneralUtility::makeInstance(CommandControllerCommand::class, $commandName, $commandDefinition);
};
$this->add($closure, $commandName, $fullCommandIdentifier);
$aliases = [];
if ($commandName !== $fullCommandIdentifier) {
// @deprecated in 5.0 will be removed in 6.0
$aliases = [$fullCommandIdentifier];
}
$this->add($closure, $commandName, $aliases);
}
}
}
Expand All @@ -211,19 +227,18 @@ private function populateNativeCommands()
/** @var BaseCommand $command */
return GeneralUtility::makeInstance($commandConfig['class'], $commandName);
};
// No aliases for native commands (they can define their own aliases)
$this->add($closure, $commandName, $commandName);
$this->add($closure, $commandName, $commandConfig['aliases'] ?? []);
}
}
}
}

private function add(\Closure $closure, $commandName, $fullCommandIdentifier)
private function add(\Closure $closure, $commandName, array $aliases = [])
{
$this->commands[$commandName]['closure'] = $closure;
if ($commandName !== $fullCommandIdentifier) {
// @deprecated in 5.0 will be removed in 6.0
$this->commands[$commandName]['alias'] = $fullCommandIdentifier;
foreach ($aliases as $alias) {
$this->aliases[$alias] = $commandName;
$this->commands[$commandName]['aliases'][] = $alias;
}
}

Expand Down