diff --git a/components/var_dumper.rst b/components/var_dumper.rst index f8f279929c5..f11d6401a3e 100644 --- a/components/var_dumper.rst +++ b/components/var_dumper.rst @@ -91,6 +91,8 @@ current PHP SAPI: .. versionadded:: 4.1 The ``dd()`` helper method was introduced in Symfony 4.1. +.. _var-dumper-dump-server: + The Dump Server --------------- @@ -117,22 +119,72 @@ server, which outputs it to its own console or to an HTML file: Inside a Symfony application, the output of the dump server is configured with the :ref:`dump_destination option ` of the -``debug`` package. +``debug`` package: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/debug.yaml + debug: + dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%" + + .. code-block:: xml -Outside a Symfony application, use the ``ServerDumper`` class:: + + + + + + + + .. code-block:: php + + // config/packages/debug.php + $container->loadFromExtension('debug', array( + 'dump_destination' => 'tcp://%env(VAR_DUMPER_SERVER)%', + )); + +Outside a Symfony application, use the :class:`Symfony\\Component\\VarDumper\\Dumper\\ServerDumper` class:: require __DIR__.'/vendor/autoload.php'; use Symfony\Component\VarDumper\VarDumper; use Symfony\Component\VarDumper\Cloner\VarCloner; use Symfony\Component\VarDumper\Dumper\ServerDumper; - - VarDumper::setHandler(function ($var) { - $cloner = new VarCloner(); - $dumper = new ServerDumper('tcp://127.0.0.1:9912'); - $dumper->dump($cloner->cloneVar($var)); + + $cloner = new VarCloner(); + $fallbackDumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg')) ? new CliDumper() : new HtmlDumper(); + $dumper = new ServerDumper('tcp://127.0.0.1:9912', $fallbackDumper, [ + 'cli' => new CliContextProvider(), + 'source' => new SourceContextProvider(), + ]); + + VarDumper::setHandler(function ($var) use ($cloner, $dumper) { + $dumper->dump($cloner->cloneVar($var)); }); + +.. note:: + + The :class:`Symfony\\Component\\VarDumper\\Dumper\\ServerDumper` accepts as second argument + a :class:`Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface` instance + as a fallback for whenever the server is unreachable and context providers as third argument. + These providers allow to gather some info about the context in which was dumped the data. + Built-in contexts providers are: ``cli``, ``request`` and ``source``. + +Then you can use the following command to start a server out-of-the-box: + +.. code-block:: terminal + + $ ./vendor/bin/var-dump-server + [OK] Server listening on tcp://127.0.0.1:9912 + DebugBundle and Twig Integration -------------------------------- diff --git a/reference/configuration/debug.rst b/reference/configuration/debug.rst index eb0931cd887..bed09590add 100644 --- a/reference/configuration/debug.rst +++ b/reference/configuration/debug.rst @@ -99,3 +99,5 @@ destination for dumps. Typically, you would set this to ``php://stderr``: $container->loadFromExtension('debug', array( 'dump_destination' => 'php://stderr', )); + +Configure it to ``"tcp://%env(VAR_DUMPER_SERVER)%"`` in order to use the :ref:`ServerDumper feature `.