Application.php 21 KB

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