Base for a static organization website

HelpFormatter.php 5.6KB

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