1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Console;
16:
17: use Cake\Console\Exception\ConsoleException;
18: use Cake\Utility\Text;
19: use SimpleXMLElement;
20:
21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class HelpFormatter
31: {
32:
33: 34: 35: 36: 37:
38: protected $_maxArgs = 6;
39:
40: 41: 42: 43: 44:
45: protected $_maxOptions = 6;
46:
47: 48: 49: 50: 51:
52: protected $_parser;
53:
54: 55: 56: 57: 58:
59: protected $_alias = 'cake';
60:
61: 62: 63: 64: 65:
66: public function __construct(ConsoleOptionParser $parser)
67: {
68: $this->_parser = $parser;
69: }
70:
71: 72: 73: 74: 75: 76: 77:
78: public function setAlias($alias)
79: {
80: if (is_string($alias)) {
81: $this->_alias = $alias;
82: } else {
83: throw new ConsoleException('Alias must be of type string.');
84: }
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function text($width = 72)
94: {
95: $parser = $this->_parser;
96: $out = [];
97: $description = $parser->getDescription();
98: if (!empty($description)) {
99: $out[] = Text::wrap($description, $width);
100: $out[] = '';
101: }
102: $out[] = '<info>Usage:</info>';
103: $out[] = $this->_generateUsage();
104: $out[] = '';
105: $subcommands = $parser->subcommands();
106: if (!empty($subcommands)) {
107: $out[] = '<info>Subcommands:</info>';
108: $out[] = '';
109: $max = $this->_getMaxLength($subcommands) + 2;
110: foreach ($subcommands as $command) {
111: $out[] = Text::wrapBlock($command->help($max), [
112: 'width' => $width,
113: 'indent' => str_repeat(' ', $max),
114: 'indentAt' => 1
115: ]);
116: }
117: $out[] = '';
118: $out[] = sprintf('To see help on a subcommand use <info>`' . $this->_alias . ' %s [subcommand] --help`</info>', $parser->getCommand());
119: $out[] = '';
120: }
121:
122: $options = $parser->options();
123: if (!empty($options)) {
124: $max = $this->_getMaxLength($options) + 8;
125: $out[] = '<info>Options:</info>';
126: $out[] = '';
127: foreach ($options as $option) {
128: $out[] = Text::wrapBlock($option->help($max), [
129: 'width' => $width,
130: 'indent' => str_repeat(' ', $max),
131: 'indentAt' => 1
132: ]);
133: }
134: $out[] = '';
135: }
136:
137: $arguments = $parser->arguments();
138: if (!empty($arguments)) {
139: $max = $this->_getMaxLength($arguments) + 2;
140: $out[] = '<info>Arguments:</info>';
141: $out[] = '';
142: foreach ($arguments as $argument) {
143: $out[] = Text::wrapBlock($argument->help($max), [
144: 'width' => $width,
145: 'indent' => str_repeat(' ', $max),
146: 'indentAt' => 1
147: ]);
148: }
149: $out[] = '';
150: }
151: $epilog = $parser->getEpilog();
152: if (!empty($epilog)) {
153: $out[] = Text::wrap($epilog, $width);
154: $out[] = '';
155: }
156:
157: return implode("\n", $out);
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: protected function _generateUsage()
168: {
169: $usage = [$this->_alias . ' ' . $this->_parser->getCommand()];
170: $subcommands = $this->_parser->subcommands();
171: if (!empty($subcommands)) {
172: $usage[] = '[subcommand]';
173: }
174: $options = [];
175: foreach ($this->_parser->options() as $option) {
176: $options[] = $option->usage();
177: }
178: if (count($options) > $this->_maxOptions) {
179: $options = ['[options]'];
180: }
181: $usage = array_merge($usage, $options);
182: $args = [];
183: foreach ($this->_parser->arguments() as $argument) {
184: $args[] = $argument->usage();
185: }
186: if (count($args) > $this->_maxArgs) {
187: $args = ['[arguments]'];
188: }
189: $usage = array_merge($usage, $args);
190:
191: return implode(' ', $usage);
192: }
193:
194: 195: 196: 197: 198: 199:
200: protected function _getMaxLength($collection)
201: {
202: $max = 0;
203: foreach ($collection as $item) {
204: $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
205: }
206:
207: return $max;
208: }
209:
210: 211: 212: 213: 214: 215:
216: public function xml($string = true)
217: {
218: $parser = $this->_parser;
219: $xml = new SimpleXMLElement('<shell></shell>');
220: $xml->addChild('command', $parser->getCommand());
221: $xml->addChild('description', $parser->getDescription());
222:
223: $subcommands = $xml->addChild('subcommands');
224: foreach ($parser->subcommands() as $command) {
225: $command->xml($subcommands);
226: }
227: $options = $xml->addChild('options');
228: foreach ($parser->options() as $option) {
229: $option->xml($options);
230: }
231: $arguments = $xml->addChild('arguments');
232: foreach ($parser->arguments() as $argument) {
233: $argument->xml($arguments);
234: }
235: $xml->addChild('epilog', $parser->getEpilog());
236:
237: return $string ? $xml->asXML() : $xml;
238: }
239: }
240: