rfc:readline_interactive_shell_result_function
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:readline_interactive_shell_result_function [2020/12/20 22:37] – tandre | rfc:readline_interactive_shell_result_function [2021/02/03 00:39] (current) – tandre | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Configurable callback to dump results of expressions in `php -a` ====== | + | ====== PHP RFC: Dump results of expressions in `php -a` ====== |
- | * Version: 0.1 | + | * Version: 0.2 |
* Date: 2020-12-19 | * Date: 2020-12-19 | ||
* Author: Tyson Andre, tandre@php.net | * Author: Tyson Andre, tandre@php.net | ||
- | * Status: | + | * Status: |
* Implementation: | * Implementation: | ||
* First Published at: https:// | * First Published at: https:// | ||
Line 9: | Line 9: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Many other REPLs (Read-Eval-Print Loops) that I'm familiar with print a (possibly truncated) representation of the result of expressions, | + | Many REPLs (Read-Eval-Print Loops) |
It would be useful to allow users to extend the functionality of the default interactive php shell ('' | It would be useful to allow users to extend the functionality of the default interactive php shell ('' | ||
Prior to this RFC, there was no way to extend the interactive php shell in this way. | Prior to this RFC, there was no way to extend the interactive php shell in this way. | ||
(I've seen https:// | (I've seen https:// | ||
- | Because PHP's interactive shell is written in C, adding new features or bug fixes would require a lot of time getting familiar with | ||
- | C programming, | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | Add a new function '' | + | By default, in '' |
+ | Additionally, | ||
- | Using this, users or applications can configure callables to be run every time a statement containing a single expression such as '' | + | This will dump the results of expressions |
+ | |||
+ | An example of the behavior of the default expression dumper is below: | ||
+ | |||
+ | <code php> | ||
+ | $ php -a | ||
+ | Interactive shell | ||
+ | |||
+ | php > 1+1; | ||
+ | => 2 | ||
+ | php > 0.5 * 2; | ||
+ | => 1.0 | ||
+ | php > namespace\MyClass:: | ||
+ | => ' | ||
+ | php > fn()=> | ||
+ | => object(Closure)# | ||
+ | } | ||
+ | php > $x = [" | ||
+ | => array(2) { | ||
+ | [0]=> | ||
+ | string(3) " | ||
+ | [1]=> | ||
+ | string(3) " | ||
+ | } | ||
+ | php > asort($x); | ||
+ | => true | ||
+ | php > $x; | ||
+ | => array(2) { | ||
+ | [1]=> | ||
+ | string(3) " | ||
+ | [0]=> | ||
+ | string(3) " | ||
+ | } | ||
+ | php > json_encode($x); | ||
+ | => ' | ||
+ | php > unset($x); | ||
+ | php > function do_something() { echo "in do_something()\n"; | ||
+ | php > do_something(); | ||
+ | in do_something() | ||
+ | php > json_decode(' | ||
+ | => object(stdClass)# | ||
+ | [" | ||
+ | string(5) " | ||
+ | } | ||
+ | php > throw new RuntimeException(" | ||
+ | |||
+ | Warning: Uncaught RuntimeException: | ||
+ | Stack trace: | ||
+ | #0 {main} | ||
+ | thrown in php shell code on line 1 | ||
+ | php > printf(" | ||
+ | newline is automatically appended by shell | ||
+ | => 42 | ||
+ | php > printf(" | ||
+ | newline not automatically appended by shell | ||
+ | => 44 | ||
+ | php > { print(" | ||
+ | test | ||
+ | php > | ||
+ | </ | ||
+ | |||
+ | Using the function '' | ||
<code php> | <code php> | ||
- | php -a | + | $ php -a |
Interactive shell | Interactive shell | ||
Line 32: | Line 92: | ||
php ( echo json_encode($result); | php ( echo json_encode($result); | ||
php ( }); | php ( }); | ||
+ | Saw readline_interactive_shell_result_function( | ||
+ | | ||
+ | echo "Saw " . trim($code) . " | ||
+ | echo json_encode($result); | ||
+ | }); | ||
+ | true | ||
php > 2+2; | php > 2+2; | ||
Saw 2+2; | Saw 2+2; | ||
4 | 4 | ||
+ | php > readline_interactive_shell_result_function(null); | ||
+ | php > 2+2; | ||
+ | php > | ||
</ | </ | ||
Line 51: | Line 120: | ||
</ | </ | ||
- | A new system ini boolean setting '' | + | The default implementation added as part of this RFC is effectively identical to the below implementation, |
+ | |||
+ | <code php> | ||
+ | readline_interactive_shell_result_function( | ||
+ | function(string $code, $result) { | ||
+ | if (!isset($result)) { | ||
+ | return; | ||
+ | } | ||
+ | if (is_scalar($result)) { | ||
+ | echo "=> " . var_export($result, | ||
+ | } else { | ||
+ | echo "=> "; var_dump($result); | ||
+ | }}); | ||
+ | </ | ||
+ | |||
+ | A new system ini boolean setting '' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | None, only interactive sessions are affected, | + | |
+ | Only interactive sessions | ||
+ | |||
+ | In interactive sessions, this will start calling [[https:// | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 70: | Line 158: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | |||
- | ==== Providing a default callback to dump expression results ==== | ||
- | |||
- | This RFC is minimal and does not provide a default implementation for rendering values of expressions (i.e. there is no callback by default). Providing a default is feasible, but there are a variety of choices that could be made (e.g. var_dump vs var_export vs shorter JSON-like equivalents for simple arrays, handling of recursive data structures, truncating output, etc). | ||
- | |||
- | * In order to provide more extensive default handlers for tab completion and rendering of evaluated expressions, | ||
From https:// | From https:// | ||
Line 97: | Line 179: | ||
* i.e. change '' | * i.e. change '' | ||
- | ===== Proposed Voting Choices | + | ==== Rendering a result limit ==== |
+ | |||
+ | https:// | ||
+ | The setting '' | ||
+ | |||
+ | < | ||
+ | My main concern in this iteration of the RFC is: what happens with big/deeply nested objects? | ||
+ | They tend to spew tons of lines if var_dump()' | ||
+ | |||
+ | I'm often enough using php -a to do some quick ad-hoc processing (example, read a big json file, and then access a value; instantiating a mediawiki bot framework and calling replace on it; ...). | ||
+ | |||
+ | It's really cool to have any interactive feedback at all, but please, at least by default, limit the output. (An example is the JS REPL in browser console - it shows you a minimal preview of the object, and then you can expand with your mouse. Obviously with a pure cli application, | ||
+ | |||
+ | As it currently stands, this makes '' | ||
+ | |||
+ | I like the whole feature, but the missing output limitation (I have yet enough nightmares from var_dump()' | ||
+ | |||
+ | Thus I'm voting no, for now. | ||
+ | </ | ||
+ | ===== Vote ===== | ||
+ | |||
+ | Voting starts on 2021-01-19 and ended 2021-02-02 | ||
Yes/No, requiring 2/3 majority | Yes/No, requiring 2/3 majority | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== References ===== | ===== References ===== | ||
+ | * https:// | ||
* https:// | * https:// | ||
+ | * https:// | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | 0.2: Dump non-null expression results by default with var_dump()/ | ||
+ | 0.3: Document the default implementation used in the implementation |
rfc/readline_interactive_shell_result_function.1608503867.txt.gz · Last modified: 2020/12/20 22:37 by tandre