1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Console-Kit library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/console-kit |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\ConsoleKit; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
15
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
16
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
20
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
21
|
|
|
|
22
|
|
|
class ConsoleIO |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Input. |
27
|
|
|
* |
28
|
|
|
* @var InputInterface |
29
|
|
|
*/ |
30
|
|
|
private $_input; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Output. |
34
|
|
|
* |
35
|
|
|
* @var OutputInterface |
36
|
|
|
*/ |
37
|
|
|
private $_output; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Helper set. |
41
|
|
|
* |
42
|
|
|
* @var HelperSet |
43
|
|
|
*/ |
44
|
|
|
private $_helperSet; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates class instance. |
48
|
|
|
* |
49
|
|
|
* @param InputInterface $input Input. |
50
|
|
|
* @param OutputInterface $output Output. |
51
|
|
|
* @param HelperSet $helper_set Helper set. |
52
|
|
|
*/ |
53
|
28 |
|
public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helper_set) |
54
|
|
|
{ |
55
|
28 |
|
$this->_input = $input; |
56
|
28 |
|
$this->_output = $output; |
57
|
28 |
|
$this->_helperSet = $helper_set; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets argument by name. |
62
|
|
|
* |
63
|
|
|
* @param string $name The name of the argument. |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
1 |
|
public function getArgument($name) |
68
|
|
|
{ |
69
|
1 |
|
return $this->_input->getArgument($name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets an option by name. |
74
|
|
|
* |
75
|
|
|
* @param string $name The name of the option. |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
1 |
|
public function getOption($name) |
80
|
|
|
{ |
81
|
1 |
|
return $this->_input->getOption($name); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Is this input means interactive? |
86
|
|
|
* |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
1 |
|
public function isInteractive() |
90
|
|
|
{ |
91
|
1 |
|
return $this->_input->isInteractive(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the decorated flag. |
96
|
|
|
* |
97
|
|
|
* @return boolean true if the output will decorate messages, false otherwise |
98
|
|
|
*/ |
99
|
1 |
|
public function isDecorated() |
100
|
|
|
{ |
101
|
1 |
|
return $this->_output->isDecorated(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Determines if verbose output is being requested. |
106
|
|
|
* |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
5 |
|
public function isVerbose() |
110
|
|
|
{ |
111
|
5 |
|
return $this->_output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Determines if very verbose output is being requested. |
116
|
|
|
* |
117
|
|
|
* @return boolean |
118
|
|
|
*/ |
119
|
5 |
|
public function isVeryVerbose() |
120
|
|
|
{ |
121
|
5 |
|
return $this->_output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Determines if debug output is being requested. |
126
|
|
|
* |
127
|
|
|
* @return boolean |
128
|
|
|
*/ |
129
|
5 |
|
public function isDebug() |
130
|
|
|
{ |
131
|
5 |
|
return $this->_output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Writes a message to the output. |
136
|
|
|
* |
137
|
|
|
* @param string|array $messages The message as an array of lines or a single string. |
138
|
|
|
* @param boolean $newline Whether to add a newline. |
139
|
|
|
* @param integer $type The type of output (one of the OUTPUT constants). |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
1 |
|
public function write($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL) |
144
|
|
|
{ |
145
|
1 |
|
$this->_output->write($messages, $newline, $type); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Writes a message to the output and adds a newline at the end. |
150
|
|
|
* |
151
|
|
|
* @param string|array $messages The message as an array of lines of a single string. |
152
|
|
|
* @param integer $type The type of output (one of the OUTPUT constants). |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
1 |
|
public function writeln($messages, $type = OutputInterface::OUTPUT_NORMAL) |
157
|
|
|
{ |
158
|
1 |
|
$this->_output->writeln($messages, $type); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Asks a confirmation to the user. |
163
|
|
|
* The question will be asked until the user answers by nothing, yes, or no. |
164
|
|
|
* |
165
|
|
|
* @param string|array $question The question to ask. |
166
|
|
|
* @param boolean $default The default answer if the user enters nothing. |
167
|
|
|
* |
168
|
|
|
* @return boolean true if the user has confirmed, false otherwise |
169
|
|
|
*/ |
170
|
2 |
|
public function askConfirmation($question, $default = true) |
171
|
|
|
{ |
172
|
|
|
/** @var QuestionHelper $helper */ |
173
|
2 |
|
$helper = $this->_helperSet->get('question'); |
174
|
2 |
|
$confirmation_question = new ConfirmationQuestion( |
175
|
2 |
|
'<question>' . $question . ' [' . ($default ? 'y' : 'n') . ']?</question> ', |
|
|
|
|
176
|
2 |
|
$default |
177
|
2 |
|
); |
178
|
|
|
|
179
|
2 |
|
return $helper->ask($this->_input, $this->_output, $confirmation_question); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Asks user to choose. |
184
|
|
|
* |
185
|
|
|
* @param string $question The question to ask. |
186
|
|
|
* @param array $options Valid answer options. |
187
|
|
|
* @param mixed $default Default answer. |
188
|
|
|
* @param string $error_message Error on incorrect answer. |
189
|
|
|
* |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
1 |
|
public function choose($question, array $options, $default, $error_message) |
193
|
|
|
{ |
194
|
|
|
/** @var QuestionHelper $helper */ |
195
|
1 |
|
$helper = $this->_helperSet->get('question'); |
196
|
1 |
|
$choice_question = new ChoiceQuestion('<question>' . $question . '</question> ', $options, $default); |
197
|
1 |
|
$choice_question->setErrorMessage($error_message); |
198
|
|
|
|
199
|
1 |
|
return $helper->ask($this->_input, $this->_output, $choice_question); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns progress bar instance. |
204
|
|
|
* |
205
|
|
|
* @param integer $max Maximum steps (0 if unknown). |
206
|
|
|
* |
207
|
|
|
* @return ProgressBar |
208
|
|
|
*/ |
209
|
1 |
|
public function createProgressBar($max = 0) |
210
|
|
|
{ |
211
|
1 |
|
return new ProgressBar($this->_output, $max); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Send a notification to the Terminal (via the BEL symbol). |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
1 |
|
public function notify() |
220
|
|
|
{ |
221
|
1 |
|
$this->_output->write("\x07"); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns output. |
226
|
|
|
* |
227
|
|
|
* @return OutputInterface |
228
|
|
|
*/ |
229
|
1 |
|
public function getOutput() |
230
|
|
|
{ |
231
|
1 |
|
return $this->_output; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|