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