Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Last active November 8, 2015 14:23
Show Gist options
  • Save arleighdickerson/1a4c144368a945b94a6d to your computer and use it in GitHub Desktop.
Save arleighdickerson/1a4c144368a945b94a6d to your computer and use it in GitHub Desktop.
<?php
/**
* A trait to add silent prompts to Yii2 Console Controller
* Requires bash shell
*/
trait SilentPromptingTrait {
/**
* @see http://www.dzone.com/snippets/password-prompt-php
* Interactively prompts for input without echoing to the terminal.
* Requires a bash shell and won't work with safe_mode settings (Uses `shell_exec`)
* @return string the user input
*/
public function promptSilent($prompt) {
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK') {
trigger_error("Can't invoke bash");
return;
}
$command = "/usr/bin/env bash -c 'read -s -p \"" .
addslashes($prompt) .
"\" value && echo \$value'";
$value = rtrim(shell_exec($command));
echo "\n";
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment