[Console] Add DialogHelper::askHiddenResponse method #5731

Merged
merged 6 commits into from Oct 16, 2012

Projects

None yet

5 participants

@romainneutron
Symfony member

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT

It adds a method to DialogHelper to ask a question and hide the response. It's pretty cool when working with passwords.

This code is more than largely inspired by Composer, see ConsoleIO.php at line 140

You will notice that this PR embeds a Windows Executable binary for windows support. This windows binary is provided by @Seldaek (see https://github.com/Seldaek/hidden-input)
This dependency is not yet available via composer.

If this is a problem to embed this file, we can think of other way to provide this support (make a package from HiddenInput and add composer recommandation for example).

@pborreli pborreli commented on an outdated diff Oct 11, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
+ if ('phar:' === substr(__FILE__, 0, 5)) {
+ $tmpExe = sys_get_temp_dir() . '/hiddeninput.exe';
+ copy($exe, $tmpExe);
+ $exe = $tmpExe;
+ }
+
+ $output->write($question);
+ $value = rtrim(shell_exec($exe));
+ $output->writeln('');
+
+ if (isset($tmpExe)) {
+ unlink($tmpExe);
+ }
+
+ return $value;
+ } elseif ($this->hasSttyAvailable()) {
@pborreli
pborreli Oct 11, 2012

you don't need the elseif, just if

@pborreli pborreli commented on an outdated diff Oct 11, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
+
+ $sttyMode = shell_exec('/usr/bin/env stty -g');
+
+ shell_exec('/usr/bin/env stty -echo');
+ $value = fgets($this->inputStream ?: STDIN, 4096);
+ shell_exec(sprintf('/usr/bin/env stty %s', escapeshellarg($sttyMode)));
+
+ if (false === $value) {
+ throw new \RuntimeException('Aborted');
+ }
+
+ $value = trim($value);
+ $output->writeln('');
+
+ return $value;
+ } elseif (false !== $shell = $this->getShell()) {
@pborreli
pborreli Oct 11, 2012

you don't need the elseif, just if

@pborreli pborreli commented on an outdated diff Oct 11, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
+ }
+
+ $value = trim($value);
+ $output->writeln('');
+
+ return $value;
+ } elseif (false !== $shell = $this->getShell()) {
+
+ $output->write($question);
+ $readCmd = $shell === 'csh' ? 'set mypassword = $<' : 'read mypassword';
+ $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
+ $value = rtrim(shell_exec($command));
+ $output->writeln('');
+
+ return $value;
+ } elseif ($fallback) {
@stof stof commented on an outdated diff Oct 11, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
@@ -72,6 +74,78 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
}
/**
+ * Ask a question to the user, the response is hidden
+ *
+ * @param OutputInterface $output An Output instance
+ * @param string|array $question The question
+ * @param Boolean $fallback In case the response can not be hidden, whether to fallback on non-hidden question or not
+ *
+ * @return string The answer
+ *
+ * @throws \RuntimeException In case the fallback is disactivated and the response can not be hidden
+ */
+ public function askHiddenResponse(OutputInterface $output, $question, $fallback = true)
+ {
+ if (defined('PHP_WINDOWS_VERSION_BUILD')) {
+ $exe = __DIR__ . '\\hiddeninput.exe';
@stof
stof Oct 11, 2012

use a / in the path, to be consistent with all other places in Symfony. As of PHP 5.3, / works in paths on windows too.

@stof
Symfony member

The link to the hiddeninput source code should be added in the readme.
And you should also update the changelog.

Btw, adding composer for hiddeninput does not make sense. Compsoer is about installing PHP code, not about downloading the source of a C++ program.

@romainneutron
Symfony member
@romainneutron
Symfony member

Changelog updated, Readme note added, CS fixed

@stof stof commented on the diff Oct 13, 2012
...y/Component/Console/Tests/Helper/DialogHelperTest.php
@@ -31,6 +31,15 @@ public function testAsk()
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
}
+ public function testAskHiddenResponse()
+ {
+ $dialog = new DialogHelper();
+
+ $dialog->setInputStream($this->getInputStream("8AM\n"));
+
+ $this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
+ }
@stof
stof Oct 13, 2012

This test should be skipped on Windows as it would trigger the binary, which will not work.

@stof
Symfony member

the missing point is now the PR to the doc for this new feature

@Seldaek Seldaek commented on an outdated diff Oct 15, 2012
src/Symfony/Component/Console/README.md
You can run the unit tests with the following command:
phpunit
+Third Party
+-----------
+
+`HiddenInput.exe` third party binary is provided within this component. Find
@Seldaek
Seldaek Oct 15, 2012

The file is called hiddeninput.exe all lowercased

@romainneutron
Symfony member

@stof documentation added

@fabpot fabpot commented on an outdated diff Oct 16, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
@@ -72,6 +74,78 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
}
/**
+ * Ask a question to the user, the response is hidden
+ *
+ * @param OutputInterface $output An Output instance
+ * @param string|array $question The question
+ * @param Boolean $fallback In case the response can not be hidden, whether to fallback on non-hidden question or not
+ *
+ * @return string The answer
+ *
+ * @throws \RuntimeException In case the fallback is disactivated and the response can not be hidden
@fabpot
fabpot Oct 16, 2012

disactivated should be deactivated

@fabpot fabpot commented on an outdated diff Oct 16, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
@@ -72,6 +74,78 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
}
/**
+ * Ask a question to the user, the response is hidden
@fabpot
fabpot Oct 16, 2012

Ask should be Asks and there is a missing dot at the end of the line.

@fabpot fabpot commented on the diff Oct 16, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
+ $exe = $tmpExe;
+ }
+
+ $output->write($question);
+ $value = rtrim(shell_exec($exe));
+ $output->writeln('');
+
+ if (isset($tmpExe)) {
+ unlink($tmpExe);
+ }
+
+ return $value;
+ }
+
+ if ($this->hasSttyAvailable()) {
+
@fabpot
fabpot Oct 16, 2012

this blank line should be removed.

@fabpot fabpot commented on the diff Oct 16, 2012
src/Symfony/Component/Console/Helper/DialogHelper.php
+ shell_exec('/usr/bin/env stty -echo');
+ $value = fgets($this->inputStream ?: STDIN, 4096);
+ shell_exec(sprintf('/usr/bin/env stty %s', $sttyMode));
+
+ if (false === $value) {
+ throw new \RuntimeException('Aborted');
+ }
+
+ $value = trim($value);
+ $output->writeln('');
+
+ return $value;
+ }
+
+ if (false !== $shell = $this->getShell()) {
+
@fabpot
fabpot Oct 16, 2012

this blank line should be removed.

@fabpot fabpot commented on an outdated diff Oct 16, 2012
src/Symfony/Component/Console/README.md
You can run the unit tests with the following command:
phpunit
+Third Party
+-----------
+
+`hiddeninput.exe` third party binary is provided within this component. Find
@fabpot
fabpot Oct 16, 2012

I would have stored the executable under a Resources/bin directory like done in some other components.

@romainneutron
Symfony member

@fabpot what you asked is now fixed

@fabpot fabpot added a commit that referenced this pull request Oct 16, 2012
@fabpot fabpot merged branch romainneutron/ConsoleHiddenQuestion (PR #5731)
This PR was merged into the master branch.

Commits
-------

aefa495 Move `hiddeninput.exe` to Resources/bin
c0f8a63 Fix CS and typos
26c35e0 Skip askHiddenResponse test on windows
e2eaf5a Update Changelog, add Readme note about hidden input third party
ac01d5d Fix tests and CS
e396edb [Console] Add DialogHelper::askHiddenResponse method

Discussion
----------

[Console] Add DialogHelper::askHiddenResponse method

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT

It adds a method to `DialogHelper` to ask a question and hide the response. It's pretty cool when working with passwords.

This code is more than largely inspired by Composer, see [ConsoleIO.php at line 140](https://github.com/composer/composer/blob/master/src/Composer/IO/ConsoleIO.php#L140)

 You will notice that this PR embeds a Windows Executable binary for windows support. This windows binary is provided by @Seldaek (see https://github.com/Seldaek/hidden-input)
This dependency is not yet available via composer.

If this is a problem to embed this file, we can think of other way to provide this support (make a package from HiddenInput and add composer recommandation for example).

---------------------------------------------------------------------------

by stof at 2012-10-11T17:20:11Z

The link to the hiddeninput source code should be added in the readme.
And you should also update the changelog.

Btw, adding composer for hiddeninput does not make sense. Compsoer is about installing PHP code, not about downloading the source of a C++ program.

---------------------------------------------------------------------------

by romainneutron at 2012-10-11T17:22:58Z

This proposition comes from a discussion I had with Jordi , nothing more :)

Romain

On 11 oct. 2012, at 19:20, Christophe Coevoet <notifications@github.com>
wrote:

The link to the hiddeninput source code should be added in the readme.
And you should also update the changelog.

Btw, adding composer for hiddeninput does not make sense. Compsoer is about
installing PHP code, not about downloading the source of a C++ program.

—
Reply to this email directly or view it on
GitHub<#5731 (comment)>.

---------------------------------------------------------------------------

by romainneutron at 2012-10-12T07:33:00Z

Changelog updated, Readme note added, CS fixed

---------------------------------------------------------------------------

by stof at 2012-10-13T22:09:24Z

the missing point is now the PR to the doc for this new feature

---------------------------------------------------------------------------

by romainneutron at 2012-10-16T00:33:59Z

@stof documentation added

---------------------------------------------------------------------------

by romainneutron at 2012-10-16T09:10:35Z

@fabpot what you asked is now fixed
d8f6021
@fabpot fabpot merged commit aefa495 into symfony:master Oct 16, 2012

1 check failed

Details default The Travis build failed
@mmucklo mmucklo pushed a commit that referenced this pull request May 23, 2013
@fabpot fabpot merged branch romainneutron/ConsoleHiddenQuestion (PR #5731)
This PR was merged into the master branch.

Commits
-------

aefa495 Move `hiddeninput.exe` to Resources/bin
c0f8a63 Fix CS and typos
26c35e0 Skip askHiddenResponse test on windows
e2eaf5a Update Changelog, add Readme note about hidden input third party
ac01d5d Fix tests and CS
e396edb [Console] Add DialogHelper::askHiddenResponse method

Discussion
----------

[Console] Add DialogHelper::askHiddenResponse method

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT

It adds a method to `DialogHelper` to ask a question and hide the response. It's pretty cool when working with passwords.

This code is more than largely inspired by Composer, see [ConsoleIO.php at line 140](https://github.com/composer/composer/blob/master/src/Composer/IO/ConsoleIO.php#L140)

 You will notice that this PR embeds a Windows Executable binary for windows support. This windows binary is provided by @Seldaek (see https://github.com/Seldaek/hidden-input)
This dependency is not yet available via composer.

If this is a problem to embed this file, we can think of other way to provide this support (make a package from HiddenInput and add composer recommandation for example).

---------------------------------------------------------------------------

by stof at 2012-10-11T17:20:11Z

The link to the hiddeninput source code should be added in the readme.
And you should also update the changelog.

Btw, adding composer for hiddeninput does not make sense. Compsoer is about installing PHP code, not about downloading the source of a C++ program.

---------------------------------------------------------------------------

by romainneutron at 2012-10-11T17:22:58Z

This proposition comes from a discussion I had with Jordi , nothing more :)

Romain

On 11 oct. 2012, at 19:20, Christophe Coevoet <notifications@github.com>
wrote:

The link to the hiddeninput source code should be added in the readme.
And you should also update the changelog.

Btw, adding composer for hiddeninput does not make sense. Compsoer is about
installing PHP code, not about downloading the source of a C++ program.

—
Reply to this email directly or view it on
GitHub<#5731 (comment)>.

---------------------------------------------------------------------------

by romainneutron at 2012-10-12T07:33:00Z

Changelog updated, Readme note added, CS fixed

---------------------------------------------------------------------------

by stof at 2012-10-13T22:09:24Z

the missing point is now the PR to the doc for this new feature

---------------------------------------------------------------------------

by romainneutron at 2012-10-16T00:33:59Z

@stof documentation added

---------------------------------------------------------------------------

by romainneutron at 2012-10-16T09:10:35Z

@fabpot what you asked is now fixed
7a5355c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment