PageRenderTime 82ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Console/Command/CommandListShell.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 237 lines | 151 code | 18 blank | 68 comment | 18 complexity | 991afa881ed768338f5e443df3707871 MD5 | raw file
  1. <?php
  2. /**
  3. * Command list Shell
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP Project
  15. * @package Cake.Console.Command
  16. * @since CakePHP v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AppShell', 'Console/Command');
  20. App::uses('Inflector', 'Utility');
  21. /**
  22. * Shows a list of commands available from the console.
  23. *
  24. * @package Cake.Console.Command
  25. */
  26. class CommandListShell extends AppShell {
  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(__d('cake_console', "<info>Current Paths:</info>"), 2);
  45. $this->out(" -app: ". APP_DIR);
  46. $this->out(" -working: " . rtrim(APP, DS));
  47. $this->out(" -root: " . rtrim(ROOT, DS));
  48. $this->out(" -core: " . rtrim(CORE_PATH, DS));
  49. $this->out("");
  50. $this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
  51. $this->out(__d('cake_console', "Your working path should be the same as your application path to change your path use the '-app' param."));
  52. $this->out(__d('cake_console', "Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp"), 2);
  53. $this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
  54. }
  55. $shellList = $this->_getShellList();
  56. if ($shellList) {
  57. ksort($shellList);
  58. if (empty($this->params['xml'])) {
  59. if (!empty($this->params['sort'])) {
  60. $this->_asSorted($shellList);
  61. } else {
  62. $this->_asText($shellList);
  63. }
  64. } else {
  65. $this->_asXml($shellList);
  66. }
  67. }
  68. }
  69. /**
  70. * Gets the shell command listing.
  71. *
  72. * @return array
  73. */
  74. protected function _getShellList() {
  75. $shellList = array();
  76. $skipFiles = array('AppShell');
  77. $corePath = App::core('Console/Command');
  78. $shells = App::objects('file', $corePath[0]);
  79. $shells = array_diff($shells, $skipFiles);
  80. $shellList = $this->_appendShells('CORE', $shells, $shellList);
  81. $appShells = App::objects('Console/Command', null, false);
  82. $appShells = array_diff($appShells, $shells, $skipFiles);
  83. $shellList = $this->_appendShells('app', $appShells, $shellList);
  84. $plugins = CakePlugin::loaded();
  85. foreach ($plugins as $plugin) {
  86. $pluginShells = App::objects($plugin . '.Console/Command');
  87. $shellList = $this->_appendShells($plugin, $pluginShells, $shellList);
  88. }
  89. return $shellList;
  90. }
  91. /**
  92. * Scan the provided paths for shells, and append them into $shellList
  93. *
  94. * @param string $type
  95. * @param array $shells
  96. * @param array $shellList
  97. * @return array
  98. */
  99. protected function _appendShells($type, $shells, $shellList) {
  100. foreach ($shells as $shell) {
  101. $shell = Inflector::underscore(str_replace('Shell', '', $shell));
  102. $shellList[$shell][$type] = $type;
  103. }
  104. return $shellList;
  105. }
  106. /**
  107. * Output text.
  108. *
  109. * @param array $shellList
  110. * @return void
  111. */
  112. protected function _asText($shellList) {
  113. if (DS === '/') {
  114. $width = exec('tput cols') - 2;
  115. }
  116. if (empty($width)) {
  117. $width = 80;
  118. }
  119. $columns = max(1, floor($width / 30));
  120. $rows = ceil(count($shellList) / $columns);
  121. foreach ($shellList as $shell => $types) {
  122. sort($types);
  123. $shellList[$shell] = str_pad($shell . ' [' . implode ($types, ', ') . ']', $width / $columns);
  124. }
  125. $out = array_chunk($shellList, $rows);
  126. for ($i = 0; $i < $rows; $i++) {
  127. $row = '';
  128. for ($j = 0; $j < $columns; $j++) {
  129. if (!isset($out[$j][$i])) {
  130. continue;
  131. }
  132. $row .= $out[$j][$i];
  133. }
  134. $this->out(" " . $row);
  135. }
  136. $this->out();
  137. $this->out(__d('cake_console', "To run an app or core command, type <info>cake shell_name [args]</info>"));
  138. $this->out(__d('cake_console', "To run a plugin command, type <info>cake Plugin.shell_name [args]</info>"));
  139. $this->out(__d('cake_console', "To get help on a specific command, type <info>cake shell_name --help</info>"), 2);
  140. }
  141. /**
  142. * Generates the shell list sorted by where the shells are found.
  143. *
  144. * @param array $shellList
  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. * @param array $shellList
  183. * @return void
  184. */
  185. protected function _asXml($shellList) {
  186. $plugins = CakePlugin::loaded();
  187. $shells = new SimpleXmlElement('<shells></shells>');
  188. foreach ($shellList as $name => $location) {
  189. $source = current($location);
  190. $callable = $name;
  191. if (in_array($source, $plugins)) {
  192. $callable = Inflector::camelize($source) . '.' . $name;
  193. }
  194. $shell = $shells->addChild('shell');
  195. $shell->addAttribute('name', $name);
  196. $shell->addAttribute('call_as', $callable);
  197. $shell->addAttribute('provider', $source);
  198. $shell->addAttribute('help', $callable . ' -h');
  199. }
  200. $this->stdout->outputAs(ConsoleOutput::RAW);
  201. $this->out($shells->saveXml());
  202. }
  203. /**
  204. * get the option parser
  205. *
  206. * @return void
  207. */
  208. public function getOptionParser() {
  209. $parser = parent::getOptionParser();
  210. return $parser->description(__d('cake_console', 'Get the list of available shells for this CakePHP application.'))
  211. ->addOption('xml', array(
  212. 'help' => __d('cake_console', 'Get the listing as XML.'),
  213. 'boolean' => true
  214. ))->addOption('sort', array(
  215. 'help' => __d('cake_console', 'Sorts the commands by where they are located.'),
  216. 'boolean' => true
  217. ));
  218. }
  219. }