Last active
November 8, 2015 14:23
-
-
Save arleighdickerson/1a4c144368a945b94a6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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