CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs