Skip to content

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Aug 8, 2020

RFC: https://wiki.php.net/rfc/readline_interactive_shell_result_function

  1. This adds readline_interactive_shell_result_function()
    that can be used to configure a custom callback to dump results of standalone expressions.
    (For interactive shells)
  2. This also adds a default dumper for expressions, which behaves like the following snippet. (see test cases)
  3. The original behavior of never dumping expression results can be restored by setting cli.enable_interactive_shell_result_function=0
readline_interactive_shell_result_function(
    function(string $code, $result) {
        if (!isset($result)) {
            return;
        }
        if (is_scalar($result)) {
            echo "=> " . var_export($result, true) . "\n";
        } else {
            echo "=> "; var_dump($result);
        }});

See ext/readline/tests/readline_interactive_result.phpt for an example of how a
customized callback could be set.

Those hooks can use projects such as php-parser or php-ast to customize
the logic of whether the value of assignments or print() expressions
should not be printed.
Example usage:

readline_interactive_shell_result_function(
    function (string $code, $result) {
        echo "Saw " . trim($code) . "\n";
        echo json_encode($result);
    });

The system ini setting cli.enable_interactive_shell_result_function
can be used to disable this functionality to debug applications that load
buggy handlers. It is enabled by default.

Currently, this only attempts to support single expressions terminated by ;,
if the ini setting is enabled, a handler is configured, and parsing the code doesn't throw a ParseError/CompileError.
See ext/readline/tests/libedit_interactive_result.phpt for an example session.

Related to https://externals.io/message/111073

@TysonAndre TysonAndre force-pushed the interactive-shell-extensions branch 2 times, most recently from 6a873a8 to caa1cd5 Compare August 9, 2020 16:40
@cmb69 cmb69 added the Feature label Aug 21, 2020
@TysonAndre TysonAndre force-pushed the interactive-shell-extensions branch 4 times, most recently from 5225ec7 to 17278b6 Compare December 20, 2020 22:10
@TysonAndre TysonAndre force-pushed the interactive-shell-extensions branch from 17278b6 to 9cb8b20 Compare January 6, 2021 02:17
This adds `readline_interactive_shell_result_function`
that can be used to dump results of standalone expressions.
(For interactive shells)

See ext/readline/tests/readline_interactive_result.phpt for an example of how a
hook could be set.

Those hooks can use projects such as php-parser or php-ast to customize
the logic of whether the value of assignments or print() expressions
should not be printed.
Example usage:

```
readline_interactive_shell_result_function(
    function (string $code, $result) {
        echo "Saw " . trim($code) . "\n";
        echo json_encode($result);
    });
```

The system ini setting `cli.enable_interactive_shell_result_function`
can be used to disable this functionality to debug applications that load
buggy handlers. It is enabled by default.

Currently, this only attempts to support single expressions terminated by `;`,
if parsing the code doesn't throw a ParseError/CompileError.
See ext/readline/tests/libedit_interactive_result.phpt for an example session.

See ext/readline/tests/readline_interactive_result_pretty_proposed_default.phpt
for what a default may look like

Related to https://externals.io/message/111073
@TysonAndre TysonAndre force-pushed the interactive-shell-extensions branch from 9cb8b20 to 565b403 Compare January 16, 2021 20:32
@TysonAndre TysonAndre changed the title Proposal: Custom callback to dump results in php -a RFC: Custom callback to dump results in php -a Jan 16, 2021
@TysonAndre TysonAndre force-pushed the interactive-shell-extensions branch from 565b403 to 102953e Compare January 17, 2021 15:02
@nikic
Copy link
Member

nikic commented Jul 19, 2021

Closing this as the RFC has been declined: https://wiki.php.net/rfc/readline_interactive_shell_result_function#vote

@nikic nikic closed this Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants