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.0 API

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