Just wondering, is it possible to create an interactive shell, using PHP alone. I mean something like you have with databases, python, etc.
If it is, how?
Here's an expanded take on this. I've added an isCLI()
check in case you're run your script both in CLI and on a web server. Otherwise the server could loop using my function. This solution will prompt the user, check the input, and re-prompt the user for fixed input if necessary. I rtrim()
the input because if the user uses return to submit their entry, that may be appended to the entry. Validation is not necessary, just don't pass a function in that case.
function isCLI() {
return (php_sapi_name() === 'cli' OR defined('STDIN'));
}
function userPrompt($message, $validator=null) {
if (!isCLI()) return null;
print($message);
$handle = fopen ('php://stdin','r');
$line = rtrim(fgets($handle), "\r\n");
if (is_callable($validator) && !call_user_func($validator, $line)) {
print("Invalid Entry.\r\n");
return userPrompt($message, $validator);
} else {
print("Continuing...\r\n");
return $line;
}
}
// Example =====================
function validateSetLangCode($str) {
return preg_match("/^[A-Z0-9]{3}-[A-Z]{2}$/", $str);
}
$code = userPrompt("Please enter the set / language codes. Use the format 'SET-EN', where SET is the three-letter set code and EN is the two-letter lang code. \r\n", 'validateSetLangCode') ?: 'SET-EN';
var_dump($code);
I know the questioner didn't want the second option, but for those that wanted the second option as I did, in addition to phpsh
, PHP also has it's own shell:
Just run php -a
.
It is not entirely clear from the question whether you want to CREATE the shell using PHP alone, or whether you want the interactive shell to process PHP commands. I'm going to assume the latter, and in that case one example is phpsh which was apparently created at Facebook, but is written in python.
Check out:
https://github.com/shaneharter/sheldon
It's pretty easy to get started. It includes Symfony2 and Zend Framework libraries that do a lot of the basic console I/O work and gives you a higher-level abstraction built around Command objects (with regex routes) and Contexts (which hold immutable state).
One of the things I love is that "out of the box" your application can run as either an interactive shell, or as a standard script that you can run from the command line, specify a command, pass any arguments, and when the command is finished the application exits.
The way I understand your question you just want the PHP interpreter to run on the command line so you that it will evaluate any PHP code that you type. I use that feature of Python all the time to test code snippets. In which case I believe the answer you are looking for is to execute (from the command line):
php -a
Assuming PHP is in your path this will drop you in to the PHP interpreter, like it does on mine:
$ php -a
Interactive shell
php >
From there you can start to evaluate arbitrary PHP expressions and see the results:
php > $a = 'abcdef';
php > echo strlen($a);
6
Since this question has been asked and answered, a better solution has been added to PHP. In all recent PHP versions you can easily get user input as so:
$input = fgets(STDIN);