Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/console/Helper/QuestionHelper.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 7a779792577d |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
11 | 11 |
12 namespace Symfony\Component\Console\Helper; | 12 namespace Symfony\Component\Console\Helper; |
13 | 13 |
14 use Symfony\Component\Console\Exception\InvalidArgumentException; | 14 use Symfony\Component\Console\Exception\InvalidArgumentException; |
15 use Symfony\Component\Console\Exception\RuntimeException; | 15 use Symfony\Component\Console\Exception\RuntimeException; |
16 use Symfony\Component\Console\Formatter\OutputFormatter; | |
17 use Symfony\Component\Console\Formatter\OutputFormatterStyle; | |
16 use Symfony\Component\Console\Input\InputInterface; | 18 use Symfony\Component\Console\Input\InputInterface; |
17 use Symfony\Component\Console\Input\StreamableInputInterface; | 19 use Symfony\Component\Console\Input\StreamableInputInterface; |
18 use Symfony\Component\Console\Output\ConsoleOutputInterface; | 20 use Symfony\Component\Console\Output\ConsoleOutputInterface; |
19 use Symfony\Component\Console\Output\OutputInterface; | 21 use Symfony\Component\Console\Output\OutputInterface; |
20 use Symfony\Component\Console\Formatter\OutputFormatterStyle; | |
21 use Symfony\Component\Console\Question\Question; | 22 use Symfony\Component\Console\Question\Question; |
22 use Symfony\Component\Console\Question\ChoiceQuestion; | 23 use Symfony\Component\Console\Question\ChoiceQuestion; |
23 | 24 |
24 /** | 25 /** |
25 * The QuestionHelper class provides helpers to interact with the user. | 26 * The QuestionHelper class provides helpers to interact with the user. |
33 private static $stty; | 34 private static $stty; |
34 | 35 |
35 /** | 36 /** |
36 * Asks a question to the user. | 37 * Asks a question to the user. |
37 * | 38 * |
38 * @param InputInterface $input An InputInterface instance | |
39 * @param OutputInterface $output An OutputInterface instance | |
40 * @param Question $question The question to ask | |
41 * | |
42 * @return mixed The user answer | 39 * @return mixed The user answer |
43 * | 40 * |
44 * @throws RuntimeException If there is no data to read in the input stream | 41 * @throws RuntimeException If there is no data to read in the input stream |
45 */ | 42 */ |
46 public function ask(InputInterface $input, OutputInterface $output, Question $question) | 43 public function ask(InputInterface $input, OutputInterface $output, Question $question) |
48 if ($output instanceof ConsoleOutputInterface) { | 45 if ($output instanceof ConsoleOutputInterface) { |
49 $output = $output->getErrorOutput(); | 46 $output = $output->getErrorOutput(); |
50 } | 47 } |
51 | 48 |
52 if (!$input->isInteractive()) { | 49 if (!$input->isInteractive()) { |
50 if ($question instanceof ChoiceQuestion) { | |
51 $choices = $question->getChoices(); | |
52 | |
53 return $choices[$question->getDefault()]; | |
54 } | |
55 | |
53 return $question->getDefault(); | 56 return $question->getDefault(); |
54 } | 57 } |
55 | 58 |
56 if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) { | 59 if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) { |
57 $this->inputStream = $stream; | 60 $this->inputStream = $stream; |
80 * | 83 * |
81 * @throws InvalidArgumentException In case the stream is not a resource | 84 * @throws InvalidArgumentException In case the stream is not a resource |
82 */ | 85 */ |
83 public function setInputStream($stream) | 86 public function setInputStream($stream) |
84 { | 87 { |
85 @trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use %s::setStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED); | 88 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.2 and will be removed in 4.0. Use %s::setStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED); |
86 | 89 |
87 if (!is_resource($stream)) { | 90 if (!is_resource($stream)) { |
88 throw new InvalidArgumentException('Input stream must be a valid resource.'); | 91 throw new InvalidArgumentException('Input stream must be a valid resource.'); |
89 } | 92 } |
90 | 93 |
100 * @return resource | 103 * @return resource |
101 */ | 104 */ |
102 public function getInputStream() | 105 public function getInputStream() |
103 { | 106 { |
104 if (0 === func_num_args() || func_get_arg(0)) { | 107 if (0 === func_num_args() || func_get_arg(0)) { |
105 @trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use %s::getStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED); | 108 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.2 and will be removed in 4.0. Use %s::getStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED); |
106 } | 109 } |
107 | 110 |
108 return $this->inputStream; | 111 return $this->inputStream; |
109 } | 112 } |
110 | 113 |
115 { | 118 { |
116 return 'question'; | 119 return 'question'; |
117 } | 120 } |
118 | 121 |
119 /** | 122 /** |
123 * Prevents usage of stty. | |
124 */ | |
125 public static function disableStty() | |
126 { | |
127 self::$stty = false; | |
128 } | |
129 | |
130 /** | |
120 * Asks the question to the user. | 131 * Asks the question to the user. |
121 * | |
122 * @param OutputInterface $output | |
123 * @param Question $question | |
124 * | 132 * |
125 * @return bool|mixed|null|string | 133 * @return bool|mixed|null|string |
126 * | 134 * |
127 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden | 135 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden |
128 */ | 136 */ |
151 throw new RuntimeException('Aborted'); | 159 throw new RuntimeException('Aborted'); |
152 } | 160 } |
153 $ret = trim($ret); | 161 $ret = trim($ret); |
154 } | 162 } |
155 } else { | 163 } else { |
156 $ret = trim($this->autocomplete($output, $question, $inputStream)); | 164 $ret = trim($this->autocomplete($output, $question, $inputStream, is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false))); |
157 } | 165 } |
158 | 166 |
159 $ret = strlen($ret) > 0 ? $ret : $question->getDefault(); | 167 $ret = strlen($ret) > 0 ? $ret : $question->getDefault(); |
160 | 168 |
161 if ($normalizer = $question->getNormalizer()) { | 169 if ($normalizer = $question->getNormalizer()) { |
165 return $ret; | 173 return $ret; |
166 } | 174 } |
167 | 175 |
168 /** | 176 /** |
169 * Outputs the question prompt. | 177 * Outputs the question prompt. |
170 * | |
171 * @param OutputInterface $output | |
172 * @param Question $question | |
173 */ | 178 */ |
174 protected function writePrompt(OutputInterface $output, Question $question) | 179 protected function writePrompt(OutputInterface $output, Question $question) |
175 { | 180 { |
176 $message = $question->getQuestion(); | 181 $message = $question->getQuestion(); |
177 | 182 |
192 $output->write($message); | 197 $output->write($message); |
193 } | 198 } |
194 | 199 |
195 /** | 200 /** |
196 * Outputs an error message. | 201 * Outputs an error message. |
197 * | |
198 * @param OutputInterface $output | |
199 * @param \Exception $error | |
200 */ | 202 */ |
201 protected function writeError(OutputInterface $output, \Exception $error) | 203 protected function writeError(OutputInterface $output, \Exception $error) |
202 { | 204 { |
203 if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) { | 205 if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) { |
204 $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'); | 206 $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'); |
213 * Autocompletes a question. | 215 * Autocompletes a question. |
214 * | 216 * |
215 * @param OutputInterface $output | 217 * @param OutputInterface $output |
216 * @param Question $question | 218 * @param Question $question |
217 * @param resource $inputStream | 219 * @param resource $inputStream |
220 * @param array $autocomplete | |
218 * | 221 * |
219 * @return string | 222 * @return string |
220 */ | 223 */ |
221 private function autocomplete(OutputInterface $output, Question $question, $inputStream) | 224 private function autocomplete(OutputInterface $output, Question $question, $inputStream, array $autocomplete) |
222 { | 225 { |
223 $autocomplete = $question->getAutocompleterValues(); | |
224 $ret = ''; | 226 $ret = ''; |
225 | 227 |
226 $i = 0; | 228 $i = 0; |
227 $ofs = -1; | 229 $ofs = -1; |
228 $matches = $autocomplete; | 230 $matches = $autocomplete; |
246 --$i; | 248 --$i; |
247 // Move cursor backwards | 249 // Move cursor backwards |
248 $output->write("\033[1D"); | 250 $output->write("\033[1D"); |
249 } | 251 } |
250 | 252 |
251 if ($i === 0) { | 253 if (0 === $i) { |
252 $ofs = -1; | 254 $ofs = -1; |
253 $matches = $autocomplete; | 255 $matches = $autocomplete; |
254 $numMatches = count($matches); | 256 $numMatches = count($matches); |
255 } else { | 257 } else { |
256 $numMatches = 0; | 258 $numMatches = 0; |
314 | 316 |
315 if ($numMatches > 0 && -1 !== $ofs) { | 317 if ($numMatches > 0 && -1 !== $ofs) { |
316 // Save cursor position | 318 // Save cursor position |
317 $output->write("\0337"); | 319 $output->write("\0337"); |
318 // Write highlighted text | 320 // Write highlighted text |
319 $output->write('<hl>'.substr($matches[$ofs], $i).'</hl>'); | 321 $output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $i)).'</hl>'); |
320 // Restore cursor position | 322 // Restore cursor position |
321 $output->write("\0338"); | 323 $output->write("\0338"); |
322 } | 324 } |
323 } | 325 } |
324 | 326 |
376 | 378 |
377 return $value; | 379 return $value; |
378 } | 380 } |
379 | 381 |
380 if (false !== $shell = $this->getShell()) { | 382 if (false !== $shell = $this->getShell()) { |
381 $readCmd = $shell === 'csh' ? 'set mypassword = $<' : 'read -r mypassword'; | 383 $readCmd = 'csh' === $shell ? 'set mypassword = $<' : 'read -r mypassword'; |
382 $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd); | 384 $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd); |
383 $value = rtrim(shell_exec($command)); | 385 $value = rtrim(shell_exec($command)); |
384 $output->writeln(''); | 386 $output->writeln(''); |
385 | 387 |
386 return $value; | 388 return $value; |
458 return self::$stty; | 460 return self::$stty; |
459 } | 461 } |
460 | 462 |
461 exec('stty 2>&1', $output, $exitcode); | 463 exec('stty 2>&1', $output, $exitcode); |
462 | 464 |
463 return self::$stty = $exitcode === 0; | 465 return self::$stty = 0 === $exitcode; |
464 } | 466 } |
465 } | 467 } |