ConsoleInputSubcommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * ConsoleInputSubcommand file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. * Name of the subcommand
  29. *
  30. * @var string
  31. */
  32. protected $_name;
  33. /**
  34. * Help string for the subcommand
  35. *
  36. * @var string
  37. */
  38. protected $_help;
  39. /**
  40. * The ConsoleOptionParser for this subcommand.
  41. *
  42. * @var ConsoleOptionParser
  43. */
  44. protected $_parser;
  45. /**
  46. * Make a new Subcommand
  47. *
  48. * @param string|array $name The long name of the subcommand, or an array with all the properties.
  49. * @param string $help The help text for this option
  50. * @param ConsoleOptionParser|array $parser A parser for this subcommand. Either a ConsoleOptionParser, or an array that can be
  51. * used with ConsoleOptionParser::buildFromArray()
  52. */
  53. public function __construct($name, $help = '', $parser = null) {
  54. if (is_array($name) && isset($name['name'])) {
  55. foreach ($name as $key => $value) {
  56. $this->{'_' . $key} = $value;
  57. }
  58. } else {
  59. $this->_name = $name;
  60. $this->_help = $help;
  61. $this->_parser = $parser;
  62. }
  63. if (is_array($this->_parser)) {
  64. $this->_parser['command'] = $this->_name;
  65. $this->_parser = ConsoleOptionParser::buildFromArray($this->_parser);
  66. }
  67. }
  68. /**
  69. * Get the value of the name attribute.
  70. *
  71. * @return string Value of this->_name.
  72. */
  73. public function name() {
  74. return $this->_name;
  75. }
  76. /**
  77. * Generate the help for this this subcommand.
  78. *
  79. * @param integer $width The width to make the name of the subcommand.
  80. * @return string
  81. */
  82. public function help($width = 0) {
  83. $name = $this->_name;
  84. if (strlen($name) < $width) {
  85. $name = str_pad($name, $width, ' ');
  86. }
  87. return $name . $this->_help;
  88. }
  89. /**
  90. * Get the usage value for this option
  91. *
  92. * @return mixed Either false or a ConsoleOptionParser
  93. */
  94. public function parser() {
  95. if ($this->_parser instanceof ConsoleOptionParser) {
  96. return $this->_parser;
  97. }
  98. return false;
  99. }
  100. /**
  101. * Append this subcommand to the Parent element
  102. *
  103. * @param SimpleXmlElement $parent The parent element.
  104. * @return SimpleXmlElement The parent with this subcommand appended.
  105. */
  106. public function xml(SimpleXmlElement $parent) {
  107. $command = $parent->addChild('command');
  108. $command->addAttribute('name', $this->_name);
  109. $command->addAttribute('help', $this->_help);
  110. return $parent;
  111. }
  112. }