1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Input; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
24
|
|
|
use Symfony\Component\Console\Input\StringInput; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
27
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
28
|
|
|
use Symfony\Component\Console\Question\Question; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Uses Symfony Console to present questions. |
32
|
|
|
* |
33
|
|
|
* @author Michiel Rook <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class ConsoleInputHandler implements InputHandler |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var resource |
39
|
|
|
*/ |
40
|
|
|
private $inputStream; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var OutputInterface |
44
|
|
|
*/ |
45
|
|
|
private $output; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ConsoleInputHandler constructor. |
49
|
|
|
* |
50
|
|
|
* @param resource $inputStream |
51
|
|
|
*/ |
52
|
3 |
|
public function __construct($inputStream, OutputInterface $output) |
53
|
|
|
{ |
54
|
3 |
|
$this->inputStream = $inputStream; |
55
|
3 |
|
$this->output = $output; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Handle the request encapsulated in the argument. |
60
|
|
|
* |
61
|
|
|
* <p>Precondition: the request.getPrompt will return a non-null |
62
|
|
|
* value.</p> |
63
|
|
|
* |
64
|
|
|
* <p>Postcondition: request.getInput will return a non-null |
65
|
|
|
* value, request.isInputValid will return true.</p> |
66
|
|
|
*/ |
67
|
3 |
|
public function handleInput(InputRequest $request) |
68
|
|
|
{ |
69
|
3 |
|
$questionHelper = new QuestionHelper(); |
70
|
3 |
|
if (method_exists($questionHelper, 'setInputStream')) { |
71
|
|
|
$questionHelper->setInputStream($this->inputStream); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
$question = $this->getQuestion($request); |
75
|
|
|
|
76
|
3 |
|
if ($request->isHidden()) { |
77
|
|
|
$question->setHidden(true); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
$input = new StringInput(''); |
81
|
3 |
|
if (method_exists($input, 'setStream')) { |
82
|
3 |
|
$input->setStream($this->inputStream); |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
$result = $questionHelper->ask($input, $this->output, $question); |
86
|
|
|
|
87
|
3 |
|
$request->setInput($result); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Question |
92
|
|
|
*/ |
93
|
3 |
|
protected function getQuestion(InputRequest $inputRequest) |
94
|
|
|
{ |
95
|
3 |
|
$prompt = $this->getPrompt($inputRequest); |
96
|
|
|
|
97
|
3 |
|
if ($inputRequest instanceof YesNoInputRequest) { |
98
|
1 |
|
return new ConfirmationQuestion($prompt); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
if ($inputRequest instanceof MultipleChoiceInputRequest) { |
102
|
1 |
|
return new ChoiceQuestion($prompt, $inputRequest->getChoices(), $inputRequest->getDefaultValue()); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return new Question($prompt, $inputRequest->getDefaultValue()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
3 |
|
protected function getPrompt(InputRequest $inputRequest) |
112
|
|
|
{ |
113
|
3 |
|
$prompt = $inputRequest->getPrompt(); |
114
|
3 |
|
$defaultValue = $inputRequest->getDefaultValue(); |
115
|
|
|
|
116
|
3 |
|
if (null !== $defaultValue) { |
|
|
|
|
117
|
1 |
|
if ($inputRequest instanceof YesNoInputRequest) { |
118
|
|
|
$defaultValue = $inputRequest->getChoices()[$defaultValue]; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$prompt .= ' [' . $defaultValue . ']'; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
$pchar = $inputRequest->getPromptChar(); |
125
|
|
|
|
126
|
3 |
|
return $prompt . ($pchar ? $pchar . ' ' : ' '); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|