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
[VarDumper] Add an options builder to configure VarDumper's behavior at runtime #48667
base: 6.3
Are you sure you want to change the base?
[VarDumper] Add an options builder to configure VarDumper's behavior at runtime #48667
Conversation
b90f2fc
to
1a3ab94
Compare
Thanks for tackling this, that's a complex feature because the matrix of possibilities is huge.
Here are some random thoughts:
- I like the style proposed in the issue:
dump()->trace()->maxDepth(4)->values($var);
. I'm just wondering ifvalues()
shouldn't be nameddump()
orvar()
or ? - I also like the
dump($var, _trace: true)
style, which could work well with autocompletion if we describe the options as virtual arguments on the function?array{_trace: bool, ...}
- instead of trace_limit, what about
trace: bool|int
? - have a look at
FrameStub
/TraceStub
for dumping traces - we might want to provide a way to define the default options on the VarDumper class?
As states by @derrabus, this cannot work I don't really like the current object API: $options = (new VarDumperOptions())
->trace()
->stringLength()
; it's too long to type. We should not forgot we are a in debug context. So we want to be able to get a result ASAP. that's why we have the function That's why I prefer the second bullet point of @nicolas-grekas comment: dd($this, _trace: true, _maxDepth: 4); |
yes it can, see #48474 (comment) ;) |
This is ugly :) I really don't like it. And it's too slow to type. |
how is a fully autodiscoverable |
@ro0NL I mixed my thought :/ I was talking about: $options = (new VarDumperOptions())
->trace()
->stringLength()
;
dump($this, _options: $options); |
maybe
|
I agree the current object API is a bit much to type. I am Should I try to implement both @GromNaN solution ( Also, I really like the solution proposed by @ro0NL. If I may find a drawback, I'm afraid this syntax could be difficult to be "discovered" by yourself without reading the documentation. |
The syntax that don't work is the one that rely on __destruct. |
1a3ab94
to
90438de
Compare
I updated the feature. The new version allows you to do both ways: // Passing options as named parameters
dump('Values', _trace: 5, _flags: AbstractDumper::DUMP_STRING_LENGTH);
// Use the options builder syntax
dump()
->trace(5)
->stringLength()
->dd('Values') // or ->dump('Values')
; Still investigating on how to use Also, I made the choice to only have the options builder syntax on |
90438de
to
0b25f56
Compare
/** | ||
* @param string|null $label | ||
* @param VarDumperOptions|iterable $options |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/iterable/array/
0b25f56
to
1b933a2
Compare
1b933a2
to
c8f0cd9
Compare
}, | ||
"require-dev": { | ||
"symfony/config": "^5.4|^6.0", | ||
"symfony/web-profiler-bundle": "^5.4|^6.0" | ||
}, | ||
"conflict": { | ||
"symfony/config": "<5.4", | ||
"symfony/dependency-injection": "<5.4" | ||
"symfony/dependency-injection": "<5.4", | ||
"symfony/var-dumper": "<6.3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed
@@ -6,6 +6,7 @@ CHANGELOG | |||
|
|||
* Add caster for `WeakMap` | |||
* Add support of named arguments to `dd()` and `dump()` to display the argument name | |||
* Add support of dumper's behavior configuration with the special `_options` named argument for `dump` and `dd` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep thins simple, eg
Allowing configuring options when dumping
/** | ||
* @author Alexandre Daubois <alex.daubois@gmail.com> | ||
*/ | ||
enum HtmlDumperTheme: string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be removed as it closes the list of possible themes, while the list is open for extensibility
$this->options = array_replace($this->options, \array_intersect_key($options, $this->options)); | ||
} | ||
|
||
public function dump(mixed ...$vars): mixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would also benefit from the generics annotation
$vars = [new ScalarStub('🐛')]; | ||
} | ||
|
||
return dump(...($vars + $this->options)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for the brackets
using dump() instead of VarDumper::dump() is adding one extra frame in the backtrace.
it might be better to skip this frame (same for dd() below
return $this->options; | ||
} | ||
|
||
public function set(string $option, mixed $value): static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed I suppose
* | ||
* @internal | ||
*/ | ||
final class VarClonerFactory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd better not add this class, it adds indirection
} | ||
|
||
/** | ||
* Set flags manually. Valid flags are {@see AbstractDumper::DUMP_*} constants. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@param AbstractDumper::DUMP_* $flags
if (!$vars) { | ||
VarDumper::dump(new ScalarStub('🐛')); | ||
$options = new VarDumperOptions($vars); | ||
$vars = \array_diff_key($vars, $options->getOptions()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather ignore any arg that tarts with a _
in the foreach below
} | ||
|
||
if (self::requiresRegister($options)) { | ||
self::register($options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will change the options of the default handler when any options changes?
this would be unexpected to me.
When dumping large objects, it could be nice to be able to have the possibility to dump only one or several properties instead of the full object. Something like: |
Following up #48432, this PR brings an options builder to configure VarDumper's behavior thanks to a special
_options
named argument. Here is a snippet of it in action and a screenshot of the result. Feedbacks on UI are really welcomed, especially about the debug backtrace!