CommandListShell.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Shell;
  16. use Cake\Console\ConsoleOutput;
  17. use Cake\Console\Shell;
  18. use Cake\Core\Plugin;
  19. use Cake\Utility\Inflector;
  20. use SimpleXmlElement;
  21. /**
  22. * Shows a list of commands available from the console.
  23. *
  24. */
  25. class CommandListShell extends Shell
  26. {
  27. /**
  28. * Contains tasks to load and instantiate
  29. *
  30. * @var array
  31. */
  32. public $tasks = ['Command'];
  33. /**
  34. * startup
  35. *
  36. * @return void
  37. */
  38. public function startup()
  39. {
  40. if (empty($this->params['xml'])) {
  41. parent::startup();
  42. }
  43. }
  44. /**
  45. * Main function Prints out the list of shells.
  46. *
  47. * @return void
  48. */
  49. public function main()
  50. {
  51. if (empty($this->params['xml'])) {
  52. $this->out("<info>Current Paths:</info>", 2);
  53. $this->out("* app: " . APP_DIR);
  54. $this->out("* root: " . rtrim(ROOT, DIRECTORY_SEPARATOR));
  55. $this->out("* core: " . rtrim(CORE_PATH, DIRECTORY_SEPARATOR));
  56. $this->out("");
  57. $this->out("<info>Available Shells:</info>", 2);
  58. }
  59. $shellList = $this->Command->getShellList();
  60. if (empty($shellList)) {
  61. return;
  62. }
  63. if (empty($this->params['xml'])) {
  64. $this->_asText($shellList);
  65. } else {
  66. $this->_asXml($shellList);
  67. }
  68. }
  69. /**
  70. * Output text.
  71. *
  72. * @param array $shellList The shell list.
  73. * @return void
  74. */
  75. protected function _asText($shellList)
  76. {
  77. foreach ($shellList as $plugin => $commands) {
  78. sort($commands);
  79. $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
  80. $this->out();
  81. }
  82. $this->out("To run an app or core command, type <info>`cake shell_name [args]`</info>");
  83. $this->out("To run a plugin command, type <info>`cake Plugin.shell_name [args]`</info>");
  84. $this->out("To get help on a specific command, type <info>`cake shell_name --help`</info>", 2);
  85. }
  86. /**
  87. * Output as XML
  88. *
  89. * @param array $shellList The shell list.
  90. * @return void
  91. */
  92. protected function _asXml($shellList)
  93. {
  94. $plugins = Plugin::loaded();
  95. $shells = new SimpleXmlElement('<shells></shells>');
  96. foreach ($shellList as $plugin => $commands) {
  97. foreach ($commands as $command) {
  98. $callable = $command;
  99. if (in_array($plugin, $plugins)) {
  100. $callable = Inflector::camelize($plugin) . '.' . $command;
  101. }
  102. $shell = $shells->addChild('shell');
  103. $shell->addAttribute('name', $command);
  104. $shell->addAttribute('call_as', $callable);
  105. $shell->addAttribute('provider', $plugin);
  106. $shell->addAttribute('help', $callable . ' -h');
  107. }
  108. }
  109. $this->_io->outputAs(ConsoleOutput::RAW);
  110. $this->out($shells->saveXml());
  111. }
  112. /**
  113. * Gets the option parser instance and configures it.
  114. *
  115. * @return \Cake\Console\ConsoleOptionParser
  116. */
  117. public function getOptionParser()
  118. {
  119. $parser = parent::getOptionParser();
  120. $parser->description(
  121. 'Get the list of available shells for this CakePHP application.'
  122. )->addOption('xml', [
  123. 'help' => 'Get the listing as XML.',
  124. 'boolean' => true
  125. ]);
  126. return $parser;
  127. }
  128. }