Skip to content

Commit

Permalink
[Console] removed the special treatment of aliases in Application
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 7, 2011
1 parent facff73 commit 7d3e20d
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -47,7 +47,6 @@
class Application
{
private $commands;
private $aliases;
private $wantHelps = false;
private $runningCommand;
private $name;
Expand All @@ -72,7 +71,6 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
$this->catchExceptions = true;
$this->autoExit = true;
$this->commands = array();
$this->aliases = array();
$this->helperSet = new HelperSet(array(
new FormatterHelper(),
new DialogHelper(),
Expand Down Expand Up @@ -390,7 +388,7 @@ public function add(Command $command)
$this->commands[$command->getName()] = $command;

foreach ($command->getAliases() as $alias) {
$this->aliases[$alias] = $command;
$this->commands[$alias] = $command;
}

return $command;
Expand All @@ -409,11 +407,11 @@ public function add(Command $command)
*/
public function get($name)
{
if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
}

$command = isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
$command = $this->commands[$name];

if ($this->wantHelps) {
$this->wantHelps = false;
Expand All @@ -438,7 +436,7 @@ public function get($name)
*/
public function has($name)
{
return isset($this->commands[$name]) || isset($this->aliases[$name]);
return isset($this->commands[$name]);
}

/**
Expand All @@ -452,18 +450,14 @@ public function getNamespaces()
{
$namespaces = array();
foreach ($this->commands as $command) {
if ($namespace = $this->extractNamespace($command->getName())) {
$namespaces[] = $namespace;
}
$namespaces[] = $this->extractNamespace($command->getName());

foreach ($command->getAliases() as $alias) {
if ($namespace = $this->extractNamespace($alias)) {
$namespaces[] = $namespace;
}
$namespaces[] = $this->extractNamespace($alias);
}
}

return array_unique($namespaces);
return array_unique(array_filter($namespaces));
}

/**
Expand Down Expand Up @@ -532,7 +526,7 @@ public function find($name)
}
}

$abbrevs = static::getAbbreviations($commands);
$abbrevs = static::getAbbreviations(array_unique($commands));
if (isset($abbrevs[$searchName]) && 1 == count($abbrevs[$searchName])) {
return $this->get($abbrevs[$searchName][0]);
}
Expand All @@ -544,7 +538,16 @@ public function find($name)
}

// aliases
$abbrevs = static::getAbbreviations(array_keys($this->aliases));
$aliases = array();
foreach ($this->commands as $command) {
foreach ($command->getAliases() as $alias) {
if ($this->extractNamespace($alias) == $namespace) {
$aliases[] = $alias;
}
}
}

$abbrevs = static::getAbbreviations(array_unique($aliases));
if (!isset($abbrevs[$searchName])) {
throw new \InvalidArgumentException(sprintf('Command "%s" is not defined.', $name));
}
Expand Down Expand Up @@ -642,10 +645,8 @@ public function asText($namespace = null)
$messages[] = '<comment>'.$space.'</comment>';
}

foreach ($commands as $command) {
$aliases = $command->getAliases() ? '<comment> ('.implode(', ', $command->getAliases()).')</comment>' : '';

$messages[] = sprintf(" <info>%-${width}s</info> %s%s", $command->getName(), $command->getDescription(), $aliases);
foreach ($commands as $name => $command) {
$messages[] = sprintf(" <info>%-${width}s</info> %s", $name, $command->getDescription());
}
}

Expand Down Expand Up @@ -685,11 +686,11 @@ public function asXml($namespace = null, $asDom = false)
$namespaceArrayXML->setAttribute('id', $space);
}

foreach ($commands as $command) {
foreach ($commands as $name => $command) {
if (!$namespace) {
$commandXML = $dom->createElement('command');
$namespaceArrayXML->appendChild($commandXML);
$commandXML->appendChild($dom->createTextNode($command->getName()));
$commandXML->appendChild($dom->createTextNode($name));
}

$node = $command->asXml(true)->getElementsByTagName('command')->item(0);
Expand Down Expand Up @@ -793,15 +794,11 @@ private function sortCommands($commands)
{
$namespacedCommands = array();
foreach ($commands as $name => $command) {
$key = $this->extractNamespace($command->getName());
$key = $this->extractNamespace($name, 1);
if (!$key) {
$key = '_global';
}

if (!isset($namespacedCommands[$key])) {
$namespacedCommands[$key] = array();
}

$namespacedCommands[$key][$name] = $command;
}
ksort($namespacedCommands);
Expand All @@ -825,12 +822,11 @@ private function getAbbreviationSuggestions($abbrevs)
return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
}

private function extractNamespace($name)
private function extractNamespace($name, $limit = null)
{
if (false !== $pos = strrpos($name, ':')) {
return substr($name, 0, $pos);
}
$parts = explode(':', $name);
array_pop($parts);

return '';
return implode(':', null === $limit ? $parts : array_slice($parts, 0, $limit));
}
}

0 comments on commit 7d3e20d

Please sign in to comment.