jonathrg/repl-breakpoint

在您的PHP脚本中的任何位置注入REPL

v1.0 2017-06-20 18:18 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:41:10 UTC


README

ReplBreakpoint 允许您在任何PHP脚本的位置启动REPL,并让您访问该点的执行状态。

安装

composer require jonathrg/repl-breakpoint

用法

这里我们定义一个变量和一个函数,然后调用 repl(get_defined_vars())

// File: examples/simple_example.php
<?php
require_once __DIR__.'/../vendor/autoload.php';
use function ReplBreakpoint\repl;

$a = 2;
function sq($x) { return $x * $x; }

repl(get_defined_vars());

使用PHP执行脚本,REPL将在第8行启动。它可以访问我们在父脚本中定义的变量

$ php examples/simple_example.php
REPL launched
To exit, write "exit" or press Ctrl+C.
> $a
2
> [$a, $a+2]
[2, 4]
> sq($a)
4

名为 $_ANS 的变量始终跟踪最后的结果

> $_ANS
4

多行代码

多行代码是正常的:如果括号不平衡,输入将自动缓冲...

> function cube($x) {
    return sq($x)*$x;
    }

> cube($_ANS)
64

... 或者当您用 \/ 结束行时。

> $_ANS \

> -2
62

选项

打印机

您可以通过在代码中添加选项来替换ReplBreakpoint使用的打印函数。只需将其添加为选项即可

// File: examples/simple_example.php
...
repl(get_defined_vars(), ['printer' => 'print_r']);

现在结果使用 print_r 打印。

$ php examples/simple_example.php
REPL launched
To exit, write "exit" or press Ctrl+C.
> $a
2
> [$a, $a+2]
Array
(
    [0] => 2
    [1] => 4
)

任何接受一个参数(要打印的值)的函数都是可以的。

函数名、文件名和行号

repl() 提供文件和行号,以便在会话开始时打印出来

// File: examples/simple_example.php
...
repl(get_defined_vars(), ['file' => __FILE__, 'line' => __LINE__, 'function' => __FUNCTION__]);

运行它(我们调用 repl() 时不在函数上下文中,所以没有显示函数)

$ php examples/simple_example.php
REPL launched in file path\to\examples\simple_example.php on line 8
To exit, write "exit" or press Ctrl+C.
> 

或者只需设置 'quiet' 标志以完全跳过信息

// File: examples/simple_example.php
...
repl(get_defined_vars(), ['quiet' => true]);

运行它

$ php examples/simple_example.php
>

错误处理程序和关闭函数

默认情况下,repl() 将尝试仅回显错误并继续。您可以通过提供自己的回调作为 'error_handler' 和 'shutdown_function' 选项来覆盖此行为。回调必须与 set_error_handlerregister_shutdown_function 兼容。