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: /**
31: * Name of the subcommand
32: *
33: * @var string
34: */
35: protected $_name = '';
36:
37: /**
38: * Help string for the subcommand
39: *
40: * @var string
41: */
42: protected $_help = '';
43:
44: /**
45: * The ConsoleOptionParser for this subcommand.
46: *
47: * @var \Cake\Console\ConsoleOptionParser
48: */
49: protected $_parser;
50:
51: /**
52: * Make a new Subcommand
53: *
54: * @param string|array $name The long name of the subcommand, or an array with all the properties.
55: * @param string $help The help text for this option.
56: * @param \Cake\Console\ConsoleOptionParser|array|null $parser A parser for this subcommand. Either a ConsoleOptionParser, or an
57: * array that can be used with ConsoleOptionParser::buildFromArray().
58: */
59: public function __construct($name, $help = '', $parser = null)
60: {
61: if (is_array($name) && isset($name['name'])) {
62: foreach ($name as $key => $value) {
63: $this->{'_' . $key} = $value;
64: }
65: } else {
66: $this->_name = $name;
67: $this->_help = $help;
68: $this->_parser = $parser;
69: }
70: if (is_array($this->_parser)) {
71: $this->_parser['command'] = $this->_name;
72: $this->_parser = ConsoleOptionParser::buildFromArray($this->_parser);
73: }
74: }
75:
76: /**
77: * Get the value of the name attribute.
78: *
79: * @return string Value of this->_name.
80: */
81: public function name()
82: {
83: return $this->_name;
84: }
85:
86: /**
87: * Get the raw help string for this command
88: *
89: * @return string
90: */
91: public function getRawHelp()
92: {
93: return $this->_help;
94: }
95:
96: /**
97: * Generate the help for this this subcommand.
98: *
99: * @param int $width The width to make the name of the subcommand.
100: * @return string
101: */
102: public function help($width = 0)
103: {
104: $name = $this->_name;
105: if (strlen($name) < $width) {
106: $name = str_pad($name, $width, ' ');
107: }
108:
109: return $name . $this->_help;
110: }
111:
112: /**
113: * Get the usage value for this option
114: *
115: * @return \Cake\Console\ConsoleOptionParser|bool Either false or a ConsoleOptionParser
116: */
117: public function parser()
118: {
119: if ($this->_parser instanceof ConsoleOptionParser) {
120: return $this->_parser;
121: }
122:
123: return false;
124: }
125:
126: /**
127: * Append this subcommand to the Parent element
128: *
129: * @param \SimpleXMLElement $parent The parent element.
130: * @return \SimpleXMLElement The parent with this subcommand appended.
131: */
132: public function xml(SimpleXMLElement $parent)
133: {
134: $command = $parent->addChild('command');
135: $command->addAttribute('name', $this->_name);
136: $command->addAttribute('help', $this->_help);
137:
138: return $parent;
139: }
140: }
141: