CommandListShell.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * startup
  26. *
  27. * @return void
  28. */
  29. public function startup() {
  30. if (empty($this->params['xml'])) {
  31. parent::startup();
  32. }
  33. }
  34. /**
  35. * Main function Prints out the list of shells.
  36. *
  37. * @return void
  38. */
  39. public function main() {
  40. if (empty($this->params['xml'])) {
  41. $this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
  42. $this->out(" -app: " . APP_DIR);
  43. $this->out(" -working: " . rtrim(APP, DS));
  44. $this->out(" -root: " . rtrim(ROOT, DS));
  45. $this->out(" -core: " . rtrim(CORE_PATH, DS));
  46. $this->out("");
  47. $this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
  48. $this->out(__d('cake_console', "Your working path should be the same as your application path. To change your path use the '-app' param."));
  49. $this->out(__d('cake_console', "Example: %s or %s", '-app relative/path/to/myapp', '-app /absolute/path/to/myapp'), 2);
  50. $this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
  51. }
  52. $shellList = $this->_getShellList();
  53. if (empty($shellList)) {
  54. return;
  55. }
  56. if (empty($this->params['xml'])) {
  57. $this->_asText($shellList);
  58. } else {
  59. $this->_asXml($shellList);
  60. }
  61. }
  62. /**
  63. * Gets the shell command listing.
  64. *
  65. * @return array
  66. */
  67. protected function _getShellList() {
  68. $skipFiles = array('AppShell');
  69. $plugins = CakePlugin::loaded();
  70. $shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
  71. $corePath = App::core('Console/Command');
  72. $shells = App::objects('file', $corePath[0]);
  73. $shells = array_diff($shells, $skipFiles);
  74. $this->_appendShells('CORE', $shells, $shellList);
  75. $appShells = App::objects('Console/Command', null, false);
  76. $appShells = array_diff($appShells, $shells, $skipFiles);
  77. $this->_appendShells('app', $appShells, $shellList);
  78. foreach ($plugins as $plugin) {
  79. $pluginShells = App::objects($plugin . '.Console/Command');
  80. $this->_appendShells($plugin, $pluginShells, $shellList);
  81. }
  82. return array_filter($shellList);
  83. }
  84. /**
  85. * Scan the provided paths for shells, and append them into $shellList
  86. *
  87. * @param string $type
  88. * @param array $shells
  89. * @param array $shellList
  90. * @return void
  91. */
  92. protected function _appendShells($type, $shells, &$shellList) {
  93. foreach ($shells as $shell) {
  94. $shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
  95. }
  96. }
  97. /**
  98. * Output text.
  99. *
  100. * @param array $shellList
  101. * @return void
  102. */
  103. protected function _asText($shellList) {
  104. foreach ($shellList as $plugin => $commands) {
  105. sort($commands);
  106. $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
  107. $this->out();
  108. }
  109. $this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
  110. $this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
  111. $this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
  112. }
  113. /**
  114. * Output as XML
  115. *
  116. * @param array $shellList
  117. * @return void
  118. */
  119. protected function _asXml($shellList) {
  120. $plugins = CakePlugin::loaded();
  121. $shells = new SimpleXmlElement('<shells></shells>');
  122. foreach ($shellList as $plugin => $commands) {
  123. foreach ($commands as $command) {
  124. $callable = $command;
  125. if (in_array($plugin, $plugins)) {
  126. $callable = Inflector::camelize($plugin) . '.' . $command;
  127. }
  128. $shell = $shells->addChild('shell');
  129. $shell->addAttribute('name', $command);
  130. $shell->addAttribute('call_as', $callable);
  131. $shell->addAttribute('provider', $plugin);
  132. $shell->addAttribute('help', $callable . ' -h');
  133. }
  134. }
  135. $this->stdout->outputAs(ConsoleOutput::RAW);
  136. $this->out($shells->saveXml());
  137. }
  138. /**
  139. * get the option parser
  140. *
  141. * @return void
  142. */
  143. public function getOptionParser() {
  144. $parser = parent::getOptionParser();
  145. return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
  146. ->addOption('sort', array(
  147. 'help' => __d('cake_console', 'Does nothing (deprecated)'),
  148. 'boolean' => true
  149. ))
  150. ->addOption('xml', array(
  151. 'help' => __d('cake_console', 'Get the listing as XML.'),
  152. 'boolean' => true
  153. ));
  154. }
  155. }