/** * Outputs the question prompt. * * Backport from Symfony 2.7 * * @param OutputInterface $output * @param Question $question * * @since 1.1.0 2015-05-22 */ protected function backportWritePrompt(OutputInterface $output, Question $question) { $message = $question->getQuestion(); if ($question instanceof ChoiceQuestion) { $width = max(array_map('strlen', array_keys($question->getChoices()))); $messages = (array) $question->getQuestion(); foreach ($question->getChoices() as $key => $value) { $messages[] = sprintf(" [<info>%-{$width}s</info>] %s", $key, $value); } $output->writeln($messages); $message = $question->getPrompt(); } $output->write($message); }
protected function writePrompt(OutputInterface $output, Question $question) { $message = $question->getQuestion(); if ($question instanceof ChoiceQuestion) { $maxWidth = max(array_map(array($this, 'strlen'), array_keys($question->getChoices()))); $messages = (array) $question->getQuestion(); foreach ($question->getChoices() as $key => $value) { $width = $maxWidth - $this->strlen($key); $messages[] = ' [<info>' . $key . str_repeat(' ', $width) . '</info>] ' . $value; } $output->writeln($messages); $message = $question->getPrompt(); } $output->write($message); }
/** * {@inheritdoc} */ protected function writePrompt(OutputInterface $output, Question $question) { $text = $question->getQuestion(); $default = $question->getDefault(); switch (true) { case null === $default: $text = sprintf(' <info>%s</info>:', $text); break; case $question instanceof ConfirmationQuestion: $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no'); break; case $question instanceof ChoiceQuestion && $question->isMultiSelect(): $choices = $question->getChoices(); $default = explode(',', $default); foreach ($default as $key => $value) { $default[$key] = $choices[trim($value)]; } $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default)); break; case $question instanceof ChoiceQuestion: $choices = $question->getChoices(); $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]); break; default: $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default); } $output->writeln($text); if ($question instanceof ChoiceQuestion) { $width = max(array_map('strlen', array_keys($question->getChoices()))); foreach ($question->getChoices() as $key => $value) { $output->writeln(sprintf(" [<comment>%-{$width}s</comment>] %s", $key, $value)); } } $output->write(' > '); }
/** * {@inheritdoc} */ protected function writePrompt(OutputInterface $output, Question $question) { $text = $question->getQuestion(); $default = $question->getDefault(); $choices = $question->getChoices(); $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]); $output->writeln($text); $output->write(' > '); }
/** * Asks the question to the user. * * This method is public for PHP 5.3 compatibility, it should be private. * * @param OutputInterface $output * @param Question $question * * @return bool|mixed|null|string * * @throws \Exception * @throws \RuntimeException */ public function doAsk(OutputInterface $output, Question $question) { $inputStream = $this->inputStream ?: STDIN; $message = $question->getQuestion(); if ($question instanceof ChoiceQuestion) { $width = max(array_map('strlen', array_keys($question->getChoices()))); $messages = (array) $question->getQuestion(); foreach ($question->getChoices() as $key => $value) { $messages[] = sprintf(" [<info>%-{$width}s</info>] %s", $key, $value); } $output->writeln($messages); $message = $question->getPrompt(); } $output->write($message); $autocomplete = $question->getAutocompleterValues(); if (null === $autocomplete || !$this->hasSttyAvailable()) { $ret = false; if ($question->isHidden()) { try { $ret = trim($this->getHiddenResponse($output, $inputStream)); } catch (\RuntimeException $e) { if (!$question->isHiddenFallback()) { throw $e; } } } if (false === $ret) { $ret = fgets($inputStream, 4096); if (false === $ret) { throw new \RuntimeException('Aborted'); } $ret = trim($ret); } } else { $ret = trim($this->autocomplete($output, $question, $inputStream)); } $ret = strlen($ret) > 0 ? $ret : $question->getDefault(); if ($normalizer = $question->getNormalizer()) { return $normalizer($ret); } return $ret; }
/** * Returns the formatted question by passing the message to the formatter along with the default * * @return string */ public function getQuestion() : string { return $this->getFormatter()->format(parent::getQuestion(), $this->getDefault()); }