CommandListShell.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Utility\Inflector;
  21. use SimpleXmlElement;
  22. /**
  23. * Shows a list of commands available from the console.
  24. *
  25. * @property \Cake\Shell\Task\CommandTask $Command
  26. */
  27. class CommandListShell extends Shell
  28. {
  29. /**
  30. * Contains tasks to load and instantiate
  31. *
  32. * @var array
  33. */
  34. public $tasks = ['Command'];
  35. /**
  36. * startup
  37. *
  38. * @return void
  39. */
  40. public function startup()
  41. {
  42. if (!$this->param('xml') && !$this->param('version')) {
  43. parent::startup();
  44. }
  45. }
  46. /**
  47. * Main function Prints out the list of shells.
  48. *
  49. * @return void
  50. */
  51. public function main()
  52. {
  53. if (!$this->param('xml') && !$this->param('version')) {
  54. $this->out("<info>Current Paths:</info>", 2);
  55. $this->out("* app: " . APP_DIR);
  56. $this->out("* root: " . rtrim(ROOT, DIRECTORY_SEPARATOR));
  57. $this->out("* core: " . rtrim(CORE_PATH, DIRECTORY_SEPARATOR));
  58. $this->out("");
  59. $this->out("<info>Available Shells:</info>", 2);
  60. }
  61. if ($this->param('version')) {
  62. $this->out(Configure::version());
  63. return;
  64. }
  65. $shellList = $this->Command->getShellList();
  66. if (!$shellList) {
  67. return;
  68. }
  69. if (!$this->param('xml')) {
  70. $this->_asText($shellList);
  71. } else {
  72. $this->_asXml($shellList);
  73. }
  74. }
  75. /**
  76. * Output text.
  77. *
  78. * @param array $shellList The shell list.
  79. * @return void
  80. */
  81. protected function _asText($shellList)
  82. {
  83. foreach ($shellList as $plugin => $commands) {
  84. sort($commands);
  85. $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
  86. $this->out();
  87. }
  88. $this->out("To run an app or core command, type <info>`cake shell_name [args]`</info>");
  89. $this->out("To run a plugin command, type <info>`cake Plugin.shell_name [args]`</info>");
  90. $this->out("To get help on a specific command, type <info>`cake shell_name --help`</info>", 2);
  91. }
  92. /**
  93. * Output as XML
  94. *
  95. * @param array $shellList The shell list.
  96. * @return void
  97. */
  98. protected function _asXml($shellList)
  99. {
  100. $plugins = Plugin::loaded();
  101. $shells = new SimpleXmlElement('<shells></shells>');
  102. foreach ($shellList as $plugin => $commands) {
  103. foreach ($commands as $command) {
  104. $callable = $command;
  105. if (in_array($plugin, $plugins)) {
  106. $callable = Inflector::camelize($plugin) . '.' . $command;
  107. }
  108. $shell = $shells->addChild('shell');
  109. $shell->addAttribute('name', $command);
  110. $shell->addAttribute('call_as', $callable);
  111. $shell->addAttribute('provider', $plugin);
  112. $shell->addAttribute('help', $callable . ' -h');
  113. }
  114. }
  115. $this->_io->outputAs(ConsoleOutput::RAW);
  116. $this->out($shells->saveXML());
  117. }
  118. /**
  119. * Gets the option parser instance and configures it.
  120. *
  121. * @return \Cake\Console\ConsoleOptionParser
  122. */
  123. public function getOptionParser()
  124. {
  125. $parser = parent::getOptionParser();
  126. $parser->setDescription(
  127. 'Get the list of available shells for this CakePHP application.'
  128. )->addOption('xml', [
  129. 'help' => 'Get the listing as XML.',
  130. 'boolean' => true
  131. ])->addOption('version', [
  132. 'help' => 'Prints the currently installed version of CakePHP.',
  133. 'boolean' => true
  134. ]);
  135. return $parser;
  136. }
  137. }