1: <?php
2: /**
3: * ConsoleInputSubcommand file
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: * @since CakePHP(tm) v 2.0
17: * @license http://www.opensource.org/licenses/mit-license.php MIT License
18: */
19:
20: /**
21: * An object to represent a single subcommand used in the command line.
22: * Created when you call ConsoleOptionParser::addSubcommand()
23: *
24: * @see ConsoleOptionParser::addSubcommand()
25: * @package Cake.Console
26: */
27: class ConsoleInputSubcommand {
28:
29: /**
30: * Name of the subcommand
31: *
32: * @var string
33: */
34: protected $_name;
35:
36: /**
37: * Help string for the subcommand
38: *
39: * @var string
40: */
41: protected $_help;
42:
43: /**
44: * The ConsoleOptionParser for this subcommand.
45: *
46: * @var ConsoleOptionParser
47: */
48: protected $_parser;
49:
50: /**
51: * Make a new Subcommand
52: *
53: * @param string|array $name The long name of the subcommand, or an array with all the properties.
54: * @param string $help The help text for this option
55: * @param ConsoleOptionParser|array $parser A parser for this subcommand. Either a ConsoleOptionParser, or an array that can be
56: * used with ConsoleOptionParser::buildFromArray()
57: */
58: public function __construct($name, $help = '', $parser = null) {
59: if (is_array($name) && isset($name['name'])) {
60: foreach ($name as $key => $value) {
61: $this->{'_' . $key} = $value;
62: }
63: } else {
64: $this->_name = $name;
65: $this->_help = $help;
66: $this->_parser = $parser;
67: }
68: if (is_array($this->_parser)) {
69: $this->_parser['command'] = $this->_name;
70: $this->_parser = ConsoleOptionParser::buildFromArray($this->_parser);
71: }
72: }
73:
74: /**
75: * Get the value of the name attribute.
76: *
77: * @return string Value of this->_name.
78: */
79: public function name() {
80: return $this->_name;
81: }
82:
83: /**
84: * Generate the help for this this subcommand.
85: *
86: * @param integer $width The width to make the name of the subcommand.
87: * @return string
88: */
89: public function help($width = 0) {
90: $name = $this->_name;
91: if (strlen($name) < $width) {
92: $name = str_pad($name, $width, ' ');
93: }
94: return $name . $this->_help;
95: }
96:
97: /**
98: * Get the usage value for this option
99: *
100: * @return mixed Either false or a ConsoleOptionParser
101: */
102: public function parser() {
103: if ($this->_parser instanceof ConsoleOptionParser) {
104: return $this->_parser;
105: }
106: return false;
107: }
108:
109: /**
110: * Append this subcommand to the Parent element
111: *
112: * @param SimpleXmlElement $parent The parent element.
113: * @return SimpleXmlElement The parent with this subcommand appended.
114: */
115: public function xml(SimpleXmlElement $parent) {
116: $command = $parent->addChild('command');
117: $command->addAttribute('name', $this->_name);
118: $command->addAttribute('help', $this->_help);
119: return $parent;
120: }
121:
122: }
123: