68 $this->catchExceptions =
true;
69 $this->autoExit =
true;
70 $this->commands = array();
71 $this->aliases = array();
81 new InputArgument(
'command', InputArgument::REQUIRED,
'The command to execute'),
83 new InputOption(
'--help',
'-h', InputOption::VALUE_NONE,
'Display this help message.'),
84 new InputOption(
'--quiet',
'-q', InputOption::VALUE_NONE,
'Do not output any message.'),
85 new InputOption(
'--verbose',
'-v', InputOption::VALUE_NONE,
'Increase verbosity of messages.'),
86 new InputOption(
'--version',
'-V', InputOption::VALUE_NONE,
'Display this program version.'),
87 new InputOption(
'--ansi',
'-a', InputOption::VALUE_NONE,
'Force ANSI output.'),
88 new InputOption(
'--no-interaction',
'-n', InputOption::VALUE_NONE,
'Do not ask any interactive question.'),
440 if (!isset($abbrevs[$namespace])) {
441 throw new \InvalidArgumentException(sprintf(
'There are no commands defined in the "%s" namespace.', $namespace));
444 if (count($abbrevs[$namespace]) > 1) {
445 throw new \InvalidArgumentException(sprintf(
'The namespace "%s" is ambiguous (%s).', $namespace, $this->
getAbbreviationSuggestions($abbrevs[$namespace])));
448 return $abbrevs[$namespace][0];
467 if (
false !== $pos = strrpos(
$name,
':')) {
472 $fullName = $namespace ? $namespace.
':'.
$name :
$name;
476 foreach ($this->commands as $command) {
477 if ($command->getNamespace() == $namespace) {
482 $abbrevs = static::getAbbreviations(
$commands);
483 if (isset($abbrevs[
$name]) && 1 == count($abbrevs[
$name])) {
484 return $this->
get($namespace ? $namespace.
':'.$abbrevs[
$name][0] : $abbrevs[
$name][0]);
487 if (isset($abbrevs[
$name]) && count($abbrevs[
$name]) > 1) {
488 $suggestions = $this->
getAbbreviationSuggestions(array_map(
function ($command) use ($namespace) {
return $namespace.
':'.$command; }, $abbrevs[
$name]));
490 throw new \InvalidArgumentException(sprintf(
'Command "%s" is ambiguous (%s).', $fullName, $suggestions));
494 $abbrevs = static::getAbbreviations(array_keys($this->aliases));
495 if (!isset($abbrevs[$fullName])) {
496 throw new \InvalidArgumentException(sprintf(
'Command "%s" is not defined.', $fullName));
499 if (count($abbrevs[$fullName]) > 1) {
500 throw new \InvalidArgumentException(sprintf(
'Command "%s" is ambiguous (%s).', $fullName, $this->
getAbbreviationSuggestions($abbrevs[$fullName])));
503 return $this->
get($abbrevs[$fullName][0]);
567 public function asText($namespace =
null)
571 $messages = array($this->
getHelp(),
'');
573 $messages[] = sprintf(
"<comment>Available commands for the \"%s\" namespace:</comment>", $namespace);
575 $messages[] =
'<comment>Available commands:</comment>';
580 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
586 if (!$namespace &&
'_global' !== $space) {
587 $messages[] =
'<comment>'.$space.
'</comment>';
591 $aliases = $command->getAliases() ?
'<comment> ('.implode(
', ', $command->getAliases()).
')</comment>' :
'';
593 $messages[] = sprintf(
" <info>%-${width}s</info> %s%s", ($command->getNamespace() ?
':' :
'').$command->getName(), $command->getDescription(),
$aliases);
597 return implode(
"\n", $messages);
608 public function asXml($namespace =
null, $asDom =
false)
612 $dom = new \DOMDocument(
'1.0',
'UTF-8');
613 $dom->formatOutput =
true;
614 $dom->appendChild($xml = $dom->createElement(
'symfony'));
616 $xml->appendChild($commandsXML = $dom->createElement(
'commands'));
619 $commandsXML->setAttribute(
'namespace', $namespace);
621 $xml->appendChild($namespacesXML = $dom->createElement(
'namespaces'));
627 $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement(
'namespace'));
628 $namespaceArrayXML->setAttribute(
'id', $space);
633 $namespaceArrayXML->appendChild($commandXML = $dom->createElement(
'command'));
634 $commandXML->appendChild($dom->createTextNode($command->getName()));
637 $node = $command->asXml(
true)->getElementsByTagName(
'command')->item(0);
638 $node = $dom->importNode($node,
true);
640 $commandsXML->appendChild($node);
644 return $asDom ? $dom : $dom->saveXml();
655 $strlen =
function ($string)
657 return function_exists(
'mb_strlen') ? mb_strlen($string) : strlen($string);
660 $title = sprintf(
' [%s] ', get_class($e));
663 foreach (explode(
"\n", $e->getMessage()) as $line) {
664 $lines[] = sprintf(
' %s ', $line);
665 $len = max($strlen($line) + 4, $len);
668 $messages = array(str_repeat(
' ', $len),
$title.str_repeat(
' ', $len - $strlen(
$title)));
670 foreach ($lines as $line) {
671 $messages[] = $line.str_repeat(
' ', $len - $strlen($line));
674 $messages[] = str_repeat(
' ', $len);
676 $output->writeln(
"\n");
677 foreach ($messages as $message) {
678 $output->writeln(
'<error>'.$message.
'</error>');
680 $output->writeln(
"\n");
682 if (
null !== $this->runningCommand) {
683 $output->writeln(sprintf(
'<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
684 $output->writeln(
"\n");
687 if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) {
688 $output->writeln(
'</comment>Exception trace:</comment>');
691 $trace = $e->getTrace();
692 array_unshift($trace, array(
694 'file' => $e->getFile() !=
null ? $e->getFile() :
'n/a',
695 'line' => $e->getLine() !=
null ? $e->getLine() :
'n/a',
699 for ($i = 0, $count = count($trace); $i < $count; $i++) {
700 $class = isset($trace[$i][
'class']) ? $trace[$i][
'class'] :
'';
701 $type = isset($trace[$i][
'type']) ? $trace[$i][
'type'] :
'';
702 $function = $trace[$i][
'function'];
703 $file = isset($trace[$i][
'file']) ? $trace[$i][
'file'] :
'n/a';
704 $line = isset($trace[$i][
'line']) ? $trace[$i][
'line'] :
'n/a';
706 $output->writeln(sprintf(
' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
709 $output->writeln(
"\n");
720 $namespacedCommands = array();
722 $key = $command->getNamespace() ? $command->getNamespace() :
'_global';
724 if (!isset($namespacedCommands[$key])) {
725 $namespacedCommands[$key] = array();
728 $namespacedCommands[$key][
$name] = $command;
730 ksort($namespacedCommands);
736 return $namespacedCommands;