PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/HelpFormatter.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 199 lines | 122 code | 10 blank | 67 comment | 8 complexity | 169015e3f6f6ff06cbfaacbf988a3e6a MD5 | raw file
  1. <?php
  2. /**
  3. * HelpFormatter
  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(tm) Project
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. App::uses('String', 'Utility');
  18. /**
  19. * HelpFormatter formats help for console shells. Can format to either
  20. * text or XML formats. Uses ConsoleOptionParser methods to generate help.
  21. *
  22. * Generally not directly used. Using $parser->help($command, 'xml'); is usually
  23. * how you would access help. Or via the `--help=xml` option on the command line.
  24. *
  25. * Xml output is useful for integration with other tools like IDE's or other build tools.
  26. *
  27. * @package Cake.Console
  28. * @since CakePHP(tm) v 2.0
  29. */
  30. class HelpFormatter {
  31. /**
  32. * The maximum number of arguments shown when generating usage.
  33. *
  34. * @var integer
  35. */
  36. protected $_maxArgs = 6;
  37. /**
  38. * The maximum number of options shown when generating usage.
  39. *
  40. * @var integer
  41. */
  42. protected $_maxOptions = 6;
  43. /**
  44. * Build the help formatter for a an OptionParser
  45. *
  46. * @param ConsoleOptionParser $parser The option parser help is being generated for.
  47. */
  48. public function __construct(ConsoleOptionParser $parser) {
  49. $this->_parser = $parser;
  50. }
  51. /**
  52. * Get the help as formatted text suitable for output on the command line.
  53. *
  54. * @param integer $width The width of the help output.
  55. * @return string
  56. */
  57. public function text($width = 72) {
  58. $parser = $this->_parser;
  59. $out = array();
  60. $description = $parser->description();
  61. if (!empty($description)) {
  62. $out[] = String::wrap($description, $width);
  63. $out[] = '';
  64. }
  65. $out[] = __d('cake_console', '<info>Usage:</info>');
  66. $out[] = $this->_generateUsage();
  67. $out[] = '';
  68. $subcommands = $parser->subcommands();
  69. if (!empty($subcommands)) {
  70. $out[] = __d('cake_console', '<info>Subcommands:</info>');
  71. $out[] = '';
  72. $max = $this->_getMaxLength($subcommands) + 2;
  73. foreach ($subcommands as $command) {
  74. $out[] = String::wrap($command->help($max), array(
  75. 'width' => $width,
  76. 'indent' => str_repeat(' ', $max),
  77. 'indentAt' => 1
  78. ));
  79. }
  80. $out[] = '';
  81. $out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command());
  82. $out[] = '';
  83. }
  84. $options = $parser->options();
  85. if (!empty($options)) {
  86. $max = $this->_getMaxLength($options) + 8;
  87. $out[] = __d('cake_console', '<info>Options:</info>');
  88. $out[] = '';
  89. foreach ($options as $option) {
  90. $out[] = String::wrap($option->help($max), array(
  91. 'width' => $width,
  92. 'indent' => str_repeat(' ', $max),
  93. 'indentAt' => 1
  94. ));
  95. }
  96. $out[] = '';
  97. }
  98. $arguments = $parser->arguments();
  99. if (!empty($arguments)) {
  100. $max = $this->_getMaxLength($arguments) + 2;
  101. $out[] = __d('cake_console', '<info>Arguments:</info>');
  102. $out[] = '';
  103. foreach ($arguments as $argument) {
  104. $out[] = String::wrap($argument->help($max), array(
  105. 'width' => $width,
  106. 'indent' => str_repeat(' ', $max),
  107. 'indentAt' => 1
  108. ));
  109. }
  110. $out[] = '';
  111. }
  112. $epilog = $parser->epilog();
  113. if (!empty($epilog)) {
  114. $out[] = String::wrap($epilog, $width);
  115. $out[] = '';
  116. }
  117. return implode("\n", $out);
  118. }
  119. /**
  120. * Generate the usage for a shell based on its arguments and options.
  121. * Usage strings favor short options over the long ones. and optional args will
  122. * be indicated with []
  123. *
  124. * @return string
  125. */
  126. protected function _generateUsage() {
  127. $usage = array('cake ' . $this->_parser->command());
  128. $subcommands = $this->_parser->subcommands();
  129. if (!empty($subcommands)) {
  130. $usage[] = '[subcommand]';
  131. }
  132. $options = array();
  133. foreach ($this->_parser->options() as $option) {
  134. $options[] = $option->usage();
  135. }
  136. if (count($options) > $this->_maxOptions) {
  137. $options = array('[options]');
  138. }
  139. $usage = array_merge($usage, $options);
  140. $args = array();
  141. foreach ($this->_parser->arguments() as $argument) {
  142. $args[] = $argument->usage();
  143. }
  144. if (count($args) > $this->_maxArgs) {
  145. $args = array('[arguments]');
  146. }
  147. $usage = array_merge($usage, $args);
  148. return implode(' ', $usage);
  149. }
  150. /**
  151. * Iterate over a collection and find the longest named thing.
  152. *
  153. * @param array $collection
  154. * @return integer
  155. */
  156. protected function _getMaxLength($collection) {
  157. $max = 0;
  158. foreach ($collection as $item) {
  159. $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
  160. }
  161. return $max;
  162. }
  163. /**
  164. * Get the help as an xml string.
  165. *
  166. * @param boolean $string Return the SimpleXml object or a string. Defaults to true.
  167. * @return mixed. See $string
  168. */
  169. public function xml($string = true) {
  170. $parser = $this->_parser;
  171. $xml = new SimpleXmlElement('<shell></shell>');
  172. $xml->addChild('command', $parser->command());
  173. $xml->addChild('description', $parser->description());
  174. $xml->addChild('epilog', $parser->epilog());
  175. $subcommands = $xml->addChild('subcommands');
  176. foreach ($parser->subcommands() as $command) {
  177. $command->xml($subcommands);
  178. }
  179. $options = $xml->addChild('options');
  180. foreach ($parser->options() as $option) {
  181. $option->xml($options);
  182. }
  183. $arguments = $xml->addChild('arguments');
  184. foreach ($parser->arguments() as $argument) {
  185. $argument->xml($arguments);
  186. }
  187. return $string ? $xml->asXml() : $xml;
  188. }
  189. }