Application.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. namespace Symfony\Components\Console;
  3. use Symfony\Components\Console\Input\InputInterface;
  4. use Symfony\Components\Console\Input\ArgvInput;
  5. use Symfony\Components\Console\Input\ArrayInput;
  6. use Symfony\Components\Console\Input\InputDefinition;
  7. use Symfony\Components\Console\Input\InputOption;
  8. use Symfony\Components\Console\Input\InputArgument;
  9. use Symfony\Components\Console\Output\OutputInterface;
  10. use Symfony\Components\Console\Output\Output;
  11. use Symfony\Components\Console\Output\ConsoleOutput;
  12. use Symfony\Components\Console\Command\Command;
  13. use Symfony\Components\Console\Command\HelpCommand;
  14. use Symfony\Components\Console\Command\ListCommand;
  15. use Symfony\Components\Console\Helper\HelperSet;
  16. use Symfony\Components\Console\Helper\FormatterHelper;
  17. use Symfony\Components\Console\Helper\DialogHelper;
  18. /*
  19. * This file is part of the Symfony framework.
  20. *
  21. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22. *
  23. * This source file is subject to the MIT license that is bundled
  24. * with this source code in the file LICENSE.
  25. */
  26. /**
  27. * An Application is the container for a collection of commands.
  28. *
  29. * It is the main entry point of a Console application.
  30. *
  31. * This class is optimized for a standard CLI environment.
  32. *
  33. * Usage:
  34. *
  35. * $app = new Application('myapp', '1.0 (stable)');
  36. * $app->addCommand(new SimpleCommand());
  37. * $app->run();
  38. *
  39. * @package Symfony
  40. * @subpackage Components_Console
  41. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  42. */
  43. class Application
  44. {
  45. protected $commands;
  46. protected $aliases;
  47. protected $wantHelps = false;
  48. protected $runningCommand;
  49. protected $name;
  50. protected $version;
  51. protected $catchExceptions;
  52. protected $autoExit;
  53. protected $definition;
  54. protected $helperSet;
  55. /**
  56. * Constructor.
  57. *
  58. * @param string $name The name of the application
  59. * @param string $version The version of the application
  60. */
  61. public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
  62. {
  63. $this->name = $name;
  64. $this->version = $version;
  65. $this->catchExceptions = true;
  66. $this->autoExit = true;
  67. $this->commands = array();
  68. $this->aliases = array();
  69. $this->helperSet = new HelperSet(array(
  70. new FormatterHelper(),
  71. new DialogHelper(),
  72. ));
  73. $this->addCommand(new HelpCommand());
  74. $this->addCommand(new ListCommand());
  75. $this->definition = new InputDefinition(array(
  76. new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
  77. new InputOption('--help', '-h', InputOption::PARAMETER_NONE, 'Display this help message.'),
  78. new InputOption('--quiet', '-q', InputOption::PARAMETER_NONE, 'Do not output any message.'),
  79. new InputOption('--verbose', '-v', InputOption::PARAMETER_NONE, 'Increase verbosity of messages.'),
  80. new InputOption('--version', '-V', InputOption::PARAMETER_NONE, 'Display this program version.'),
  81. new InputOption('--color', '-c', InputOption::PARAMETER_NONE, 'Force ANSI color output.'),
  82. new InputOption('--no-interaction', '-n', InputOption::PARAMETER_NONE, 'Do not ask any interactive question.'),
  83. ));
  84. }
  85. /**
  86. * Runs the current application.
  87. *
  88. * @param InputInterface $input An Input instance
  89. * @param OutputInterface $output An Output instance
  90. *
  91. * @return integer 0 if everything went fine, or an error code
  92. *
  93. * @throws \Exception When doRun returns Exception
  94. */
  95. public function run(InputInterface $input = null, OutputInterface $output = null)
  96. {
  97. if (null === $input) {
  98. $input = new ArgvInput();
  99. }
  100. if (null === $output) {
  101. $output = new ConsoleOutput();
  102. }
  103. try {
  104. $statusCode = $this->doRun($input, $output);
  105. } catch (\Exception $e) {
  106. if (!$this->catchExceptions) {
  107. throw $e;
  108. }
  109. $this->renderException($e, $output);
  110. $statusCode = $e->getCode();
  111. $statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
  112. }
  113. if ($this->autoExit) {
  114. // @codeCoverageIgnoreStart
  115. exit($statusCode);
  116. // @codeCoverageIgnoreEnd
  117. } else {
  118. return $statusCode;
  119. }
  120. }
  121. /**
  122. * Runs the current application.
  123. *
  124. * @param InputInterface $input An Input instance
  125. * @param OutputInterface $output An Output instance
  126. *
  127. * @return integer 0 if everything went fine, or an error code
  128. */
  129. public function doRun(InputInterface $input, OutputInterface $output)
  130. {
  131. $name = $input->getFirstArgument('command');
  132. if (true === $input->hasParameterOption(array('--color', '-c'))) {
  133. $output->setDecorated(true);
  134. }
  135. if (true === $input->hasParameterOption(array('--help', '-H'))) {
  136. if (!$name) {
  137. $name = 'help';
  138. $input = new ArrayInput(array('command' => 'help'));
  139. } else {
  140. $this->wantHelps = true;
  141. }
  142. }
  143. if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) {
  144. $input->setInteractive(false);
  145. }
  146. if (true === $input->hasParameterOption(array('--quiet', '-q'))) {
  147. $output->setVerbosity(Output::VERBOSITY_QUIET);
  148. } elseif (true === $input->hasParameterOption(array('--verbose', '-v'))) {
  149. $output->setVerbosity(Output::VERBOSITY_VERBOSE);
  150. }
  151. if (true === $input->hasParameterOption(array('--version', '-V'))) {
  152. $output->writeln($this->getLongVersion());
  153. return 0;
  154. }
  155. if (!$name) {
  156. $name = 'list';
  157. $input = new ArrayInput(array('command' => 'list'));
  158. }
  159. // the command name MUST be the first element of the input
  160. $command = $this->findCommand($name);
  161. $this->runningCommand = $command;
  162. $statusCode = $command->run($input, $output);
  163. $this->runningCommand = null;
  164. return is_numeric($statusCode) ? $statusCode : 0;
  165. }
  166. /**
  167. * Set a helper set to be used with the command.
  168. *
  169. * @param HelperSet $helperSet The helper set
  170. */
  171. public function setHelperSet(HelperSet $helperSet)
  172. {
  173. $this->helperSet = $helperSet;
  174. }
  175. /**
  176. * Get the helper set associated with the command
  177. *
  178. * @return HelperSet The HelperSet instance associated with this command
  179. */
  180. public function getHelperSet()
  181. {
  182. return $this->helperSet;
  183. }
  184. /**
  185. * Gets the InputDefinition related to this Application.
  186. *
  187. * @return InputDefinition The InputDefinition instance
  188. */
  189. public function getDefinition()
  190. {
  191. return $this->definition;
  192. }
  193. /**
  194. * Gets the help message.
  195. *
  196. * @return string A help message.
  197. */
  198. public function getHelp()
  199. {
  200. $messages = array(
  201. $this->getLongVersion(),
  202. '',
  203. '<comment>Usage:</comment>',
  204. sprintf(" [options] command [arguments]\n"),
  205. '<comment>Options:</comment>',
  206. );
  207. foreach ($this->definition->getOptions() as $option) {
  208. $messages[] = sprintf(' %-29s %s %s',
  209. '<info>--'.$option->getName().'</info>',
  210. $option->getShortcut() ? '<info>-'.$option->getShortcut().'</info>' : ' ',
  211. $option->getDescription()
  212. );
  213. }
  214. return implode("\n", $messages);
  215. }
  216. /**
  217. * Sets whether to catch exceptions or not during commands execution.
  218. *
  219. * @param Boolean $boolean Whether to catch exceptions or not during commands execution
  220. */
  221. public function setCatchExceptions($boolean)
  222. {
  223. $this->catchExceptions = (Boolean) $boolean;
  224. }
  225. /**
  226. * Sets whether to automatically exit after a command execution or not.
  227. *
  228. * @param Boolean $boolean Whether to automatically exit after a command execution or not
  229. */
  230. public function setAutoExit($boolean)
  231. {
  232. $this->autoExit = (Boolean) $boolean;
  233. }
  234. /**
  235. * Gets the name of the application.
  236. *
  237. * @return string The application name
  238. */
  239. public function getName()
  240. {
  241. return $this->name;
  242. }
  243. /**
  244. * Sets the application name.
  245. *
  246. * @param string $name The application name
  247. */
  248. public function setName($name)
  249. {
  250. $this->name = $name;
  251. }
  252. /**
  253. * Gets the application version.
  254. *
  255. * @return string The application version
  256. */
  257. public function getVersion()
  258. {
  259. return $this->version;
  260. }
  261. /**
  262. * Sets the application version.
  263. *
  264. * @param string $version The application version
  265. */
  266. public function setVersion($version)
  267. {
  268. $this->version = $version;
  269. }
  270. /**
  271. * Returns the long version of the application.
  272. *
  273. * @return string The long application version
  274. */
  275. public function getLongVersion()
  276. {
  277. if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
  278. return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
  279. } else {
  280. return '<info>Console Tool</info>';
  281. }
  282. }
  283. /**
  284. * Registers a new command.
  285. *
  286. * @param string $name The command name
  287. *
  288. * @return Command The newly created command
  289. */
  290. public function register($name)
  291. {
  292. return $this->addCommand(new Command($name));
  293. }
  294. /**
  295. * Adds an array of command objects.
  296. *
  297. * @param Command[] $commands An array of commands
  298. */
  299. public function addCommands(array $commands)
  300. {
  301. foreach ($commands as $command) {
  302. $this->addCommand($command);
  303. }
  304. }
  305. /**
  306. * Adds a command object.
  307. *
  308. * If a command with the same name already exists, it will be overridden.
  309. *
  310. * @param Command $command A Command object
  311. *
  312. * @return Command The registered command
  313. */
  314. public function addCommand(Command $command)
  315. {
  316. $command->setApplication($this);
  317. $this->commands[$command->getFullName()] = $command;
  318. foreach ($command->getAliases() as $alias) {
  319. $this->aliases[$alias] = $command;
  320. }
  321. return $command;
  322. }
  323. /**
  324. * Returns a registered command by name or alias.
  325. *
  326. * @param string $name The command name or alias
  327. *
  328. * @return Command A Command object
  329. *
  330. * @throws \InvalidArgumentException When command name given does not exist
  331. */
  332. public function getCommand($name)
  333. {
  334. if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
  335. throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
  336. }
  337. $command = isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
  338. if ($this->wantHelps) {
  339. $this->wantHelps = false;
  340. $helpCommand = $this->getCommand('help');
  341. $helpCommand->setCommand($command);
  342. return $helpCommand;
  343. }
  344. return $command;
  345. }
  346. /**
  347. * Returns true if the command exists, false otherwise
  348. *
  349. * @param string $name The command name or alias
  350. *
  351. * @return Boolean true if the command exists, false otherwise
  352. */
  353. public function hasCommand($name)
  354. {
  355. return isset($this->commands[$name]) || isset($this->aliases[$name]);
  356. }
  357. /**
  358. * Returns an array of all unique namespaces used by currently registered commands.
  359. *
  360. * It does not returns the global namespace which always exists.
  361. *
  362. * @return array An array of namespaces
  363. */
  364. public function getNamespaces()
  365. {
  366. $namespaces = array();
  367. foreach ($this->commands as $command) {
  368. if ($command->getNamespace()) {
  369. $namespaces[$command->getNamespace()] = true;
  370. }
  371. }
  372. return array_keys($namespaces);
  373. }
  374. /**
  375. * Finds a registered namespace by a name or an abbreviation.
  376. *
  377. * @return string A registered namespace
  378. *
  379. * @throws \InvalidArgumentException When namespace is incorrect or ambiguous
  380. */
  381. public function findNamespace($namespace)
  382. {
  383. $abbrevs = static::getAbbreviations($this->getNamespaces());
  384. if (!isset($abbrevs[$namespace])) {
  385. throw new \InvalidArgumentException(sprintf('There are no commands defined in the "%s" namespace.', $namespace));
  386. }
  387. if (count($abbrevs[$namespace]) > 1) {
  388. throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions($abbrevs[$namespace])));
  389. }
  390. return $abbrevs[$namespace][0];
  391. }
  392. /**
  393. * Finds a command by name or alias.
  394. *
  395. * Contrary to getCommand, this command tries to find the best
  396. * match if you give it an abbreviation of a name or alias.
  397. *
  398. * @param string $name A command name or a command alias
  399. *
  400. * @return Command A Command instance
  401. *
  402. * @throws \InvalidArgumentException When command name is incorrect or ambiguous
  403. */
  404. public function findCommand($name)
  405. {
  406. // namespace
  407. $namespace = '';
  408. if (false !== $pos = strrpos($name, ':')) {
  409. $namespace = $this->findNamespace(substr($name, 0, $pos));
  410. $name = substr($name, $pos + 1);
  411. }
  412. $fullName = $namespace ? $namespace.':'.$name : $name;
  413. // name
  414. $commands = array();
  415. foreach ($this->commands as $command) {
  416. if ($command->getNamespace() == $namespace) {
  417. $commands[] = $command->getName();
  418. }
  419. }
  420. $abbrevs = static::getAbbreviations($commands);
  421. if (isset($abbrevs[$name]) && 1 == count($abbrevs[$name])) {
  422. return $this->getCommand($namespace ? $namespace.':'.$abbrevs[$name][0] : $abbrevs[$name][0]);
  423. }
  424. if (isset($abbrevs[$name]) && count($abbrevs[$name]) > 1) {
  425. $suggestions = $this->getAbbreviationSuggestions(array_map(function ($command) use ($namespace) { return $namespace.':'.$command; }, $abbrevs[$name]));
  426. throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $suggestions));
  427. }
  428. // aliases
  429. $abbrevs = static::getAbbreviations(array_keys($this->aliases));
  430. if (!isset($abbrevs[$fullName])) {
  431. throw new \InvalidArgumentException(sprintf('Command "%s" is not defined.', $fullName));
  432. }
  433. if (count($abbrevs[$fullName]) > 1) {
  434. throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $this->getAbbreviationSuggestions($abbrevs[$fullName])));
  435. }
  436. return $this->getCommand($abbrevs[$fullName][0]);
  437. }
  438. /**
  439. * Gets the commands (registered in the given namespace if provided).
  440. *
  441. * The array keys are the full names and the values the command instances.
  442. *
  443. * @param string $namespace A namespace name
  444. *
  445. * @return array An array of Command instances
  446. */
  447. public function getCommands($namespace = null)
  448. {
  449. if (null === $namespace) {
  450. return $this->commands;
  451. }
  452. $commands = array();
  453. foreach ($this->commands as $name => $command) {
  454. if ($namespace === $command->getNamespace()) {
  455. $commands[$name] = $command;
  456. }
  457. }
  458. return $commands;
  459. }
  460. /**
  461. * Returns an array of possible abbreviations given a set of names.
  462. *
  463. * @param array An array of names
  464. *
  465. * @return array An array of abbreviations
  466. */
  467. static public function getAbbreviations($names)
  468. {
  469. $abbrevs = array();
  470. foreach ($names as $name) {
  471. for ($len = strlen($name) - 1; $len > 0; --$len) {
  472. $abbrev = substr($name, 0, $len);
  473. if (!isset($abbrevs[$abbrev])) {
  474. $abbrevs[$abbrev] = array($name);
  475. } else {
  476. $abbrevs[$abbrev][] = $name;
  477. }
  478. }
  479. }
  480. // Non-abbreviations always get entered, even if they aren't unique
  481. foreach ($names as $name) {
  482. $abbrevs[$name] = array($name);
  483. }
  484. return $abbrevs;
  485. }
  486. /**
  487. * Returns a text representation of the Application.
  488. *
  489. * @param string $namespace An optional namespace name
  490. *
  491. * @return string A string representing the Application
  492. */
  493. public function asText($namespace = null)
  494. {
  495. $commands = $namespace ? $this->getCommands($this->findNamespace($namespace)) : $this->commands;
  496. $messages = array($this->getHelp(), '');
  497. if ($namespace) {
  498. $messages[] = sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $namespace);
  499. } else {
  500. $messages[] = '<comment>Available commands:</comment>';
  501. }
  502. $width = 0;
  503. foreach ($commands as $command) {
  504. $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
  505. }
  506. $width += 2;
  507. // add commands by namespace
  508. foreach ($this->sortCommands($commands) as $space => $commands) {
  509. if (!$namespace && '_global' !== $space) {
  510. $messages[] = '<comment>'.$space.'</comment>';
  511. }
  512. foreach ($commands as $command) {
  513. $aliases = $command->getAliases() ? '<comment> ('.implode(', ', $command->getAliases()).')</comment>' : '';
  514. $messages[] = sprintf(" <info>%-${width}s</info> %s%s", ($command->getNamespace() ? ':' : '').$command->getName(), $command->getDescription(), $aliases);
  515. }
  516. }
  517. return implode("\n", $messages);
  518. }
  519. /**
  520. * Returns an XML representation of the Application.
  521. *
  522. * @param string $namespace An optional namespace name
  523. * @param Boolean $asDom Whether to return a DOM or an XML string
  524. *
  525. * @return string|DOMDocument An XML string representing the Application
  526. */
  527. public function asXml($namespace = null, $asDom = false)
  528. {
  529. $commands = $namespace ? $this->getCommands($this->findNamespace($namespace)) : $this->commands;
  530. $dom = new \DOMDocument('1.0', 'UTF-8');
  531. $dom->formatOutput = true;
  532. $dom->appendChild($xml = $dom->createElement('symfony'));
  533. $xml->appendChild($commandsXML = $dom->createElement('commands'));
  534. if ($namespace) {
  535. $commandsXML->setAttribute('namespace', $namespace);
  536. } else {
  537. $xml->appendChild($namespacesXML = $dom->createElement('namespaces'));
  538. }
  539. // add commands by namespace
  540. foreach ($this->sortCommands($commands) as $space => $commands) {
  541. if (!$namespace) {
  542. $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
  543. $namespaceArrayXML->setAttribute('id', $space);
  544. }
  545. foreach ($commands as $command) {
  546. if (!$namespace) {
  547. $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
  548. $commandXML->appendChild($dom->createTextNode($command->getName()));
  549. }
  550. $commandXML = new \DOMDocument('1.0', 'UTF-8');
  551. $commandXML->formatOutput = true;
  552. $commandXML->loadXML($command->asXml());
  553. $node = $commandXML->getElementsByTagName('command')->item(0);
  554. $node = $dom->importNode($node, true);
  555. $commandsXML->appendChild($node);
  556. }
  557. }
  558. return $asDom ? $dom : $dom->saveXml();
  559. }
  560. /**
  561. * Renders a catched exception.
  562. *
  563. * @param Exception $e An exception instance
  564. * @param OutputInterface $output An OutputInterface instance
  565. */
  566. public function renderException($e, $output)
  567. {
  568. $strlen = function ($string)
  569. {
  570. return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
  571. };
  572. $title = sprintf(' [%s] ', get_class($e));
  573. $len = $strlen($title);
  574. $lines = array();
  575. foreach (explode("\n", $e->getMessage()) as $line) {
  576. $lines[] = sprintf(' %s ', $line);
  577. $len = max($strlen($line) + 4, $len);
  578. }
  579. $messages = array(str_repeat(' ', $len), $title.str_repeat(' ', $len - $strlen($title)));
  580. foreach ($lines as $line) {
  581. $messages[] = $line.str_repeat(' ', $len - $strlen($line));
  582. }
  583. $messages[] = str_repeat(' ', $len);
  584. $output->writeln("\n");
  585. foreach ($messages as $message) {
  586. $output->writeln('<error>'.$message.'</error>');
  587. }
  588. $output->writeln("\n");
  589. if (null !== $this->runningCommand) {
  590. $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
  591. $output->writeln("\n");
  592. }
  593. if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) {
  594. $output->writeln('</comment>Exception trace:</comment>');
  595. // exception related properties
  596. $trace = $e->getTrace();
  597. array_unshift($trace, array(
  598. 'function' => '',
  599. 'file' => $e->getFile() != null ? $e->getFile() : 'n/a',
  600. 'line' => $e->getLine() != null ? $e->getLine() : 'n/a',
  601. 'args' => array(),
  602. ));
  603. for ($i = 0, $count = count($trace); $i < $count; $i++) {
  604. $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
  605. $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
  606. $function = $trace[$i]['function'];
  607. $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
  608. $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
  609. $output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
  610. }
  611. $output->writeln("\n");
  612. }
  613. }
  614. private function sortCommands($commands)
  615. {
  616. $namespacedCommands = array();
  617. foreach ($commands as $name => $command) {
  618. $key = $command->getNamespace() ? $command->getNamespace() : '_global';
  619. if (!isset($namespacedCommands[$key])) {
  620. $namespacedCommands[$key] = array();
  621. }
  622. $namespacedCommands[$key][$name] = $command;
  623. }
  624. ksort($namespacedCommands);
  625. foreach ($namespacedCommands as $name => &$commands) {
  626. ksort($commands);
  627. }
  628. return $namespacedCommands;
  629. }
  630. private function getAbbreviationSuggestions($abbrevs)
  631. {
  632. return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
  633. }
  634. }