Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:ieure/php_repl
Browse files Browse the repository at this point in the history
  • Loading branch information
ieure committed May 14, 2009
2 parents 4a6f875 + bc45ea9 commit 95481d1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 54 deletions.
63 changes: 34 additions & 29 deletions PHP/Repl.php
Expand Up @@ -126,15 +126,13 @@ public function __destruct()
*/
public function run()
{
static $__sugar__ = array(',' => 'dissect',
'd' => 'doc',
'l' => 'dir');
ob_start();
while (true) {
ob_flush();
ob_end_clean();

$__run__ = true;
while ($__run__) {
try {
$__code__ = $this->read();
if (($__run__ == (boolean) $__code__) === false) {
if (((boolean) $__code__ = $this->read()) === false) {
break;
}
ob_start(array($this, 'ob_cleanup'));
Expand All @@ -143,24 +141,9 @@ public function run()
ini_set('html_errors', 'Off');
ini_set('display_errors', 'On');

if (substr($__code__, 0, 1) == ',' &&
isset($__sugar__[$m = substr($__code__, 1, 1)])) {
$__code__ = preg_replace('/^,.\s*/', '', $__code__);
if (substr($__code__, 0, 1) != '$') {
$__code__ = "'$__code__'";
}
$__code__ = "\$this->dissect($__code__)";
}

$_ = eval($__exp__ = $this->cleanup($__code__));
ob_flush();
ob_end_clean();
$this->_print($_);
$this->_print($_ = eval($this->cleanup($__code__)));
} catch (Exception $e) {
$_ = $e;
ob_flush();
ob_end_clean();
echo $e . "\n";
echo ($_ = $e) . "\n";
}
}
}
Expand Down Expand Up @@ -223,9 +206,29 @@ private function cleanup($input)
'interface', 'abstract', 'static', 'echo',
'include', 'include_once', 'require',
'require_once');
static $sugar = array(',' => 'dissect',
'd' => 'doc',
'l' => 'dir',
'e' => 'cleanup');
static $last;

$input = trim($input);

// Sugar
if (substr($input, 0, 1) == ',' &&
isset($sugar[$m = substr($input, 1, 1)])) {
$input = preg_replace('/^,.\s*/', '', $input);
if (empty($input)) {
$input = $last;
}

if (substr($input, 0, 1) != '$') {
$input = "'$input'";
}
return $this->cleanup("\$this->{$sugar[$m]}($input)");
}


// Add a trailing semicolon
if (substr($input, -1) != ';') {
$input .= ';';
Expand All @@ -236,7 +239,8 @@ private function cleanup($input)
if (!in_array($first, $implicit)) {
$input = 'return ' . $input;
}
return $input;

return $last = $input;
}

/**
Expand Down Expand Up @@ -333,9 +337,8 @@ protected function getReflection($thing)
*/
protected function dissect($thing)
{
$ref = $this->getReflection($thing);
echo (string) $ref;
return get_class($ref);
echo (string) $ref = $this->getReflection($thing);
return "---";
}

/**
Expand All @@ -354,6 +357,7 @@ protected function dir($thing)
foreach ($rc->getMethods() as $meth) {
echo "\{$meth->getName()}()\n";
}
return "---";
}

/**
Expand All @@ -366,7 +370,8 @@ protected function dir($thing)
protected function doc($thing)
{
echo preg_replace('/^\s*\*/m', ' *',
$this->getReflection($thing)->getDocComment());
$this->getReflection($thing)->getDocComment()) . "\n";
return "---";
}
}

Expand Down
43 changes: 22 additions & 21 deletions README.mkd
Expand Up @@ -45,11 +45,11 @@ The type of display varies depending on the return type. Numeric types and boole
Uncaught Exceptions are caught and dumped:

