InputTask.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * $Id: InputTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. include_once 'phing/input/InputRequest.php';
  23. include_once 'phing/input/YesNoInputRequest.php';
  24. include_once 'phing/input/MultipleChoiceInputRequest.php';
  25. /**
  26. * Reads input from the InputHandler.
  27. *
  28. * @see Project::getInputHandler()
  29. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  30. * @author Ulrich Schmidt <usch@usch.net> (Ant)
  31. * @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
  32. * @version $Revision: 905 $
  33. * @package phing.tasks.system
  34. */
  35. class InputTask extends Task {
  36. private $validargs;
  37. private $message = ""; // required
  38. private $propertyName; // required
  39. private $defaultValue;
  40. private $promptChar;
  41. /**
  42. * Defines valid input parameters as comma separated strings. If set, input
  43. * task will reject any input not defined as accepted and requires the user
  44. * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
  45. * be accepted you need to define both values as accepted arguments.
  46. *
  47. * @param validargs A comma separated String defining valid input args.
  48. */
  49. public function setValidargs ($validargs) {
  50. $this->validargs = $validargs;
  51. }
  52. /**
  53. * Defines the name of a property to be set from input.
  54. *
  55. * @param string $name Name for the property to be set from input
  56. */
  57. public function setPropertyName($name) {
  58. $this->propertyName = $name;
  59. }
  60. /**
  61. * Sets the Message which gets displayed to the user during the build run.
  62. * @param message The message to be displayed.
  63. */
  64. public function setMessage ($message) {
  65. $this->message = $message;
  66. }
  67. /**
  68. * Set a multiline message.
  69. */
  70. public function addText($msg) {
  71. $this->message .= $this->project->replaceProperties($msg);
  72. }
  73. /**
  74. * Add a default value.
  75. * @param string $v
  76. */
  77. public function setDefaultValue($v) {
  78. $this->defaultValue = $v;
  79. }
  80. /**
  81. * Set the character/string to use for the prompt.
  82. * @param string $c
  83. */
  84. public function setPromptChar($c) {
  85. $this->promptChar = $c;
  86. }
  87. /**
  88. * Actual method executed by phing.
  89. * @throws BuildException
  90. */
  91. public function main() {
  92. if ($this->propertyName === null) {
  93. throw new BuildException("You must specify a value for propertyName attribute.");
  94. }
  95. if ($this->validargs !== null) {
  96. $accept = preg_split('/[\s,]+/', $this->validargs);
  97. // is it a boolean (yes/no) inputrequest?
  98. $yesno = false;
  99. if (count($accept) == 2) {
  100. $yesno = true;
  101. foreach($accept as $ans) {
  102. if(!StringHelper::isBoolean($ans)) {
  103. $yesno = false;
  104. break;
  105. }
  106. }
  107. }
  108. if ($yesno) $request = new YesNoInputRequest($this->message, $accept);
  109. else $request = new MultipleChoiceInputRequest($this->message, $accept);
  110. } else {
  111. $request = new InputRequest($this->message);
  112. }
  113. // default default is curr prop value
  114. $request->setDefaultValue($this->project->getProperty($this->propertyName));
  115. $request->setPromptChar($this->promptChar);
  116. // unless overridden...
  117. if ($this->defaultValue !== null) {
  118. $request->setDefaultValue($this->defaultValue);
  119. }
  120. $this->project->getInputHandler()->handleInput($request);
  121. $value = $request->getInput();
  122. if ($value !== null) {
  123. $this->project->setUserProperty($this->propertyName, $value);
  124. }
  125. }
  126. }