|
|
@@ -31,6 +31,16 @@ class ConsoleInput {
|
|
|
* @var resource
|
|
|
*/
|
|
|
protected $_input;
|
|
|
+/**
|
|
|
+ * Can this instance use readline?
|
|
|
+ * Two conditions must be met:
|
|
|
+ * 1. Readline support must be enabled.
|
|
|
+ * 2. Handle we are attached to must be stdin.
|
|
|
+ * Allows rich editing with arrow keys and history when inputting a string.
|
|
|
+ *
|
|
|
+ * @var bool
|
|
|
+ */
|
|
|
+ private $_can_readline;
|
|
|
|
|
|
/**
|
|
|
* Constructor
|
|
|
@@ -38,6 +48,7 @@ class ConsoleInput {
|
|
|
* @param string $handle The location of the stream to use as input.
|
|
|
*/
|
|
|
public function __construct($handle = 'php://stdin') {
|
|
|
+ $this->_can_readline = extension_loaded('readline') && $handle == 'php://stdin' ? true : false;
|
|
|
$this->_input = fopen($handle, 'r');
|
|
|
}
|
|
|
|
|
|
@@ -47,6 +58,13 @@ class ConsoleInput {
|
|
|
* @return mixed The value of the stream
|
|
|
*/
|
|
|
public function read() {
|
|
|
+ if ($this->_can_readline) {
|
|
|
+ $line = readline('');
|
|
|
+ if (!empty($line)) {
|
|
|
+ readline_add_history($line);
|
|
|
+ }
|
|
|
+ return $line;
|
|
|
+ }
|
|
|
return fgets($this->_input);
|
|
|
}
|
|
|
|