Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Custom callback to dump results in php -a #5962

Closed

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
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 changed the title Proposal: Custom callback to dump results in php -a RFC: Custom callback to dump results in php -a Jan 16, 2021
@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.

None yet

3 participants