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. * @package Cake.Console.Command
  13. * @since CakePHP v 2.0
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('AppShell', 'Console/Command');
  17. App::uses('Inflector', 'Utility');
  18. /**
  19. * Shows a list of commands available from the console.
  20. *
  21. * @package Cake.Console.Command
  22. */
  23. class CommandListShell extends AppShell {
  24. /**
  25. * Contains tasks to load and instantiate
  26. *
  27. * @var array
  28. */
  29. public $tasks = array('Command');
  30. /**
  31. * startup
  32. *
  33. * @return void
  34. */
  35. public function startup() {
  36. if (empty($this->params['xml'])) {
  37. parent::startup();
  38. }
  39. }
  40. /**
  41. * Main function Prints out the list of shells.
  42. *
  43. * @return void
  44. */
  45. public function main() {
  46. if (empty($this->params['xml'])) {
  47. $this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
  48. $this->out(" -app: " . APP_DIR);
  49. $this->out(" -working: " . rtrim(APP, DS));
  50. $this->out(" -root: " . rtrim(ROOT, DS));
  51. $this->out(" -core: " . rtrim(CORE_PATH, DS));
  52. $this->out("");
  53. $this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
  54. $this->out(__d('cake_console', "Your working path should be the same as your application path. To change your path use the '-app' param."));
  55. $this->out(__d('cake_console', "Example: %s or %s", '-app relative/path/to/myapp', '-app /absolute/path/to/myapp'), 2);
  56. $this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
  57. }
  58. $shellList = $this->Command->getShellList();
  59. if (empty($shellList)) {
  60. return;
  61. }
  62. if (empty($this->params['xml'])) {
  63. $this->_asText($shellList);
  64. } else {
  65. $this->_asXml($shellList);
  66. }
  67. }
  68. /**
  69. * Output text.
  70. *
  71. * @param array $shellList The shell list.
  72. * @return void
  73. */
  74. protected function _asText($shellList) {
  75. foreach ($shellList as $plugin => $commands) {
  76. sort($commands);
  77. $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
  78. $this->out();
  79. }
  80. $this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
  81. $this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
  82. $this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
  83. }
  84. /**
  85. * Output as XML
  86. *
  87. * @param array $shellList The shell list.
  88. * @return void
  89. */
  90. protected function _asXml($shellList) {
  91. $plugins = CakePlugin::loaded();
  92. $shells = new SimpleXmlElement('<shells></shells>');
  93. foreach ($shellList as $plugin => $commands) {
  94. foreach ($commands as $command) {
  95. $callable = $command;
  96. if (in_array($plugin, $plugins)) {
  97. $callable = Inflector::camelize($plugin) . '.' . $command;
  98. }
  99. $shell = $shells->addChild('shell');
  100. $shell->addAttribute('name', $command);
  101. $shell->addAttribute('call_as', $callable);
  102. $shell->addAttribute('provider', $plugin);
  103. $shell->addAttribute('help', $callable . ' -h');
  104. }
  105. }
  106. $this->stdout->outputAs(ConsoleOutput::RAW);
  107. $this->out($shells->saveXml());
  108. }
  109. /**
  110. * Gets the option parser instance and configures it.
  111. *
  112. * @return ConsoleOptionParser
  113. */
  114. public function getOptionParser() {
  115. $parser = parent::getOptionParser();
  116. $parser->description(
  117. __d('cake_console', 'Get the list of available shells for this CakePHP application.')
  118. )->addOption('sort', array(
  119. 'help' => __d('cake_console', 'Does nothing (deprecated)'),
  120. 'boolean' => true
  121. ))->addOption('xml', array(
  122. 'help' => __d('cake_console', 'Get the listing as XML.'),
  123. 'boolean' => true
  124. ));
  125. return $parser;
  126. }
  127. }