8000 [RFC] add input to the arguments of dialog helper by cordoval · Pull Request #8366 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add input to the arguments of dialog helper
  • Loading branch information
cordoval committed Jun 27, 2013
commit 0bcc61ce66cba413728f76736cd4fcc8dccc9b68
36 changes: 23 additions & 13 deletions src/Symfony/Component/Console/Helper/DialogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console\Helper;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

Expand All @@ -28,6 +29,7 @@ class DialogHelper extends Helper
/**
* Asks the user to select a value.
*
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param array $choices List of choices to pick from
Expand All @@ -40,7 +42,7 @@ class DialogHelper extends Helper
*
* @throws \InvalidArgumentException
*/
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
public function select(InputInterface $input, OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
{
$width = max(array_map('strlen', array_keys($choices)));

Expand All @@ -51,7 +53,7 @@ public function select(OutputInterface $output, $question, $choices, $default =

$output->writeln($messages);

$result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) {
$result = $this->askAndValidate($input, $output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) {
// Collapse all spaces.
$selectedChoices = str_replace(" ", "", $picked);

Expand Down Expand Up @@ -87,6 +89,7 @@ public function select(OutputInterface $output, $question, $choices, $default =
/**
* Asks a question to the user.
*
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param string $default The default answer if none is given by the user
Expand All @@ -96,10 +99,14 @@ public function select(OutputInterface $output, $question, $choices, $default =
*
* @throws \RuntimeException If there is no data to read in the input stream
*/
public function ask(OutputInterface $output, $question, $default = null, array $autocomplete = null)
public function ask(InputInterface $input, OutputInterface $output, $question, $default = null, array $autocomplete = null)
{
$output->write($question);

if (!$input->isInteractive()) {
return $default;
}

$inputStream = $this->inputStream ?: STDIN;

if (null === $autocomplete || !$this->hasSttyAvailable()) {
Expand Down Expand Up @@ -221,17 +228,18 @@ public function ask(OutputInterface $output, $question, $default = null, array $
*
* The question will be asked until the user answers by nothing, yes, or no.
*
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param Boolean $default The default answer if the user enters nothing
*
* @return Boolean true if the user has confirmed, false otherwise
*/
public function askConfirmation(OutputInterface $output, $question, $default = true)
public function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default = true)
{
$answer = 'z';
while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
$answer = $this->ask($output, $question);
$answer = $this->ask($input, $output, $question);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely to be an infinite loop in non-interactive mode, as $this->ask() will return true, not the expected string being in array('y', 'n')

}

if (false === $default) {
Expand All @@ -252,7 +260,7 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
*
* @throws \RuntimeException In case the fallback is deactivated and the response can not be hidden
*/
public function askHiddenResponse(OutputInterface $output, $question, $fallback = true)
public function askHiddenResponse(InputInterface $input, OutputInterface $output, $question, $fallback = true)
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
Expand Down Expand Up @@ -305,7 +313,7 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback
}

if ($fallback) {
return $this->ask($output, $question);
return $this->ask($input, $output, $question);
}

throw new \RuntimeException('Unable to hide the response');
Expand All @@ -318,6 +326,7 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback
* validated data when the data is valid and throw an exception
* otherwise.
*
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param callable $validator A PHP callback
Expand All @@ -329,12 +338,12 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback
*
* @throws \Exception When any of the validators return an error
*/
public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null, array $autocomplete = null)
public function askAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = false, $default = null, array $autocomplete = null)
{
$that = $this;

$interviewer = function() use ($output, $question, $default, $autocomplete, $that) {
return $that->ask($output, $question, $default, $autocomplete);
$interviewer = function() use ($input, $output, $question, $default, $autocomplete, $that) {
return $that->ask($input, $output, $question, $default, $autocomplete);
};

return $this->validateAttempts($interviewer, $output, $validator, $attempts);
Expand All @@ -347,6 +356,7 @@ public function askAndValidate(OutputInterface $output, $question, $validator, $
* validated data when the data is valid and throw an exception
* otherwise.
*
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
* @param string|array $question The question to ask
* @param callable $validator A PHP callback
Expand All @@ -359,12 +369,12 @@ public function askAndValidate(OutputInterface $output, $question, $validator, $
* @throws \RuntimeException In case the fallback is deactivated and the response can not be hidden
*
*/
public function askHiddenResponseAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $fallback = true)
public function askHiddenResponseAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = false, $fallback = true)
{
$that = $this;

$interviewer = function() use ($output, $question, $fallback, $that) {
return $that->askHiddenResponse($output, $question, $fallback);
$interviewer = function() use ($input, $output, $question, $fallback, $that) {
return $that->askHiddenResponse($input, $output, $question, $fallback);
};

return $this->validateAttempts($interviewer, $output, $validator, $attempts);
Expand Down
0