1: <?php
2: /**
3: * ConsoleInputSubcommand file
4: *
5: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13: * @link https://cakephp.org CakePHP(tm) Project
14: * @since 2.0.0
15: * @license https://opensource.org/licenses/mit-license.php MIT License
16: */
17: namespace Cake\Console;
18:
19: use SimpleXMLElement;
20:
21: /**
22: * An object to represent a single subcommand used in the command line.
23: * Created when you call ConsoleOptionParser::addSubcommand()
24: *
25: * @see \Cake\Console\ConsoleOptionParser::addSubcommand()
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 \Cake\Console\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 \Cake\Console\ConsoleOptionParser|array|null $parser A parser for this subcommand. Either a ConsoleOptionParser, or an
56: * array that can be used with ConsoleOptionParser::buildFromArray().
57: */
58: public function __construct($name, $help = '', $parser = null)
59: {
60: if (is_array($name) && isset($name['name'])) {
61: foreach ($name as $key => $value) {
62: $this->{'_' . $key} = $value;
63: }
64: } else {
65: $this->_name = $name;
66: $this->_help = $help;
67: $this->_parser = $parser;
68: }
69: if (is_array($this->_parser)) {
70: $this->_parser['command'] = $this->_name;
71: $this->_parser = ConsoleOptionParser::buildFromArray($this->_parser);
72: }
73: }
74:
75: /**
76: * Get the value of the name attribute.
77: *
78: * @return string Value of this->_name.
79: */
80: public function name()
81: {
82: return $this->_name;
83: }
84:
85: /**
86: * Get the raw help string for this command
87: *
88: * @return string
89: */
90: public function getRawHelp()
91: {
92: return $this->_help;
93: }
94:
95: /**
96: * Generate the help for this this subcommand.
97: *
98: * @param int $width The width to make the name of the subcommand.
99: * @return string
100: */
101: public function help($width = 0)
102: {
103: $name = $this->_name;
104: if (strlen($name) < $width) {
105: $name = str_pad($name, $width, ' ');
106: }
107:
108: return $name . $this->_help;
109: }
110:
111: /**
112: * Get the usage value for this option
113: *
114: * @return \Cake\Console\ConsoleOptionParser|bool Either false or a ConsoleOptionParser
115: */
116: public function parser()
117: {
118: if ($this->_parser instanceof ConsoleOptionParser) {
119: return $this->_parser;
120: }
121:
122: return false;
123: }
124:
125: /**
126: * Append this subcommand to the Parent element
127: *
128: * @param \SimpleXMLElement $parent The parent element.
129: * @return \SimpleXMLElement The parent with this subcommand appended.
130: */
131: public function xml(SimpleXMLElement $parent)
132: {
133: $command = $parent->addChild('command');
134: $command->addAttribute('name', $this->_name);
135: $command->addAttribute('help', $this->_help);
136:
137: return $parent;
138: }
139: }
140: