CommandListShell.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * CommandListTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2006-2010, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
  14. * @link http://cakephp.org CakePHP Project
  15. * @package cake
  16. * @subpackage cake.tests.cases.console.libs
  17. * @since CakePHP v 2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Shell', 'Console');
  21. /**
  22. * Shows a list of commands available from the console.
  23. *
  24. * @package cake.console.libs
  25. */
  26. class CommandListShell extends Shell {
  27. /**
  28. * startup
  29. *
  30. * @return void
  31. */
  32. public function startup() {
  33. if (empty($this->params['xml'])) {
  34. parent::startup();
  35. }
  36. }
  37. /**
  38. * Main function Prints out the list of shells.
  39. *
  40. * @return void
  41. */
  42. public function main() {
  43. if (empty($this->params['xml'])) {
  44. $this->out("<info>Current Paths:</info>", 2);
  45. $this->out(" -app: ". APP_DIR);
  46. $this->out(" -working: " . rtrim(APP_PATH, DS));
  47. $this->out(" -root: " . rtrim(ROOT, DS));
  48. $this->out(" -core: " . rtrim(CORE_PATH, DS));
  49. $this->out("");
  50. $this->out("<info>Changing Paths:</info>", 2);
  51. $this->out("Your working path should be the same as your application path");
  52. $this->out("to change your path use the '-app' param.");
  53. $this->out("Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp", 2);
  54. $this->out("<info>Available Shells:</info>", 2);
  55. }
  56. $shellList = $this->_getShellList();
  57. if ($shellList) {
  58. ksort($shellList);
  59. if (empty($this->params['xml'])) {
  60. if (!empty($this->params['sort'])) {
  61. $this->_asSorted($shellList);
  62. } else {
  63. $this->_asText($shellList);
  64. }
  65. } else {
  66. $this->_asXml($shellList);
  67. }
  68. }
  69. }
  70. /**
  71. * Gets the shell command listing.
  72. *
  73. * @return array
  74. */
  75. protected function _getShellList() {
  76. $shellList = array();
  77. $corePaths = App::core('shells');
  78. $shellList = $this->_appendShells('CORE', $corePaths, $shellList);
  79. $appPaths = array_diff(App::path('shells'), $corePaths);
  80. $shellList = $this->_appendShells('app', $appPaths, $shellList);
  81. $plugins = App::objects('plugin');
  82. foreach ($plugins as $plugin) {
  83. $pluginPath = App::pluginPath($plugin) . 'console' . DS . 'shells' . DS;
  84. $shellList = $this->_appendShells($plugin, array($pluginPath), $shellList);
  85. }
  86. return $shellList;
  87. }
  88. /**
  89. * Scan the provided paths for shells, and append them into $shellList
  90. *
  91. * @return array
  92. */
  93. protected function _appendShells($type, $paths, $shellList) {
  94. foreach ($paths as $path) {
  95. if (!is_dir($path)) {
  96. continue;
  97. }
  98. $shells = App::objects('file', $path);
  99. if (empty($shells)) {
  100. continue;
  101. }
  102. foreach ($shells as $shell) {
  103. $shell = str_replace('Shell.php', '', $shell);
  104. $shellList[$shell][$type] = $type;
  105. }
  106. }
  107. return $shellList;
  108. }
  109. /**
  110. * Output text.
  111. *
  112. * @return void
  113. */
  114. protected function _asText($shellList) {
  115. if (DS === '/') {
  116. $width = exec('tput cols') - 2;
  117. }
  118. if (empty($width)) {
  119. $width = 80;
  120. }
  121. $columns = max(1, floor($width / 30));
  122. $rows = ceil(count($shellList) / $columns);
  123. foreach ($shellList as $shell => $types) {
  124. sort($types);
  125. $shellList[$shell] = str_pad($shell . ' [' . implode ($types, ', ') . ']', $width / $columns);
  126. }
  127. $out = array_chunk($shellList, $rows);
  128. for ($i = 0; $i < $rows; $i++) {
  129. $row = '';
  130. for ($j = 0; $j < $columns; $j++) {
  131. if (!isset($out[$j][$i])) {
  132. continue;
  133. }
  134. $row .= $out[$j][$i];
  135. }
  136. $this->out(" " . $row);
  137. }
  138. $this->out();
  139. $this->out("To run a command, type <info>cake shell_name [args]</info>");
  140. $this->out("To get help on a specific command, type <info>cake shell_name --help</info>", 2);
  141. }
  142. /**
  143. * Generates the shell list sorted by where the shells are found.
  144. *
  145. * @return void
  146. */
  147. protected function _asSorted($shellList) {
  148. $grouped = array();
  149. foreach ($shellList as $shell => $types) {
  150. foreach ($types as $type) {
  151. $type = Inflector::camelize($type);
  152. if (empty($grouped[$type])) {
  153. $grouped[$type] = array();
  154. }
  155. $grouped[$type][] = $shell;
  156. }
  157. }
  158. if (!empty($grouped['App'])) {
  159. sort($grouped['App'], SORT_STRING);
  160. $this->out('[ App ]');
  161. $this->out(' ' . implode(', ', $grouped['App']), 2);
  162. unset($grouped['App']);
  163. }
  164. foreach ($grouped as $section => $shells) {
  165. if ($section == 'CORE') {
  166. continue;
  167. }
  168. sort($shells, SORT_STRING);
  169. $this->out('[ ' . $section . ' ]');
  170. $this->out(' ' . implode(', ', $shells), 2);
  171. }
  172. if (!empty($grouped['CORE'])) {
  173. sort($grouped['CORE'], SORT_STRING);
  174. $this->out('[ Core ]');
  175. $this->out(' ' . implode(', ', $grouped['CORE']), 2);
  176. }
  177. $this->out();
  178. }
  179. /**
  180. * Output as XML
  181. *
  182. * @return void
  183. */
  184. protected function _asXml($shellList) {
  185. $plugins = App::objects('plugin');
  186. $shells = new SimpleXmlElement('<shells></shells>');
  187. foreach ($shellList as $name => $location) {
  188. $source = current($location);
  189. $callable = $name;
  190. if (in_array($source, $plugins)) {
  191. $callable = Inflector::underscore($source) . '.' . $name;
  192. }
  193. $shell = $shells->addChild('shell');
  194. $shell->addAttribute('name', $name);
  195. $shell->addAttribute('call_as', $callable);
  196. $shell->addAttribute('provider', $source);
  197. $shell->addAttribute('help', $callable . ' -h');
  198. }
  199. $this->out($shells->saveXml());
  200. }
  201. /**
  202. * get the option parser
  203. *
  204. * @return void
  205. */
  206. public function getOptionParser() {
  207. $parser = parent::getOptionParser();
  208. return $parser->description('Get the list of available shells for this CakePHP application.')
  209. ->addOption('xml', array(
  210. 'help' => __('Get the listing as XML.'),
  211. 'boolean' => true
  212. ))->addOption('sort', array(
  213. 'help' => __('Sorts the commands by where they are located.'),
  214. 'boolean' => true
  215. ));
  216. }
  217. }