Skip to content

Commit ff94744

Browse files
[VarDumper] Add an options builder to configure VarDumper's behavior at runtime
1 parent 53db051 commit ff94744

File tree

1 file changed

+83
-3
lines changed

1 file changed

+83
-3
lines changed

components/var_dumper.rst

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ current PHP SAPI:
7070

7171
.. note::
7272

73-
If you want to catch the dump output as a string, please read the
74-
:ref:`advanced section <var-dumper-advanced>` which contains examples of
75-
it.
73+
If you want to catch the dump output as a string or customize the way information
74+
are displayed, please read the :ref:`advanced section <var-dumper-advanced>` which
75+
contains examples of it.
7676
You'll also learn how to change the format or redirect the output to
7777
wherever you want.
7878

@@ -496,6 +496,8 @@ like this::
496496
$dumper->dump($cloner->cloneVar($var));
497497
});
498498

499+
.. _var-dumper-cloners:
500+
499501
Cloners
500502
~~~~~~~
501503

@@ -875,3 +877,81 @@ that holds a file name or a URL, you can wrap them in a ``LinkStub`` to tell
875877

876878
return $array;
877879
}
880+
881+
Customizing The Output On ``dump()`` Call
882+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
883+
884+
The ``dump()`` function takes full advantage of PHP 8 named arguments. By using
885+
the named argument syntax when calling ``dump()``, a label will be displayed right
886+
before the dump output::
887+
888+
$user = new User();
889+
$password = 'root';
890+
891+
dump(user: $user, password: $password);
892+
893+
This will output the label ``user`` before dumping the actual content of ``$user``, and
894+
also the label ``password`` before dumping ``$password`` content.
895+
896+
.. tip::
897+
898+
By passing multiple arguments to the ``dump()`` function in the same call, if no
899+
explicit named argument is given, the index (starting from 1) of the dumped variable
900+
will be displayed to help you group outputs by ``dump()`` call.
901+
902+
.. versionadded:: 6.3
903+
904+
The support of named arguments was introduced in Symfony 6.3.
905+
906+
The ``dump()`` function also accepts a set of named arguments to tweak the
907+
output. These named arguments all start with a ``_`` to avoid interfering with
908+
other named arguments you may use to display your own variables. For example, if
909+
you want to display a debug stacktrace with your variable content, you can do it
910+
as follow::
911+
912+
$var = ['test'];
913+
dump($var, _trace: true);
914+
915+
Here is the list of the supported options:
916+
917+
* ``_format``: explicitly choose the format to display the output between ``cli``,
918+
``html`` or ``server``.
919+
* ``_trace``: displays the debug backtrace from where ``dump()`` has been called.
920+
This option accepts a boolean (``true`` to display the trace, ``false`` to hide it),
921+
but also an integer. This integer limits the stacktrace size being displayed.
922+
* ``_theme``: set the theme to use when using ``html`` format, between ``dark`` or ``light``.
923+
* ``_charset``: set the charset to use for the output content.
924+
* ``_flags``: a bitmask of ``AbstractDumper::DUMP_*`` flags. To see available flags, have a
925+
look at :class:`Symfony\\Component\\VarDumper\\Dumper\\AbstractDumper`.
926+
* ``_max_items``: set the value being passed to
927+
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxItems` of the default
928+
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section.
929+
* ``_min_depth``: set the value being passed to
930+
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMinDepth` of the default
931+
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section.
932+
* ``_max_string``: set the value being passed to
933+
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxString` of the default
934+
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section.
935+
* ``_max_depth``: set the value being passed to
936+
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxDepth` of the default
937+
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section.
938+
* ``_max_items_per_depth``: set the value being passed to
939+
:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::setMaxItemsPerDepth` of the default
940+
cloner. For more information, see the :ref:`Cloners <var-dumper-cloners>` section.
941+
942+
You can also set these options globally thanks to the ``VAR_DUMPER_OPTIONS`` environment variable,
943+
formatted as a query string:
944+
945+
.. code-block:: bash
946+
947+
# .env (or .env.local)
948+
VAR_DUMPER_OPTIONS="_trace=3&_max_items=4&_theme=light"
949+
950+
Finally, the :class:`Symfony\\Component\\VarDumper\\Dumper\\VarDumperOptions` is an option
951+
builder that you can use to create your options array thanks to a fluent interface. This
952+
class provides the ``VarDumperOptions::toArray()`` method to get the options as an array
953+
and use them in a ``dump()`` call with to the spread operator.
954+
955+
.. versionadded:: 6.3
956+
957+
Special options of ``dump()`` were introduced in Symfony 6.3.

0 commit comments

Comments
 (0)