1<?php
2/*
3 *  $Id: ae986f84ca9d952b0e14f2df9f21657213ef5e47 $
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
22require_once 'phing/Task.php';
23include_once 'phing/input/InputRequest.php';
24include_once 'phing/input/YesNoInputRequest.php';
25include_once 'phing/input/MultipleChoiceInputRequest.php';
26
27/**
28 * Reads input from the InputHandler.
29 *
30 * @see       Project::getInputHandler()
31 * @author    Hans Lellelid <hans@xmpl.org> (Phing)
32 * @author    Ulrich Schmidt <usch@usch.net> (Ant)
33 * @author    Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
34 * @version   $Id: ae986f84ca9d952b0e14f2df9f21657213ef5e47 $
35 * @package   phing.tasks.system
36 */
37class InputTask extends Task {
38
39    private $validargs;
40    private $message = ""; // required
41    private $propertyName; // required
42    private $defaultValue;
43    private $promptChar;
44
45    /**
46     * Defines valid input parameters as comma separated strings. If set, input
47     * task will reject any input not defined as accepted and requires the user
48     * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
49     * be accepted you need to define both values as accepted arguments.
50     *
51     * @param validargs A comma separated String defining valid input args.
52     */
53    public function setValidargs ($validargs) {
54        $this->validargs = $validargs;
55    }
56
57    /**
58     * Defines the name of a property to be set from input.
59     *
60     * @param string $name Name for the property to be set from input
61     */
62    public function setPropertyName($name) {
63        $this->propertyName = $name;
64    }
65
66    /**
67     * Sets the Message which gets displayed to the user during the build run.
68     * @param message The message to be displayed.
69     */
70    public function setMessage ($message) {
71        $this->message = $message;
72    }
73
74    /**
75     * Set a multiline message.
76     */
77    public function addText($msg) {
78        $this->message .= $this->project->replaceProperties($msg);
79    }
80
81    /**
82     * Add a default value.
83     * @param string $v
84     */
85    public function setDefaultValue($v) {
86        $this->defaultValue = $v;
87    }
88
89    /**
90     * Set the character/string to use for the prompt.
91     * @param string $c
92     */
93    public function setPromptChar($c) {
94        $this->promptChar = $c;
95    }
96
97    /**
98     * Actual method executed by phing.
99     * @throws BuildException
100     */
101    public function main() {
102
103        if ($this->propertyName === null) {
104            throw new BuildException("You must specify a value for propertyName attribute.");
105        }
106
107        if ($this->message === "") {
108            throw new BuildException("You must specify a message for input task.");
109        }
110
111        if ($this->validargs !== null) {
112            $accept = preg_split('/[\s,]+/', $this->validargs);
113
114            // is it a boolean (yes/no) inputrequest?
115            $yesno = false;
116            if (count($accept) == 2) {
117                $yesno = true;
118                foreach($accept as $ans) {
119                    if(!StringHelper::isBoolean($ans)) {
120                        $yesno = false;
121                        break;
122                    }
123                }
124            }
125            if ($yesno) $request = new YesNoInputRequest($this->message, $accept);
126            else $request = new MultipleChoiceInputRequest($this->message, $accept);
127        } else {
128            $request = new InputRequest($this->message);
129        }
130
131        // default default is curr prop value
132        $request->setDefaultValue($this->project->getProperty($this->propertyName));
133
134        $request->setPromptChar($this->promptChar);
135
136        // unless overridden...
137        if ($this->defaultValue !== null) {
138            $request->setDefaultValue($this->defaultValue);
139        }
140
141        $this->project->getInputHandler()->handleInput($request);
142
143        $value = $request->getInput();
144
145        if ($value !== null) {
146            $this->project->setUserProperty($this->propertyName, $value);
147        }
148    }
149
150}
151