| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* $Id: DefaultInputHandler.php 293 2007-11-04 16:51:45Z hans $
|
|
|
5 |
*
|
|
|
6 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
7 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
8 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
9 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
10 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
11 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
12 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
13 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
14 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
15 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
16 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
17 |
*
|
|
|
18 |
* This software consists of voluntary contributions made by many individuals
|
|
|
19 |
* and is licensed under the LGPL. For more information please see
|
|
|
20 |
* <http://phing.info>.
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
require_once 'phing/input/InputHandler.php';
|
|
|
24 |
include_once 'phing/system/io/ConsoleReader.php';
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Prompts using print(); reads input from Console.
|
|
|
28 |
*
|
|
|
29 |
* @author Hans Lellelid <hans@xmpl.org> (Phing)
|
|
|
30 |
* @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
|
|
|
31 |
* @version $Revision: 1.6 $
|
|
|
32 |
* @package phing.input
|
|
|
33 |
*/
|
|
|
34 |
class DefaultInputHandler implements InputHandler {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Prompts and requests input. May loop until a valid input has
|
|
|
38 |
* been entered.
|
|
|
39 |
* @throws BuildException
|
|
|
40 |
*/
|
|
|
41 |
public function handleInput(InputRequest $request) {
|
|
|
42 |
$prompt = $this->getPrompt($request);
|
|
|
43 |
$in = new ConsoleReader();
|
|
|
44 |
do {
|
|
|
45 |
print $prompt;
|
|
|
46 |
try {
|
|
|
47 |
$input = $in->readLine();
|
|
|
48 |
if ($input === "" && ($request->getDefaultValue() !== null) ) {
|
|
|
49 |
$input = $request->getDefaultValue();
|
|
|
50 |
}
|
|
|
51 |
$request->setInput($input);
|
|
|
52 |
} catch (Exception $e) {
|
|
|
53 |
throw new BuildException("Failed to read input from Console.", $e);
|
|
|
54 |
}
|
|
|
55 |
} while (!$request->isInputValid());
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Constructs user prompt from a request.
|
|
|
60 |
*
|
|
|
61 |
* <p>This implementation adds (choice1,choice2,choice3,...) to the
|
|
|
62 |
* prompt for <code>MultipleChoiceInputRequest</code>s.</p>
|
|
|
63 |
*
|
|
|
64 |
* @param $request the request to construct the prompt for.
|
|
|
65 |
* Must not be <code>null</code>.
|
|
|
66 |
*/
|
|
|
67 |
protected function getPrompt(InputRequest $request) {
|
|
|
68 |
$prompt = $request->getPrompt();
|
|
|
69 |
|
|
|
70 |
if ($request instanceof YesNoInputRequest) {
|
|
|
71 |
$prompt .= '(' . implode('/', $request->getChoices()) .')';
|
|
|
72 |
} elseif ($request instanceof MultipleChoiceInputRequest) { // (a,b,c,d)
|
|
|
73 |
$prompt .= '(' . implode(',', $request->getChoices()) . ')';
|
|
|
74 |
}
|
|
|
75 |
if ($request->getDefaultValue() !== null) {
|
|
|
76 |
$prompt .= ' ['.$request->getDefaultValue().']';
|
|
|
77 |
}
|
|
|
78 |
$pchar = $request->getPromptChar();
|
|
|
79 |
return $prompt . ($pchar ? $pchar . ' ' : ' ');
|
|
|
80 |
}
|
|
|
81 |
}
|