CommandListShell.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP Project
  11. * @package Cake.Console.Command
  12. * @since CakePHP v 2.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('AppShell', 'Console/Command');
  16. App::uses('Inflector', 'Utility');
  17. /**
  18. * Shows a list of commands available from the console.
  19. *
  20. * @package Cake.Console.Command
  21. */
  22. class CommandListShell extends AppShell {
  23. /**
  24. * startup
  25. *
  26. * @return void
  27. */
  28. public function startup() {
  29. if (empty($this->params['xml'])) {
  30. parent::startup();
  31. }
  32. }
  33. /**
  34. * Main function Prints out the list of shells.
  35. *
  36. * @return void
  37. */
  38. public function main() {
  39. if (empty($this->params['xml'])) {
  40. $this->out(__d('cake_console', "<info>Current Paths:</info>"), 2);
  41. $this->out(" -app: " . APP_DIR);
  42. $this->out(" -working: " . rtrim(APP, DS));
  43. $this->out(" -root: " . rtrim(ROOT, DS));
  44. $this->out(" -core: " . rtrim(CORE_PATH, DS));
  45. $this->out("");
  46. $this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
  47. $this->out(__d('cake_console', "Your working path should be the same as your application path to change your path use the '-app' param."));
  48. $this->out(__d('cake_console', "Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp"), 2);
  49. $this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
  50. }
  51. $shellList = $this->_getShellList();
  52. if (empty($shellList)) {
  53. return;
  54. }
  55. if (empty($this->params['xml'])) {
  56. $this->_asText($shellList);
  57. } else {
  58. $this->_asXml($shellList);
  59. }
  60. }
  61. /**
  62. * Gets the shell command listing.
  63. *
  64. * @return array
  65. */
  66. protected function _getShellList() {
  67. $skipFiles = array('AppShell');
  68. $plugins = CakePlugin::loaded();
  69. $shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);
  70. $corePath = App::core('Console/Command');
  71. $shells = App::objects('file', $corePath[0]);
  72. $shells = array_diff($shells, $skipFiles);
  73. $this->_appendShells('CORE', $shells, $shellList);
  74. $appShells = App::objects('Console/Command', null, false);
  75. $appShells = array_diff($appShells, $shells, $skipFiles);
  76. $this->_appendShells('app', $appShells, $shellList);
  77. foreach ($plugins as $plugin) {
  78. $pluginShells = App::objects($plugin . '.Console/Command');
  79. $this->_appendShells($plugin, $pluginShells, $shellList);
  80. }
  81. return array_filter($shellList);
  82. }
  83. /**
  84. * Scan the provided paths for shells, and append them into $shellList
  85. *
  86. * @param string $type
  87. * @param array $shells
  88. * @param array $shellList
  89. * @return void
  90. */
  91. protected function _appendShells($type, $shells, &$shellList) {
  92. foreach ($shells as $shell) {
  93. $shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
  94. }
  95. }
  96. /**
  97. * Output text.
  98. *
  99. * @param array $shellList
  100. * @return void
  101. */
  102. protected function _asText($shellList) {
  103. foreach ($shellList as $plugin => $commands) {
  104. sort($commands);
  105. $this->out(sprintf('[<info>%s</info>] %s', $plugin, implode(', ', $commands)));
  106. $this->out();
  107. }
  108. $this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
  109. $this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
  110. $this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
  111. }
  112. /**
  113. * Output as XML
  114. *
  115. * @param array $shellList
  116. * @return void
  117. */
  118. protected function _asXml($shellList) {
  119. $plugins = CakePlugin::loaded();
  120. $shells = new SimpleXmlElement('<shells></shells>');
  121. foreach ($shellList as $plugin => $commands) {
  122. foreach ($commands as $command) {
  123. $callable = $command;
  124. if (in_array($plugin, $plugins)) {
  125. $callable = Inflector::camelize($plugin) . '.' . $command;
  126. }
  127. $shell = $shells->addChild('shell');
  128. $shell->addAttribute('name', $command);
  129. $shell->addAttribute('call_as', $callable);
  130. $shell->addAttribute('provider', $plugin);
  131. $shell->addAttribute('help', $callable . ' -h');
  132. }
  133. }
  134. $this->stdout->outputAs(ConsoleOutput::RAW);
  135. $this->out($shells->saveXml());
  136. }
  137. /**
  138. * get the option parser
  139. *
  140. * @return void
  141. */
  142. public function getOptionParser() {
  143. $parser = parent::getOptionParser();
  144. return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
  145. ->addOption('sort', array(
  146. 'help' => __d('cake_console', 'Does nothing (deprecated)'),
  147. 'boolean' => true
  148. ))
  149. ->addOption('xml', array(
  150. 'help' => __d('cake_console', 'Get the listing as XML.'),
  151. 'boolean' => true
  152. ));
  153. }
  154. }