summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Helper/QuestionHelper.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/console/Helper/QuestionHelper.php')
-rw-r--r--vendor/symfony/console/Helper/QuestionHelper.php84
1 files changed, 48 insertions, 36 deletions
diff --git a/vendor/symfony/console/Helper/QuestionHelper.php b/vendor/symfony/console/Helper/QuestionHelper.php
index 5bb30df8..f0f37a09 100644
--- a/vendor/symfony/console/Helper/QuestionHelper.php
+++ b/vendor/symfony/console/Helper/QuestionHelper.php
@@ -13,10 +13,12 @@ namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
+use Symfony\Component\Console\Formatter\OutputFormatter;
+use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ChoiceQuestion;
@@ -34,11 +36,7 @@ class QuestionHelper extends Helper
/**
* Asks a question to the user.
*
- * @param InputInterface $input An InputInterface instance
- * @param OutputInterface $output An OutputInterface instance
- * @param Question $question The question to ask
- *
- * @return string The user answer
+ * @return mixed The user answer
*
* @throws RuntimeException If there is no data to read in the input stream
*/
@@ -52,14 +50,16 @@ class QuestionHelper extends Helper
return $question->getDefault();
}
+ if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) {
+ $this->inputStream = $stream;
+ }
+
if (!$question->getValidator()) {
return $this->doAsk($output, $question);
}
- $that = $this;
-
- $interviewer = function () use ($output, $question, $that) {
- return $that->doAsk($output, $question);
+ $interviewer = function () use ($output, $question) {
+ return $this->doAsk($output, $question);
};
return $this->validateAttempts($interviewer, $output, $question);
@@ -70,12 +70,17 @@ class QuestionHelper extends Helper
*
* This is mainly useful for testing purpose.
*
+ * @deprecated since version 3.2, to be removed in 4.0. Use
+ * StreamableInputInterface::setStream() instead.
+ *
* @param resource $stream The input stream
*
* @throws InvalidArgumentException In case the stream is not a resource
*/
public function setInputStream($stream)
{
+ @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);
+
if (!is_resource($stream)) {
throw new InvalidArgumentException('Input stream must be a valid resource.');
}
@@ -86,10 +91,17 @@ class QuestionHelper extends Helper
/**
* Returns the helper's input stream.
*
+ * @deprecated since version 3.2, to be removed in 4.0. Use
+ * StreamableInputInterface::getStream() instead.
+ *
* @return resource
*/
public function getInputStream()
{
+ if (0 === func_num_args() || func_get_arg(0)) {
+ @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);
+ }
+
return $this->inputStream;
}
@@ -102,19 +114,21 @@ class QuestionHelper extends Helper
}
/**
+ * Prevents usage of stty.
+ */
+ public static function disableStty()
+ {
+ self::$stty = false;
+ }
+
+ /**
* 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
+ * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden
*/
- public function doAsk(OutputInterface $output, Question $question)
+ private function doAsk(OutputInterface $output, Question $question)
{
$this->writePrompt($output, $question);
@@ -126,7 +140,7 @@ class QuestionHelper extends Helper
if ($question->isHidden()) {
try {
$ret = trim($this->getHiddenResponse($output, $inputStream));
- } catch (\RuntimeException $e) {
+ } catch (RuntimeException $e) {
if (!$question->isHiddenFallback()) {
throw $e;
}
@@ -136,12 +150,12 @@ class QuestionHelper extends Helper
if (false === $ret) {
$ret = fgets($inputStream, 4096);
if (false === $ret) {
- throw new \RuntimeException('Aborted');
+ throw new RuntimeException('Aborted');
}
$ret = trim($ret);
}
} else {
- $ret = trim($this->autocomplete($output, $question, $inputStream));
+ $ret = trim($this->autocomplete($output, $question, $inputStream, is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
}
$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
@@ -155,9 +169,6 @@ class QuestionHelper extends Helper
/**
* Outputs the question prompt.
- *
- * @param OutputInterface $output
- * @param Question $question
*/
protected function writePrompt(OutputInterface $output, Question $question)
{
@@ -182,9 +193,6 @@ class QuestionHelper extends Helper
/**
* Outputs an error message.
- *
- * @param OutputInterface $output
- * @param \Exception $error
*/
protected function writeError(OutputInterface $output, \Exception $error)
{
@@ -202,12 +210,13 @@ class QuestionHelper extends Helper
*
* @param OutputInterface $output
* @param Question $question
+ * @param resource $inputStream
+ * @param array $autocomplete
*
* @return string
*/
- private function autocomplete(OutputInterface $output, Question $question, $inputStream)
+ private function autocomplete(OutputInterface $output, Question $question, $inputStream, array $autocomplete)
{
- $autocomplete = $question->getAutocompleterValues();
$ret = '';
$i = 0;
@@ -235,7 +244,7 @@ class QuestionHelper extends Helper
$output->write("\033[1D");
}
- if ($i === 0) {
+ if (0 === $i) {
$ofs = -1;
$matches = $autocomplete;
$numMatches = count($matches);
@@ -303,7 +312,7 @@ class QuestionHelper extends Helper
// Save cursor position
$output->write("\0337");
// Write highlighted text
- $output->write('<hl>'.substr($matches[$ofs], $i).'</hl>');
+ $output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $i)).'</hl>');
// Restore cursor position
$output->write("\0338");
}
@@ -318,7 +327,8 @@ class QuestionHelper extends Helper
/**
* Gets a hidden response from user.
*
- * @param OutputInterface $output An Output instance
+ * @param OutputInterface $output An Output instance
+ * @param resource $inputStream The handler resource
*
* @return string The answer
*
@@ -364,7 +374,7 @@ class QuestionHelper extends Helper
}
if (false !== $shell = $this->getShell()) {
- $readCmd = $shell === 'csh' ? 'set mypassword = $<' : 'read -r mypassword';
+ $readCmd = 'csh' === $shell ? 'set mypassword = $<' : 'read -r mypassword';
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
$value = rtrim(shell_exec($command));
$output->writeln('');
@@ -382,11 +392,11 @@ class QuestionHelper extends Helper
* @param OutputInterface $output An Output instance
* @param Question $question A Question instance
*
- * @return string The validated response
+ * @return mixed The validated response
*
* @throws \Exception In case the max number of attempts has been reached and no valid response has been given
*/
- private function validateAttempts($interviewer, OutputInterface $output, Question $question)
+ private function validateAttempts(callable $interviewer, OutputInterface $output, Question $question)
{
$error = null;
$attempts = $question->getMaxAttempts();
@@ -397,6 +407,8 @@ class QuestionHelper extends Helper
try {
return call_user_func($question->getValidator(), $interviewer());
+ } catch (RuntimeException $e) {
+ throw $e;
} catch (\Exception $error) {
}
}
@@ -444,6 +456,6 @@ class QuestionHelper extends Helper
exec('stty 2>&1', $output, $exitcode);
- return self::$stty = $exitcode === 0;
+ return self::$stty = 0 === $exitcode;
}
}