Skip to content

Commit d3e0e77

Browse files
committed
[VarDumper] Enhance the ServerDumper documentation
1 parent 60eaca3 commit d3e0e77

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

components/var_dumper.rst

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ current PHP SAPI:
9191
.. versionadded:: 4.1
9292
The ``dd()`` helper method was introduced in Symfony 4.1.
9393

94+
95+
.. _var-dumper-dump-server:
96+
9497
The Dump Server
9598
---------------
9699

@@ -117,22 +120,71 @@ server, which outputs it to its own console or to an HTML file:
117120
118121
Inside a Symfony application, the output of the dump server is configured with
119122
the :ref:`dump_destination option <configuration-debug-dump_destination>` of the
120-
``debug`` package.
123+
``debug`` package:
124+
125+
.. configuration-block::
126+
127+
.. code-block:: yaml
128+
129+
# config/packages/debug.yaml
130+
debug:
131+
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"
132+
133+
.. code-block:: xml
134+
135+
<!-- config/packages/debug.xml -->
136+
<?xml version="1.0" encoding="UTF-8" ?>
137+
<container xmlns="http://symfony.com/schema/dic/debug"
138+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
139+
xmlns:debug="http://symfony.com/schema/dic/debug"
140+
xsi:schemaLocation="http://symfony.com/schema/dic/services
141+
http://symfony.com/schema/dic/services/services-1.0.xsd
142+
http://symfony.com/schema/dic/debug http://symfony.com/schema/dic/debug/debug-1.0.xsd">
143+
144+
<debug:config dump-destination="tcp://%env(VAR_DUMPER_SERVER)%" />
145+
</container>
146+
147+
.. code-block:: php
148+
149+
// config/packages/debug.php
150+
$container->loadFromExtension('debug', array(
151+
'dump_destination' => 'tcp://%env(VAR_DUMPER_SERVER)%',
152+
));
121153
122-
Outside a Symfony application, use the ``ServerDumper`` class::
154+
Outside a Symfony application, use the :class:``Symfony\\Component\\VarDumper\\Dumper\\ServerDumper`` class::
123155

124156
require __DIR__.'/vendor/autoload.php';
125157
126158
use Symfony\Component\VarDumper\VarDumper;
127159
use Symfony\Component\VarDumper\Cloner\VarCloner;
128160
use Symfony\Component\VarDumper\Dumper\ServerDumper;
129-
130-
VarDumper::setHandler(function ($var) {
131-
$cloner = new VarCloner();
132-
$dumper = new ServerDumper('tcp://127.0.0.1:9912');
133-
$dumper->dump($cloner->cloneVar($var));
161+
162+
$cloner = new VarCloner();
163+
$fallbackDumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg')) ? new CliDumper() : new HtmlDumper();
164+
$dumper = new ServerDumper('tcp://127.0.0.1:9912', $fallbackDumper, [
165+
'cli' => new CliContextProvider(),
166+
'source' => new SourceContextProvider(),
167+
]);
168+
169+
VarDumper::setHandler(function ($var) use ($cloner, $dumper) {
170+
$dumper->dump($cloner->cloneVar($var));
134171
});
135172

173+
174+
.. note::
175+
176+
The :class:``Symfony\\Component\\VarDumper\\Dumper\\ServerDumper`` accepts as second argument
177+
a :class:``Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface`` instance
178+
as a fallback for whenever the server is unreachable and context providers as third argument.
179+
These providers allow to gather some info about the context in which was dumped the data.
180+
Built-in contexts providers are: ``cli``, ``request`` and ``source``.
181+
182+
Then you can use the following command to start a server out-of-the-box:
183+
184+
.. code-block:: terminal
185+
$ ./vendor/bin/var-dump-server
186+
[OK] Server listening on tcp://127.0.0.1:9912
187+
136188
DebugBundle and Twig Integration
137189
--------------------------------
138190

reference/configuration/debug.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ destination for dumps. Typically, you would set this to ``php://stderr``:
9999
$container->loadFromExtension('debug', array(
100100
'dump_destination' => 'php://stderr',
101101
));
102+
103+
Configure it to ``"tcp://%env(VAR_DUMPER_SERVER)%"`` in order to use the :ref:`ServerDumper feature <var-dumper-dump-server>`.

0 commit comments

Comments
 (0)