comparison vendor/symfony/console/Helper/QuestionHelper.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
17 use Symfony\Component\Console\Formatter\OutputFormatterStyle; 17 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18 use Symfony\Component\Console\Input\InputInterface; 18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\StreamableInputInterface; 19 use Symfony\Component\Console\Input\StreamableInputInterface;
20 use Symfony\Component\Console\Output\ConsoleOutputInterface; 20 use Symfony\Component\Console\Output\ConsoleOutputInterface;
21 use Symfony\Component\Console\Output\OutputInterface; 21 use Symfony\Component\Console\Output\OutputInterface;
22 use Symfony\Component\Console\Question\ChoiceQuestion;
22 use Symfony\Component\Console\Question\Question; 23 use Symfony\Component\Console\Question\Question;
23 use Symfony\Component\Console\Question\ChoiceQuestion;
24 24
25 /** 25 /**
26 * The QuestionHelper class provides helpers to interact with the user. 26 * The QuestionHelper class provides helpers to interact with the user.
27 * 27 *
28 * @author Fabien Potencier <fabien@symfony.com> 28 * @author Fabien Potencier <fabien@symfony.com>
45 if ($output instanceof ConsoleOutputInterface) { 45 if ($output instanceof ConsoleOutputInterface) {
46 $output = $output->getErrorOutput(); 46 $output = $output->getErrorOutput();
47 } 47 }
48 48
49 if (!$input->isInteractive()) { 49 if (!$input->isInteractive()) {
50 if ($question instanceof ChoiceQuestion) { 50 $default = $question->getDefault();
51
52 if (null !== $default && $question instanceof ChoiceQuestion) {
51 $choices = $question->getChoices(); 53 $choices = $question->getChoices();
52 54
53 return $choices[$question->getDefault()]; 55 if (!$question->isMultiselect()) {
54 } 56 return isset($choices[$default]) ? $choices[$default] : $default;
55 57 }
56 return $question->getDefault(); 58
59 $default = explode(',', $default);
60 foreach ($default as $k => $v) {
61 $v = trim($v);
62 $default[$k] = isset($choices[$v]) ? $choices[$v] : $v;
63 }
64 }
65
66 return $default;
57 } 67 }
58 68
59 if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) { 69 if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) {
60 $this->inputStream = $stream; 70 $this->inputStream = $stream;
61 } 71 }
85 */ 95 */
86 public function setInputStream($stream) 96 public function setInputStream($stream)
87 { 97 {
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); 98 @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);
89 99
90 if (!is_resource($stream)) { 100 if (!\is_resource($stream)) {
91 throw new InvalidArgumentException('Input stream must be a valid resource.'); 101 throw new InvalidArgumentException('Input stream must be a valid resource.');
92 } 102 }
93 103
94 $this->inputStream = $stream; 104 $this->inputStream = $stream;
95 } 105 }
102 * 112 *
103 * @return resource 113 * @return resource
104 */ 114 */
105 public function getInputStream() 115 public function getInputStream()
106 { 116 {
107 if (0 === func_num_args() || func_get_arg(0)) { 117 if (0 === \func_num_args() || func_get_arg(0)) {
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); 118 @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);
109 } 119 }
110 120
111 return $this->inputStream; 121 return $this->inputStream;
112 } 122 }
128 } 138 }
129 139
130 /** 140 /**
131 * Asks the question to the user. 141 * Asks the question to the user.
132 * 142 *
133 * @return bool|mixed|null|string 143 * @return bool|mixed|string|null
134 * 144 *
135 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden 145 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden
136 */ 146 */
137 private function doAsk(OutputInterface $output, Question $question) 147 private function doAsk(OutputInterface $output, Question $question)
138 { 148 {
159 throw new RuntimeException('Aborted'); 169 throw new RuntimeException('Aborted');
160 } 170 }
161 $ret = trim($ret); 171 $ret = trim($ret);
162 } 172 }
163 } else { 173 } else {
164 $ret = trim($this->autocomplete($output, $question, $inputStream, is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false))); 174 $ret = trim($this->autocomplete($output, $question, $inputStream, \is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
165 } 175 }
166 176
167 $ret = strlen($ret) > 0 ? $ret : $question->getDefault(); 177 $ret = \strlen($ret) > 0 ? $ret : $question->getDefault();
168 178
169 if ($normalizer = $question->getNormalizer()) { 179 if ($normalizer = $question->getNormalizer()) {
170 return $normalizer($ret); 180 return $normalizer($ret);
171 } 181 }
172 182
179 protected function writePrompt(OutputInterface $output, Question $question) 189 protected function writePrompt(OutputInterface $output, Question $question)
180 { 190 {
181 $message = $question->getQuestion(); 191 $message = $question->getQuestion();
182 192
183 if ($question instanceof ChoiceQuestion) { 193 if ($question instanceof ChoiceQuestion) {
184 $maxWidth = max(array_map(array($this, 'strlen'), array_keys($question->getChoices()))); 194 $maxWidth = max(array_map([$this, 'strlen'], array_keys($question->getChoices())));
185 195
186 $messages = (array) $question->getQuestion(); 196 $messages = (array) $question->getQuestion();
187 foreach ($question->getChoices() as $key => $value) { 197 foreach ($question->getChoices() as $key => $value) {
188 $width = $maxWidth - $this->strlen($key); 198 $width = $maxWidth - $this->strlen($key);
189 $messages[] = ' [<info>'.$key.str_repeat(' ', $width).'</info>] '.$value; 199 $messages[] = ' [<info>'.$key.str_repeat(' ', $width).'</info>] '.$value;
226 $ret = ''; 236 $ret = '';
227 237
228 $i = 0; 238 $i = 0;
229 $ofs = -1; 239 $ofs = -1;
230 $matches = $autocomplete; 240 $matches = $autocomplete;
231 $numMatches = count($matches); 241 $numMatches = \count($matches);
232 242
233 $sttyMode = shell_exec('stty -g'); 243 $sttyMode = shell_exec('stty -g');
234 244
235 // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) 245 // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
236 shell_exec('stty -icanon -echo'); 246 shell_exec('stty -icanon -echo');
251 } 261 }
252 262
253 if (0 === $i) { 263 if (0 === $i) {
254 $ofs = -1; 264 $ofs = -1;
255 $matches = $autocomplete; 265 $matches = $autocomplete;
256 $numMatches = count($matches); 266 $numMatches = \count($matches);
257 } else { 267 } else {
258 $numMatches = 0; 268 $numMatches = 0;
259 } 269 }
260 270
261 // Pop the last character off the end of our string 271 // Pop the last character off the end of our string
275 } 285 }
276 286
277 $ofs += ('A' === $c[2]) ? -1 : 1; 287 $ofs += ('A' === $c[2]) ? -1 : 1;
278 $ofs = ($numMatches + $ofs) % $numMatches; 288 $ofs = ($numMatches + $ofs) % $numMatches;
279 } 289 }
280 } elseif (ord($c) < 32) { 290 } elseif (\ord($c) < 32) {
281 if ("\t" === $c || "\n" === $c) { 291 if ("\t" === $c || "\n" === $c) {
282 if ($numMatches > 0 && -1 !== $ofs) { 292 if ($numMatches > 0 && -1 !== $ofs) {
283 $ret = $matches[$ofs]; 293 $ret = $matches[$ofs];
284 // Echo out remaining chars for current match 294 // Echo out remaining chars for current match
285 $output->write(substr($ret, $i)); 295 $output->write(substr($ret, $i));
286 $i = strlen($ret); 296 $i = \strlen($ret);
287 } 297 }
288 298
289 if ("\n" === $c) { 299 if ("\n" === $c) {
290 $output->write($c); 300 $output->write($c);
291 break; 301 break;
340 * 350 *
341 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden 351 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden
342 */ 352 */
343 private function getHiddenResponse(OutputInterface $output, $inputStream) 353 private function getHiddenResponse(OutputInterface $output, $inputStream)
344 { 354 {
345 if ('\\' === DIRECTORY_SEPARATOR) { 355 if ('\\' === \DIRECTORY_SEPARATOR) {
346 $exe = __DIR__.'/../Resources/bin/hiddeninput.exe'; 356 $exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
347 357
348 // handle code running from a phar 358 // handle code running from a phar
349 if ('phar:' === substr(__FILE__, 0, 5)) { 359 if ('phar:' === substr(__FILE__, 0, 5)) {
350 $tmpExe = sys_get_temp_dir().'/hiddeninput.exe'; 360 $tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
410 if (null !== $error) { 420 if (null !== $error) {
411 $this->writeError($output, $error); 421 $this->writeError($output, $error);
412 } 422 }
413 423
414 try { 424 try {
415 return call_user_func($question->getValidator(), $interviewer()); 425 return \call_user_func($question->getValidator(), $interviewer());
416 } catch (RuntimeException $e) { 426 } catch (RuntimeException $e) {
417 throw $e; 427 throw $e;
418 } catch (\Exception $error) { 428 } catch (\Exception $error) {
419 } 429 }
420 } 430 }
436 self::$shell = false; 446 self::$shell = false;
437 447
438 if (file_exists('/usr/bin/env')) { 448 if (file_exists('/usr/bin/env')) {
439 // handle other OSs with bash/zsh/ksh/csh if available to hide the answer 449 // handle other OSs with bash/zsh/ksh/csh if available to hide the answer
440 $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null"; 450 $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
441 foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) { 451 foreach (['bash', 'zsh', 'ksh', 'csh'] as $sh) {
442 if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) { 452 if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
443 self::$shell = $sh; 453 self::$shell = $sh;
444 break; 454 break;
445 } 455 }
446 } 456 }