-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[VarDumper] Add an options builder to configure VarDumper's behavior at runtime #17787
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
Open
alexandre-daubois
wants to merge
1
commit into
symfony:6.3
Choose a base branch
from
alexandre-daubois:dd-dump-advanved
base: 6.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,9 +70,9 @@ current PHP SAPI: | |
|
||
.. note:: | ||
|
||
If you want to catch the dump output as a string, please read the | ||
:ref:`advanced section <var-dumper-advanced>` which contains examples of | ||
it. | ||
If you want to catch the dump output as a string or customize the way information | ||
are displayed, please read the :ref:`advanced section <var-dumper-advanced>` which | ||
contains examples of it. | ||
You'll also learn how to change the format or redirect the output to | ||
wherever you want. | ||
|
||
|
@@ -496,6 +496,8 @@ like this:: | |
$dumper->dump($cloner->cloneVar($var)); | ||
}); | ||
|
||
.. _var-dumper-cloners: | ||
|
||
Cloners | ||
~~~~~~~ | ||
|
||
|
@@ -875,3 +877,86 @@ that holds a file name or a URL, you can wrap them in a ``LinkStub`` to tell | |
|
||
return $array; | ||
} | ||
|
||
Customizing The Output On ``dump()`` Call | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The ``dump()`` function takes full advantage of PHP 8 named arguments. By using | ||
the named argument syntax when calling ``dump()``, a label will be displayed right | ||
before the dump output:: | ||
|
||
$user = new User(); | ||
$password = 'root'; | ||
|
||
dump(user: $user, password: $password); | ||
|
||
This will output the label ``user`` before dumping the actual content of ``$user``, and | ||
also the label ``password`` before dumping ``$password`` content. | ||
|
||
.. tip:: | ||
|
||
By passing multiple arguments to the ``dump()`` function in the same call, if no | ||
explicit named argument is given, the index (starting from 1) of the dumped variable | ||
will be displayed to help you group outputs by ``dump()`` call. | ||
|
||
.. versionadded:: 6.3 | ||
|
||
The support of named arguments was introduced in Symfony 6.3. | ||
|
||
The ``dump()`` function also accepts a set of named arguments to tweak the | ||
output. These named arguments all start with a ``_`` to avoid interfering with | ||
other named arguments you may use to display your own variables. For example, if | ||
you want to display a debug stacktrace with your variable content, you can do it | ||
as follow:: | ||
|
||
$var = ['test']; | ||
dump($var, _trace: true); | ||
|
||
Here is the list of the supported options: | ||
|
||
* ``_format``: explicitly choose the format to display the output between ``cli``, | ||
``html`` or ``server``. | ||
* ``_trace``: displays the debug backtrace from where ``dump()`` has been called. | ||
This option accepts a boolean (``true`` to display the trace, ``false`` to hide it), | ||
but also an integer. This integer limits the stacktrace size being displayed. | ||
* ``_theme``: set the theme to use when using ``html`` format, between ``dark`` or ``light``. | ||
* ``_charset``: set the charset to use for the output content. | ||
* ``_flags``: a bitmask of ``AbstractDumper::DUMP_*`` flags. To see available flags, have a | ||
look at :class:`Symfony\\Component\\VarDumper\\Dumper\\AbstractDumper`. | ||
* ``_max_items``: set the value being passed to | ||
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxItems` of the default | ||
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section. | ||
* ``_min_depth``: set the value being passed to | ||
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMinDepth` of the default | ||
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section. | ||
* ``_max_string``: set the value being passed to | ||
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxString` of the default | ||
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section. | ||
* ``_max_depth``: set the value being passed to | ||
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxDepth` of the default | ||
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section. | ||
* ``_max_items_per_depth``: set the value being passed to | ||
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxItemsPerDepth` of the default | ||
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section. | ||
|
||
You can also set these options globally thanks to the ``VAR_DUMPER_OPTIONS`` environment variable, | ||
formatted as a query string: | ||
|
||
.. code-block:: bash | ||
|
||
# .env (or .env.local) | ||
VAR_DUMPER_OPTIONS="_trace=3&_max_items=4&_theme=light" | ||
|
||
Finally, the :class:`Symfony\\Component\\VarDumper\\Dumper\\VarDumperOptions` is an option | ||
builder that you can use to create your options array thanks to a fluent interface. This | ||
class provides the ``VarDumperOptions::toArray()`` method to get the options as an array | ||
and use them in a ``dump()`` call with to the spread operator. | ||
|
||
.. note:: | ||
|
||
If a format is set with ``VAR_DUMPER_FORMAT`` environment variable, it will be used instead | ||
of the one defined by the ``VAR_DUMPER_OPTIONS`` environment variable. | ||
|
||
.. versionadded:: 6.3 | ||
|
||
Special options of ``dump()`` were introduced in Symfony 6.3. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Special options" is not clear to me. Do you have |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe we have to add an usage example ?