Last active
August 16, 2022 15:16
-
-
Save ziadoz/4506844 to your computer and use it in GitHub Desktop.
Simple PHP REPL
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
#!/usr/bin/env php | |
<?php | |
if (php_sapi_name() !== 'cli') { | |
exit(1); | |
} | |
function input() { | |
return fgets(STDIN); | |
} | |
function output($val) { | |
fputs(STDOUT, $val); | |
} | |
function colour($text = '', $colour = null) { | |
$styles = array( | |
'green' => "\033[0;32m%s\033[0m", | |
'red' => "\033[31;31m%s\033[0m", | |
'yellow' => "\033[33;33m%s\033[0m", | |
'blue' => "\033[33;34m%s\033[0m", | |
); | |
return sprintf(isset($styles[$colour]) ? $styles[$colour] : "%s", $text); | |
} | |
function cmd($cmd, $input) { | |
$input = trim($input); | |
return substr($input, 0, strlen($cmd)) === $cmd; | |
} | |
function snip($cmd, $input) { | |
return substr($input, strlen($cmd)); | |
} | |
function asi($input) { | |
if (trim($input) === '') { | |
return $input; | |
} | |
return rtrim($input, "\n;") . ';'; | |
} | |
output('PHP Version ' . colour(PHP_VERSION, 'green') . PHP_EOL); | |
output('quit: Exit the REPL' . PHP_EOL); | |
output('dump: Perform a vardump()' . PHP_EOL); | |
output('printr: Perform a print_r()' . PHP_EOL); | |
output('exec: Execute an external program' . PHP_EOL); | |
output('>>>: Start a heredoc of PHP code' . PHP_EOL); | |
output('<<<: End a heredoc of PHP code' . PHP_EOL); | |
output('!!!: Discard a heredoc of PHP code' . PHP_EOL); | |
output(PHP_EOL); | |
$buffer = ''; | |
$buffering = false; | |
while (true) { | |
output(colour('> ', $buffering ? 'blue' : 'yellow')); | |
$input = input(); | |
if (cmd('quit', $input)) { | |
exit; | |
} | |
if (cmd('exec', $input)) { | |
exec(snip('exec', $input), $result); | |
$result = trim(implode("\n", $result)); | |
if (! empty($result)) { | |
output($result . PHP_EOL); | |
} | |
continue; | |
} | |
if (cmd('dump', $input)) { | |
$input = 'var_dump(' . snip('dump', $input) . ');'; | |
} elseif (cmd('printr', $input)) { | |
$input = 'print_r(' . snip('printr', $input) . ');'; | |
} | |
if (cmd('>>>', $input)) { | |
$buffering = true; | |
$input = snip('>>>', $input); | |
} elseif (cmd('<<<', $input)) { | |
if (! $buffering) { | |
continue; | |
} | |
$buffering = false; | |
$input = snip('<<<', $input); | |
} elseif (cmd('!!!', $input)) { | |
$buffering = false; | |
$buffer = ''; | |
continue; | |
} | |
if ($buffering) { | |
$buffer .= $input; | |
continue; | |
} | |
if (! empty($buffer)) { | |
$input = $buffer; | |
$buffer = ''; | |
} else { | |
$input = asi($input) . PHP_EOL; | |
} | |
eval($input); | |
output(PHP_EOL); | |
} |
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 repl.php | |
> $i = 0 | |
> $i++ | |
> echo $i | |
1 | |
> quit | |
php repl.php | |
> >>> | |
> class Foo extends StdClass | |
> { | |
> // Your code goes here. | |
> // Spans mulitple lines. | |
> } | |
> <<< | |
> $f = new Foo | |
> $f->first = 'Joe' | |
> $f->last = 'Bloggs' | |
> dump $f | |
> quite |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment