Skip to content

[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
wants to merge 1 commit into
base: 6.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 88 additions & 3 deletions components/var_dumper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -496,6 +496,8 @@ like this::
$dumper->dump($cloner->cloneVar($var));
});

.. _var-dumper-cloners:

Cloners
~~~~~~~

Expand Down Expand Up @@ -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.
Comment on lines +952 to +953
Copy link
Contributor

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 ?


.. 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Special options" is not clear to me. Do you have VAR_DUMPER_OPTIONS in mind ?