1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: App::uses('CakeText', 'Utility');
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class HelpFormatter {
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: public function __construct(ConsoleOptionParser $parser) {
53: $this->_parser = $parser;
54: }
55:
56: 57: 58: 59: 60: 61:
62: public function text($width = 72) {
63: $parser = $this->_parser;
64: $out = array();
65: $description = $parser->description();
66: if (!empty($description)) {
67: $out[] = CakeText::wrap($description, $width);
68: $out[] = '';
69: }
70: $out[] = __d('cake_console', '<info>Usage:</info>');
71: $out[] = $this->_generateUsage();
72: $out[] = '';
73: $subcommands = $parser->subcommands();
74: if (!empty($subcommands)) {
75: $out[] = __d('cake_console', '<info>Subcommands:</info>');
76: $out[] = '';
77: $max = $this->_getMaxLength($subcommands) + 2;
78: foreach ($subcommands as $command) {
79: $out[] = CakeText::wrap($command->help($max), array(
80: 'width' => $width,
81: 'indent' => str_repeat(' ', $max),
82: 'indentAt' => 1
83: ));
84: }
85: $out[] = '';
86: $out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command());
87: $out[] = '';
88: }
89:
90: $options = $parser->options();
91: if (!empty($options)) {
92: $max = $this->_getMaxLength($options) + 8;
93: $out[] = __d('cake_console', '<info>Options:</info>');
94: $out[] = '';
95: foreach ($options as $option) {
96: $out[] = CakeText::wrap($option->help($max), array(
97: 'width' => $width,
98: 'indent' => str_repeat(' ', $max),
99: 'indentAt' => 1
100: ));
101: }
102: $out[] = '';
103: }
104:
105: $arguments = $parser->arguments();
106: if (!empty($arguments)) {
107: $max = $this->_getMaxLength($arguments) + 2;
108: $out[] = __d('cake_console', '<info>Arguments:</info>');
109: $out[] = '';
110: foreach ($arguments as $argument) {
111: $out[] = CakeText::wrap($argument->help($max), array(
112: 'width' => $width,
113: 'indent' => str_repeat(' ', $max),
114: 'indentAt' => 1
115: ));
116: }
117: $out[] = '';
118: }
119: $epilog = $parser->epilog();
120: if (!empty($epilog)) {
121: $out[] = CakeText::wrap($epilog, $width);
122: $out[] = '';
123: }
124: return implode("\n", $out);
125: }
126:
127: 128: 129: 130: 131: 132: 133:
134: protected function _generateUsage() {
135: $usage = array('cake ' . $this->_parser->command());
136: $subcommands = $this->_parser->subcommands();
137: if (!empty($subcommands)) {
138: $usage[] = '[subcommand]';
139: }
140: $options = array();
141: foreach ($this->_parser->options() as $option) {
142: $options[] = $option->usage();
143: }
144: if (count($options) > $this->_maxOptions) {
145: $options = array('[options]');
146: }
147: $usage = array_merge($usage, $options);
148: $args = array();
149: foreach ($this->_parser->arguments() as $argument) {
150: $args[] = $argument->usage();
151: }
152: if (count($args) > $this->_maxArgs) {
153: $args = array('[arguments]');
154: }
155: $usage = array_merge($usage, $args);
156: return implode(' ', $usage);
157: }
158:
159: 160: 161: 162: 163: 164:
165: protected function _getMaxLength($collection) {
166: $max = 0;
167: foreach ($collection as $item) {
168: $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
169: }
170: return $max;
171: }
172:
173: 174: 175: 176: 177: 178:
179: public function xml($string = true) {
180: $parser = $this->_parser;
181: $xml = new SimpleXmlElement('<shell></shell>');
182: $xml->addChild('command', $parser->command());
183: $xml->addChild('description', $parser->description());
184:
185: $xml->addChild('epilog', $parser->epilog());
186: $subcommands = $xml->addChild('subcommands');
187: foreach ($parser->subcommands() as $command) {
188: $command->xml($subcommands);
189: }
190: $options = $xml->addChild('options');
191: foreach ($parser->options() as $option) {
192: $option->xml($options);
193: }
194: $arguments = $xml->addChild('arguments');
195: foreach ($parser->arguments() as $argument) {
196: $argument->xml($arguments);
197: }
198: return $string ? $xml->asXml() : $xml;
199: }
200:
201: }
202: