| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\ReadlineException; |
| 6 |
|
| 7 |
/** |
| 8 |
* This function adds a line to the command line history. |
| 9 |
* |
| 10 |
* @param string $line The line to be added in the history. |
| 11 |
* @throws ReadlineException |
| 12 |
* |
| 13 |
*/ |
| 14 |
function readline_add_history(string $line): void |
| 15 |
{ |
| 16 |
error_clear_last(); |
| 17 |
$result = \readline_add_history($line); |
| 18 |
if ($result === false) { |
| 19 |
throw ReadlineException::createFromPhpError(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Sets up a readline callback interface then prints |
| 26 |
* prompt and immediately returns. |
| 27 |
* Calling this function twice without removing the previous |
| 28 |
* callback interface will automatically and conveniently overwrite the old |
| 29 |
* interface. |
| 30 |
* |
| 31 |
* The callback feature is useful when combined with |
| 32 |
* stream_select as it allows interleaving of IO and |
| 33 |
* user input, unlike readline. |
| 34 |
* |
| 35 |
* |
| 36 |
* Readline Callback Interface Example |
| 37 |
* |
| 38 |
* 10) { |
| 39 |
* $prompting = false; |
| 40 |
* readline_callback_handler_remove(); |
| 41 |
* } else { |
| 42 |
* readline_callback_handler_install("[$c] Enter something: ", 'rl_callback'); |
| 43 |
* } |
| 44 |
* } |
| 45 |
* |
| 46 |
* $c = 1; |
| 47 |
* $prompting = true; |
| 48 |
* |
| 49 |
* readline_callback_handler_install("[$c] Enter something: ", 'rl_callback'); |
| 50 |
* |
| 51 |
* while ($prompting) { |
| 52 |
* $w = NULL; |
| 53 |
* $e = NULL; |
| 54 |
* $n = stream_select($r = array(STDIN), $w, $e, null); |
| 55 |
* if ($n && in_array(STDIN, $r)) { |
| 56 |
* // read a character, will call the callback when a newline is entered |
| 57 |
* readline_callback_read_char(); |
| 58 |
* } |
| 59 |
* } |
| 60 |
* |
| 61 |
* echo "Prompting disabled. All done.\n"; |
| 62 |
* ?> |
| 63 |
* ]]> |
| 64 |
* |
| 65 |
* |
| 66 |
* |
| 67 |
* @param string $prompt The prompt message. |
| 68 |
* @param callable $callback The callback function takes one parameter; the |
| 69 |
* user input returned. |
| 70 |
* @throws ReadlineException |
| 71 |
* |
| 72 |
*/ |
| 73 |
function readline_callback_handler_install(string $prompt, callable $callback): void |
| 74 |
{ |
| 75 |
error_clear_last(); |
| 76 |
$result = \readline_callback_handler_install($prompt, $callback); |
| 77 |
if ($result === false) { |
| 78 |
throw ReadlineException::createFromPhpError(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* This function clears the entire command line history. |
| 85 |
* |
| 86 |
* @throws ReadlineException |
| 87 |
* |
| 88 |
*/ |
| 89 |
function readline_clear_history(): void |
| 90 |
{ |
| 91 |
error_clear_last(); |
| 92 |
$result = \readline_clear_history(); |
| 93 |
if ($result === false) { |
| 94 |
throw ReadlineException::createFromPhpError(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* This function registers a completion function. This is the same kind of |
| 101 |
* functionality you'd get if you hit your tab key while using Bash. |
| 102 |
* |
| 103 |
* @param callable $function You must supply the name of an existing function which accepts a |
| 104 |
* partial command line and returns an array of possible matches. |
| 105 |
* @throws ReadlineException |
| 106 |
* |
| 107 |
*/ |
| 108 |
function readline_completion_function(callable $function): void |
| 109 |
{ |
| 110 |
error_clear_last(); |
| 111 |
$result = \readline_completion_function($function); |
| 112 |
if ($result === false) { |
| 113 |
throw ReadlineException::createFromPhpError(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* This function reads a command history from a file. |
| 120 |
* |
| 121 |
* @param string|null $filename Path to the filename containing the command history. |
| 122 |
* |
| 123 |
* @throws ReadlineException |
| 124 |
* |
| 125 |
*/ |
| 126 |
function readline_read_history(?string $filename = null): void |
| 127 |
{ |
| 128 |
error_clear_last(); |
| 129 |
if ($filename !== null) { |
| 130 |
$result = \readline_read_history($filename); |
| 131 |
} else { |
| 132 |
$result = \readline_read_history(); |
| 133 |
} |
| 134 |
if ($result === false) { |
| 135 |
throw ReadlineException::createFromPhpError(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* This function writes the command history to a file. |
| 142 |
* |
| 143 |
* @param string|null $filename Path to the saved file. |
| 144 |
* |
| 145 |
* @throws ReadlineException |
| 146 |
* |
| 147 |
*/ |
| 148 |
function readline_write_history(?string $filename = null): void |
| 149 |
{ |
| 150 |
error_clear_last(); |
| 151 |
if ($filename !== null) { |
| 152 |
$result = \readline_write_history($filename); |
| 153 |
} else { |
| 154 |
$result = \readline_write_history(); |
| 155 |
} |
| 156 |
if ($result === false) { |
| 157 |
throw ReadlineException::createFromPhpError(); |
| 158 |
} |
| 159 |
} |
| 160 |
|