ListCommand.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Command;
  11. use Symfony\Component\Console\Helper\DescriptorHelper;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputDefinition;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * ListCommand displays the list of all available commands for the application.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class ListCommand extends Command
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('list')
  31. ->setDefinition($this->createDefinition())
  32. ->setDescription('Lists commands')
  33. ->setHelp(<<<'EOF'
  34. The <info>%command.name%</info> command lists all commands:
  35. <info>php %command.full_name%</info>
  36. You can also display the commands for a specific namespace:
  37. <info>php %command.full_name% test</info>
  38. You can also output the information in other formats by using the <comment>--format</comment> option:
  39. <info>php %command.full_name% --format=xml</info>
  40. It's also possible to get raw list of commands (useful for embedding command runner):
  41. <info>php %command.full_name% --raw</info>
  42. EOF
  43. )
  44. ;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getNativeDefinition()
  50. {
  51. return $this->createDefinition();
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function execute(InputInterface $input, OutputInterface $output)
  57. {
  58. if ($input->getOption('xml')) {
  59. @trigger_error('The --xml option was deprecated in version 2.7 and will be removed in version 3.0. Use the --format option instead.', E_USER_DEPRECATED);
  60. $input->setOption('format', 'xml');
  61. }
  62. $helper = new DescriptorHelper();
  63. $helper->describe($output, $this->getApplication(), array(
  64. 'format' => $input->getOption('format'),
  65. 'raw_text' => $input->getOption('raw'),
  66. 'namespace' => $input->getArgument('namespace'),
  67. ));
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. private function createDefinition()
  73. {
  74. return new InputDefinition(array(
  75. new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
  76. new InputOption('xml', null, InputOption::VALUE_NONE, 'To output list as XML'),
  77. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
  78. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  79. ));
  80. }
  81. }