1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
use Symfony\Component\Console\Helper\QuestionHelper;
21
use Symfony\Component\Console\Input\StringInput;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Question\ChoiceQuestion;
24
use Symfony\Component\Console\Question\ConfirmationQuestion;
25
use Symfony\Component\Console\Question\Question;
26

27
/**
28
 * Uses Symfony Console to present questions
29
 *
30
 * @author  Michiel Rook <mrook@php.net>
31
 * @package phing.input
32
 */
33
class ConsoleInputHandler implements InputHandler
34
{
35
    /**
36
     * @var resource
37
     */
38
    private $inputStream;
39

40
    /**
41
     * @var OutputInterface
42
     */
43
    private $output;
44

45
    /**
46
     * ConsoleInputHandler constructor.
47
     *
48
     * @param resource $inputStream
49
     * @param OutputInterface $output
50
     */
51 1
    public function __construct($inputStream, OutputInterface $output)
52
    {
53 1
        $this->inputStream = $inputStream;
54 1
        $this->output = $output;
55
    }
56

57
    /**
58
     * Handle the request encapsulated in the argument.
59
     *
60
     * <p>Precondition: the request.getPrompt will return a non-null
61
     * value.</p>
62
     *
63
     * <p>Postcondition: request.getInput will return a non-null
64
     * value, request.isInputValid will return true.</p>
65
     *
66
     * @param  InputRequest $request
67
     * @return void
68
     */
69 1
    public function handleInput(InputRequest $request)
70
    {
71 1
        $questionHelper = new QuestionHelper();
72 1
        if (method_exists($questionHelper, 'setInputStream')) {
73 0
            $questionHelper->setInputStream($this->inputStream);
74
        }
75

76 1
        $question = $this->getQuestion($request);
77

78 1
        if ($request->isHidden()) {
79 0
            $question->setHidden(true);
80
        }
81

82 1
        $input = new StringInput('');
83 1
        if (method_exists($input, 'setStream')) {
84 1
            $input->setStream($this->inputStream);
85
        }
86

87 1
        $result = $questionHelper->ask($input, $this->output, $question);
88

89 1
        $request->setInput($result);
90
    }
91

92
    /**
93
     * @param InputRequest $inputRequest
94
     * @return Question
95
     */
96 1
    protected function getQuestion(InputRequest $inputRequest)
97
    {
98 1
        $prompt = $this->getPrompt($inputRequest);
99

100 1
        if ($inputRequest instanceof YesNoInputRequest) {
101 1
            return new ConfirmationQuestion($prompt);
102
        }
103

104 1
        if ($inputRequest instanceof MultipleChoiceInputRequest) {
105 1
            return new ChoiceQuestion($prompt, $inputRequest->getChoices(), $inputRequest->getDefaultValue());
106
        }
107

108 1
        return new Question($prompt, $inputRequest->getDefaultValue());
109
    }
110

111
    /**
112
     * @param InputRequest $inputRequest
113
     * @return string
114
     */
115 1
    protected function getPrompt(InputRequest $inputRequest)
116
    {
117 1
        $prompt = $inputRequest->getPrompt();
118 1
        $defaultValue = $inputRequest->getDefaultValue();
119

120 1
        if ($defaultValue !== null) {
121 1
            if ($inputRequest instanceof YesNoInputRequest) {
122 0
                $defaultValue = $inputRequest->getChoices()[$defaultValue];
123
            }
124

125 1
            $prompt .= ' [' . $defaultValue . ']';
126
        }
127

128 1
        $pchar = $inputRequest->getPromptChar();
129

130 1
        return $prompt . ($pchar ? $pchar . ' ' : ' ');
131
    }
132
}

Read our documentation on viewing source code .

Loading