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