Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

GeneratorBundle/Helper/CommandAssistant.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Helper;
4
5
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\ChoiceQuestion;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use Symfony\Component\Console\Question\Question;
11
use Symfony\Component\HttpKernel\Kernel;
12
13
class CommandAssistant
14
{
15
    /**
16
     * @var InputInterface
17
     */
18
    private $input;
19
20
    /**
21
     * @var OutputInterface
22
     */
23
    private $output;
24
25
    /**
26
     * @var QuestionHelper
27
     */
28
    private $questionHelper;
29
30
    /**
31
     * @var Kernel
32
     */
33
    private $kernel;
34
35
    /**
36
     * @param $input InputInterface
37
     */
38
    public function setInput(InputInterface $input)
39
    {
40
        $this->input = $input;
41
    }
42
43
    /**
44
     * @return OutputInterface
45
     */
46
    public function getOutput()
47
    {
48
        return $this->output;
49
    }
50
51
    /**
52
     * @param $output OutputInterface
53
     */
54
    public function setOutput(OutputInterface $output)
55
    {
56
        $this->output = $output;
57
    }
58
59
    /**
60
     * @param $questionHelper QuestionHelper
61
     */
62
    public function setQuestionHelper(QuestionHelper $questionHelper)
63
    {
64
        $this->questionHelper = $questionHelper;
65
    }
66
67
    /**
68
     * @return Kernel
69
     */
70
    public function getKernel()
71
    {
72
        return $this->kernel;
73
    }
74
75
    /**
76
     * @param $kernel Kernel
77
     */
78
    public function setKernel(Kernel $kernel)
79
    {
80
        $this->kernel = $kernel;
81
    }
82
83
    public function writeSection($text, $style = 'bg=blue;fg=white')
84
    {
85
        $this->getQuestionHelper()->writeSection($this->output, $text, $style);
86
    }
87
88
    /**
89
     * @return Questionhelper
90
     */
91
    private function getQuestionHelper()
92
    {
93
        return $this->questionHelper;
94
    }
95
96
    public function writeLine($text, $type = OutputInterface::OUTPUT_NORMAL)
97
    {
98
        $this->output->writeln($text, $type);
99
    }
100
101
    public function write(
102
        $text,
103
        $newLine = false,
104
        $type = OutputInterface::OUTPUT_NORMAL
105
    ) {
106
        $this->output->write($text, $newLine, $type);
107
    }
108
109 View Code Duplication
    public function writeError($message, $exit = false)
110
    {
111
        $this->output->writeln(
112
            $this->getQuestionHelper()
113
                ->getHelperSet()
114
                ->get('formatter')
115
                ->formatBlock($message, 'error')
116
        );
117
        if ($exit) {
118
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method writeError() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
119
        }
120
    }
121
122 View Code Duplication
    public function askAndValidate(
123
        $question,
124
        $validator,
125
        $defaultValue = null,
126
        array $autoComplete = null
127
    ) {
128
        $validationQuestion = new Question(
129
            $this->getQuestionHelper()->getQuestion($question, $defaultValue),
130
            $defaultValue
131
        );
132
        $validationQuestion->setAutocompleterValues($autoComplete);
133
        $validationQuestion->setValidator($validator);
134
135
        return $this->getQuestionHelper()->ask(
136
            $this->input,
137
            $this->output,
138
            $validationQuestion
139
        );
140
    }
141
142
    public function askConfirmation(
143
        $question,
144
        $defaultString,
145
        $separator = '?',
146
        $defaultValue = true
147
    ) {
148
        $confirmationQuestion = new ConfirmationQuestion(
149
            $this->getQuestionHelper()->getQuestion(
150
                $question,
151
                $defaultString,
152
                $separator
153
            ), $defaultValue
154
        );
155
156
        return $this->getQuestionHelper()->ask(
157
            $this->input,
158
            $this->output,
159
            $confirmationQuestion
160
        );
161
    }
162
163 View Code Duplication
    public function ask($question, $default = null, array $autoComplete = null)
164
    {
165
        $askQuestion = new Question(
166
            $this->questionHelper->getQuestion($question, $default), $default
167
        );
168
        $askQuestion->setAutocompleterValues($autoComplete);
169
170
        return $this->getQuestionHelper()->ask(
171
            $this->input,
172
            $this->output,
173
            $askQuestion
174
        );
175
    }
176
177
    public function askSelect(
178
        $question,
179
        $choices,
180
        $default = null,
181
        $multiSelect = false,
182
        $errorMessage = 'Value "%s" is invalid'
183
    ) {
184
        $bundleQuestion = new ChoiceQuestion(
185
            $this->getQuestionHelper()->getQuestion($question, $default),
186
            $choices
187
        );
188
        $bundleQuestion->setErrorMessage($errorMessage);
189
        $bundleQuestion->setMultiselect($multiSelect);
190
        if ($multiSelect) {
191
            $toReturn = array();
192
            foreach ($this->getQuestionHelper()->ask(
193
                $this->input,
194
                $this->output,
195
                $bundleQuestion
196
            ) as $each) {
197
                array_push(
198
                    $toReturn,
199
                    array_search($each, $bundleQuestion->getChoices())
200
                );
201
            }
202
203
            return $toReturn;
204
        } else {
205
            $value = $this->getQuestionHelper()->ask(
206
                $this->input,
207
                $this->output,
208
                $bundleQuestion
209
            );
210
211
            return array_search($value, $bundleQuestion->getChoices());
212
        }
213
    }
214
215
    public function setOption($name, $value)
216
    {
217
        $this->input->setOption($name, $value);
218
    }
219
220
    public function hasOption($name)
221
    {
222
        return $this->input->hasOption($name);
223
    }
224
225
    public function getOption($name)
226
    {
227
        return $this->input->getOption($name);
228
    }
229
230
    public function isInteractive()
231
    {
232
        return $this->input->isInteractive();
233
    }
234
235
    public function getOptionOrDefault($option, $default = null)
236
    {
237
        return $this->input->hasOption($option) ? $this->input->getOption(
238
            $option
239
        ) : $default;
240
    }
241
}
242