ConsoleInputSubcommand.php 2.9 KB

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