Application.php 24 KB

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