php> throw new Exception("Test 123", 123)
exception 'Exception' with message 'Test 123' in /Users/ieure/Projects/php_repl/PHP/Repl.php(108) : eval()'d code:1
exception 'Exception' with message 'Test 123' in /Users/ieure/Projects/php_repl/PHP/Repl.php(143) : eval()'d code:1
Stack trace:
#0 /Users/ieure/Projects/php_repl/PHP/Repl.php(108): eval()
#1 /Users/ieure/Projects/php_repl/PHP/Repl.php(53): PHP_Repl->run()
#2 /Users/ieure/Projects/php_repl/PHP/Repl.php(248): PHP_Repl->__construct(Array)
#0 /Users/ieure/Projects/php_repl/PHP/Repl.php(143): eval()
#1 /Users/ieure/Projects/php_repl/PHP/Repl.php(62): PHP_Repl->run()
#2 /Users/ieure/Projects/php_repl/testrepl(20): PHP_Repl->__construct(Array)
#3 {main}
php>

Expand All @@ -67,22 +67,9 @@ If the last character of the entered line is a backslash (`\`), the REPL will ac
)
php>

Certain aspects of the input are altered before the code is evaluated. For example, `return` is prepended for most expressions, a semicolon is added if it was omitted, and so on. Since this may produce unexpected effects, you can examine the expanded code in two ways:
Certain aspects of the input are altered before the code is evaluated. For example, `return` is prepended for most expressions, a semicolon is added if it was omitted, and so on. See /Expansion/, below.

1. The special variable `$__exp__`. This contains the expansion of the last read input. Note that evaluating this to see it’s value will overwrite it.
2. The `PHP_Repl::cleanup()` method. When you pass it a string of input as you’d enter it on the REPL commandline, it will return the expanded form which will be evaluated.

Examples:

php> 5+5
int(10)
php> $__exp__
'return 5+5;'
php> $this->cleanup('5+5')
'return 5+5;'
php>

When you’re done with the REPL, you can send `^D`, `die()`, or `exit` to exit.
When you’re done with the REPL, you can send `^D`, `die`, or `exit` to quit.

Sugar
=====
Expand All @@ -100,6 +87,7 @@ Doc blocks can be accessed with `,d`:
* @author Ian Eure <ieure@php.net>
* @version @package_version@
*/
'---'
php> ,d PHP_Repl::read
/**
* Read input
Expand All @@ -108,6 +96,7 @@ Doc blocks can be accessed with `,d`:
*
* @return string Input
*/
'---'
php>

Reflection
Expand All @@ -122,8 +111,20 @@ The REPL knows how to reflect classes, objects, and methods.
Parameter #1 [ <optional> $sort_flags ]
}
}
string(4) "sort"
NULL
'---'

Expansion
---------
As mentioned, input is slightly altered before it is evaluated. Since this may produce unexpected effects, you can examine the expanded code with the `,e` shortcut, or by running `$this->cleanup()`:

php> 5+5
int(10)
php> ,e
'return 5+5;'
php> $this->cleanup('5+5')
'return 5+5;'
php> ,e 5+5
'return 5+5;'


Configuration
Expand Down
3 changes: 2 additions & 1 deletion php-repl.el → data/php-repl.el
Expand Up @@ -74,7 +74,8 @@
"PHP" buf php-repl-program nil
(mapconcat 'identity php-repl-program-arguments " "))
(setq inferior-php-buffer buf)
(pop-to-buffer buf t)
(display-buffer buf t)
;; (pop-to-buffer buf t)
(inferior-php-mode)))

(define-derived-mode inferior-php-mode comint-mode "Inferior PHP")
Expand Down
10 changes: 7 additions & 3 deletions package.xml
Expand Up @@ -18,8 +18,8 @@
</lead>
<date>2009-04-21</date>
<version>
<release>0.8.5</release>
<api>0.8.5</api>
<release>0.8.6dev2</release>
<api>0.8.6</api>
</version>
<stability>
<release>beta</release>
Expand All @@ -30,6 +30,7 @@
- Allow multiline input.
- Basic dissection and sugar.
- Save last-evaluated code in $__exp__.
- Start output buffering at the top of run().
</notes>
<contents>
<dir name="/">
Expand All @@ -40,7 +41,7 @@
<tasks:replace from="@package_version@" to="version"
type="package-info"/>

</file>
</file>)
</dir>
<dir name="scripts">
<file name="php-repl" role="script" install-as="php-repl"
Expand All @@ -51,6 +52,9 @@
type="package-info"/>
</file>
</dir>
<dir name="data">
<file name="php-repl.el" role="data"/>
</dir>
</dir>
</contents>
<dependencies>
Expand Down

0 comments on commit 95481d1

Please sign in to comment.