Base for a static organization website

ConsoleInputSubcommand.php 3.0KB

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