Skip to content

Commit

Permalink
Refactor and improve documentation for CompletionHandler::getCommandN…
Browse files Browse the repository at this point in the history
…ames
  • Loading branch information
stecman committed Jun 1, 2017
1 parent 902f29d commit 8b1d65f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/CompletionCommand.php
Expand Up @@ -34,7 +34,8 @@ protected function configure()
END
);

// setHidden() method was not available before Symfony 3.2
// Hide this command from listing if supported
// Command::setHidden() was not available before Symfony 3.2.0
if (method_exists($this, 'setHidden')) {
$this->setHidden(true);
}
Expand Down
37 changes: 23 additions & 14 deletions src/CompletionHandler.php
Expand Up @@ -39,6 +39,7 @@ public function __construct(Application $application, CompletionContext $context
$this->application = $application;
$this->context = $context;

// Set up completions for commands that are built-into Application
$this->addHandler(
new Completion(
'help',
Expand Down Expand Up @@ -436,28 +437,36 @@ protected function getAllOptions()
);
}

/**
* Get command names available for completion
*
* Filters out hidden commands where supported.
*
* @return string[]
*/
protected function getCommandNames()
{
$commands = array();

// Command::Hidden isn't supported before Symfony Console 3.2.0
// We don't complete hidden command names as these are intended to be private
if (method_exists('\Symfony\Component\Console\Command\Command', 'isHidden')) {
$commands = array();

foreach ($this->application->all() as $name => $command) {
if ($command->isHidden()) {
continue;
if (!$command->isHidden()) {
$commands[] = $name;
}

$commands[] = $name;
}

return $commands;

} else {
foreach ($this->application->all() as $name => $command) {
if ($name === '_completion') {
continue;
}

$commands[] = $name;
}
}
// Fallback for compatibility with Symfony Console < 3.2.0
// This was the behaviour prior to pull #75
$commands = $this->application->all();
unset($commands['_completion']);

return $commands;
return array_keys($commands);
}
}
}

0 comments on commit 8b1d65f

Please sign in to comment.