Skip to content

Commit

Permalink
feature #23831 [VarDumper] Introduce a new way to collect dumps throu…
Browse files Browse the repository at this point in the history
…gh a server dumper (ogizanagi, nicolas-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[VarDumper] Introduce a new way to collect dumps through a server dumper

| Q             | A
| ------------- | ---
| Branch?       | 4.1 <!-- see comment below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #22987 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | todo <!--highly recommended for new features-->

Could also be interesting as alternate solution for #23442.

Inspired by the [`ServerLogHandler`](#21080) and @nicolas-grekas's [comment](#22987 (comment)) in #22987.

---

## Description

This PR introduces a new `server:dump` command available in the `VarDumper` component.
Conjointly with a new `ServerDumper` data dumper, it allows to send serialized `Data` clones to a single centralized server, in charge of dumping them on CLI output, or in an file in HTML format.

## Showcase

### HTTP calls

For instance, when working on an API and dumping something, you might end up with a mix of your response and the CLI dumped version of the data you asked:

```php
class HelloController extends AbstractController
{
    /**
     * @route("/hello")
     */
    public function hello(Request $request, UserInterface $user)
    {
        dump($request->attributes, $user);

        return JsonResponse::create([
            'status' => 'OK',
            'message' => "Hello {$user->getUsername()}"
        ]);
    }
}
```

<img width="732" alt="screenshot 2017-08-08 a 16 44 24" src="https://user-images.githubusercontent.com/2211145/29077731-0b2152d6-7c59-11e7-99dd-2d060a906d48.PNG">

You might even get the HTML dump version [under some conditions](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php#L146-L157).

Dumping to a server allows collecting dumps in a separate place:

<!--![server-dumper-http-to-cli-output](https://user-images.githubusercontent.com/2211145/29077977-8ace19ce-7c59-11e7-998e-ee9c49e67958.gif)-->

![server-dump-http-to-cli](https://user-images.githubusercontent.com/2211145/29226044-dcc95f12-7ed0-11e7-8343-40aeb7b18dd5.gif)

~⚠️ By swallowing the previous dumpers output, the dump data collector is not active when running the dump server. Disable swallowing if you want both.~ ➜ Dumps are still collected in the profiler thanks to f24712e

### CLI calls

The best probably is (to me) that you can also debug CLI applications...

![server-dump-cli](https://user-images.githubusercontent.com/2211145/29225951-84e29098-7ed0-11e7-8067-abaa9c50d169.gif)

<!--![server-dumper-cli-to-cli-output](https://user-images.githubusercontent.com/2211145/29078261-1eb950ea-7c5a-11e7-94ee-aa3ae3bf7fb0.gif)-->

...and get HTML formatted dumps:

![server-dumper-cli-to-html-output](https://user-images.githubusercontent.com/2211145/30784609-d7dc19b8-a158-11e7-9b11-88ae819cfcca.gif)

<!--![server-dump-cli-to-html](https://user-images.githubusercontent.com/2211145/29225866-2b5675e4-7ed0-11e7-98eb-339611bd94a7.gif)-->

<!--![server-dumper-cli-to-html-output](https://user-images.githubusercontent.com/2211145/29078247-17e33e5c-7c5a-11e7-94f7-47de6774e0e8.gif)-->

hence, benefit from all the features of this format (collapse, search, ...)

### HTML output at a glance

<img width="831" alt="screenshot 2017-08-11 a 19 28 25" src="https://user-images.githubusercontent.com/2211145/29225513-eae349f2-7ece-11e7-9861-8cda9e80ba7f.PNG">

The HTML output benefits from all the `HtmlDumper` features and contains extra informations about the context (sources, requests, command line, ...). It doesn't aim to replace the profiler for HTTP calls with the framework, but is really handy for CLI apps or by wiring it in your own web app, out of the framework usage.

### CLI output at a glance

<img width="829" alt="screenshot 2017-08-11 a 19 52 57" src="https://user-images.githubusercontent.com/2211145/29225482-c24afe18-7ece-11e7-8e83-d019b0d8303e.PNG">

## Usage within the framework

### Config

For instance, in your `config_dev.yml`:

```yml
#config_dev.yml
debug:
    server_dump: true
```

or in your `config.yml`:

```yml
#config.yml
debug:
    server_dump: '%kernel.debug%'
```

~~The configuration also allows to set a `host` option, but there is already a sensible default value (`tcp://0.0.0.0:9912`) so you don't have to deal with it.~~ Since b002175, in case you want to change the default host value used, simply use the `VAR_DUMPER_SERVER` env var.

When the server is running, all dumps are collected by the server and previous dumpers ones are "swallowed" ~~by default. If you want both to collect dumps on the server AND keep previous dumpers on regular outputs, you can disable swallowing:~~

<!--
```yml
debug:
    server_dump:
        swallow: false
```
-->

When the server isn't running or in case of failure to send the data clones to the server, the server dumper will delegates to the configured wrapped dumper, so dumps are displayed and collected as usual.

### Running the server

```bash
bin/console server:dump [--format=cli|html]
```

#### Options

- ~~The `output` option defaults to `null` which will display dumps on CLI. It accepts a file path in which dumps will be collected in HTML format.~~
- The `format` option allows to switch format to use. For instance, use the `html` format and redirect the output to a file in order to open it in your browser and inspect dumps in HTML format.
- ~~The default `host` value is the same as the one configured under the `debug.server_dump.host` config option, so you don't have to deal with it in most cases.~~
    Since b002175, in case you want to change the default host value used, simply use the `VAR_DUMPER_SERVER` env var:

    ```bash
    VAR_DUMPER_SERVER=0.0.0.0:9204 bin/console server:dump
    ```

## Manual wiring

If you want to wire it yourself in your own project or using it to inspect dumps as html before the kernel is even boot for instance:

```php
$host = 'tcp://0.0.0.0:9912'; // use null to read from the VAR_DUMPER_SERVER env var
$cloner = new VarCloner();
$dumper = new ServerDumper($host, new CliDumper());

VarDumper::setHandler(function ($var) use ($dumper, $cloner) {
    $dumper->dump($cloner->cloneVar($var));
});
```

## Create your own server app

The component already provides a default server app by means of the `ServerDumpCommand`, but
 you could also build your own by using the `DumpServer`:

```php
$host = 'tcp://0.0.0.0:9912'; // use null to read from the VAR_DUMPER_SERVER env var

$server = new DumpServer($host);

$server->start();

$server->listen(function (Data $data, array $context, $clientId) {
    // do something
});
```

Commits
-------

138dad6 [VarDumper] Some tweaks after Nicolas' commit & ServerDumperPlaceholderCommand
088c52e [VarDumper] Review config to use debug.dump_destination & tweak data collector
3db1404 [VarDumper] Introduce a new way to collect dumps through a server dumper
  • Loading branch information
fabpot committed Mar 23, 2018
2 parents 54305d6 + 138dad6 commit 4bbdf06
Show file tree
Hide file tree
Showing 24 changed files with 1,182 additions and 89 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/DebugBundle/CHANGELOG.md
@@ -0,0 +1,8 @@
CHANGELOG
=========

4.1.0
-----

* Added the `server:dump` command to run a server collecting and displaying
dumps on a single place with multiple formats support
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DebugBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\VarDumper\Command\ServerDumpCommand;
use Symfony\Component\VarDumper\Server\DumpServer;

/**
* A placeholder command easing VarDumper server discovery.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @internal
*/
class ServerDumpPlaceholderCommand extends ServerDumpCommand
{
public function __construct(DumpServer $server = null, array $descriptors = array())
{
parent::__construct(new class() extends DumpServer {
public function __construct()
{
}
}, $descriptors);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
(new SymfonyStyle($input, $output))->getErrorStyle()->warning('In order to use the VarDumper server, set the "debug.dump_destination" config option to "tcp://%env(VAR_DUMPER_SERVER)%"');

return 8;
}
}
Expand Up @@ -48,7 +48,7 @@ public function getConfigTreeBuilder()
->end()
->scalarNode('dump_destination')
->info('A stream URL where dumps should be written to')
->example('php://stderr')
->example('php://stderr, or tcp://%env(VAR_DUMPER_SERVER)% when using the "server:dump" command')
->defaultNull()
->end()
->end()
Expand Down
Expand Up @@ -11,11 +11,13 @@

namespace Symfony\Bundle\DebugBundle\DependencyInjection;

use Symfony\Bundle\DebugBundle\Command\ServerDumpPlaceholderCommand;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\VarDumper\Dumper\ServerDumper;

/**
* DebugExtension.
Expand All @@ -40,14 +42,37 @@ public function load(array $configs, ContainerBuilder $container)
->addMethodCall('setMinDepth', array($config['min_depth']))
->addMethodCall('setMaxString', array($config['max_string_length']));

if (null !== $config['dump_destination']) {
if (null === $config['dump_destination']) {
//no-op
} elseif (0 === strpos($config['dump_destination'], 'tcp://')) {
$serverDumperHost = $config['dump_destination'];
$container->getDefinition('debug.dump_listener')
->replaceArgument(1, new Reference('var_dumper.server_dumper'))
;
$container->getDefinition('data_collector.dump')
->replaceArgument(4, new Reference('var_dumper.server_dumper'))
;
$container->getDefinition('var_dumper.dump_server')
->replaceArgument(0, $serverDumperHost)
;
$container->getDefinition('var_dumper.server_dumper')
->replaceArgument(0, $serverDumperHost)
;
} else {
$container->getDefinition('var_dumper.cli_dumper')
->replaceArgument(0, $config['dump_destination'])
;
$container->getDefinition('data_collector.dump')
->replaceArgument(4, new Reference('var_dumper.cli_dumper'))
;
}

if (!isset($serverDumperHost)) {
$container->getDefinition('var_dumper.command.server_dump')->setClass(ServerDumpPlaceholderCommand::class);
if (!class_exists(ServerDumper::class)) {
$container->removeDefinition('var_dumper.command.server_dump');
}
}
}

/**
Expand Down
52 changes: 51 additions & 1 deletion src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Expand Up @@ -4,6 +4,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="env(VAR_DUMPER_SERVER)">127.0.0.1:9912</parameter>
</parameters>

<services>
<defaults public="false" />

Expand All @@ -19,7 +23,7 @@
<argument type="service" id="debug.file_link_formatter" on-invalid="ignore"></argument>
<argument>%kernel.charset%</argument>
<argument type="service" id="request_stack" />
<argument>null</argument><!-- var_dumper.cli_dumper when debug.dump_destination is set -->
<argument>null</argument><!-- var_dumper.cli_dumper or var_dumper.server_dumper when debug.dump_destination is set -->
</service>

<service id="debug.dump_listener" class="Symfony\Component\HttpKernel\EventListener\DumpListener">
Expand All @@ -34,6 +38,7 @@
<argument>%kernel.charset%</argument>
<argument>0</argument> <!-- flags -->
</service>

<service id="var_dumper.html_dumper" class="Symfony\Component\VarDumper\Dumper\HtmlDumper">
<argument>null</argument>
<argument>%kernel.charset%</argument>
Expand All @@ -44,5 +49,50 @@
</argument>
</call>
</service>

<service id="var_dumper.server_dumper" class="Symfony\Component\VarDumper\Dumper\ServerDumper">
<argument>null</argument> <!-- server host -->
<argument type="service" id="var_dumper.cli_dumper" />
<argument type="collection">
<argument type="service" key="source">
<service class="Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider">
<argument>%kernel.charset%</argument>
<argument type="string">%kernel.project_dir%</argument>
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
</service>
</argument>
<argument type="service" key="request">
<service class="Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider">
<argument type="service" id="request_stack" />
</service>
</argument>
<argument type="service" key="cli">
<service class="Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider" />
</argument>
</argument>
</service>

<service id="var_dumper.dump_server" class="Symfony\Component\VarDumper\Server\DumpServer">
<argument /> <!-- server host -->
<argument type="service" id="logger" on-invalid="null" />
<tag name="monolog.logger" channel="debug" />
</service>

<service id="var_dumper.command.server_dump" class="Symfony\Component\VarDumper\Command\ServerDumpCommand">
<argument type="service" id="var_dumper.dump_server" />
<argument type="collection">
<argument type="service" key="cli">
<service class="Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor">
<argument type="service" id="var_dumper.cli_dumper" />
</service>
</argument>
<argument type="service" key="html">
<service class="Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor">
<argument type="service" id="var_dumper.html_dumper" />
</service>
</argument>
</argument>
<tag name="console.command" command="server:dump" />
</service>
</services>
</container>
103 changes: 21 additions & 82 deletions src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php
Expand Up @@ -18,9 +18,10 @@
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use Twig\Template;
use Symfony\Component\VarDumper\Dumper\ServerDumper;

/**
* @author Nicolas Grekas <p@tchwork.com>
Expand All @@ -38,6 +39,7 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
private $requestStack;
private $dumper;
private $dumperIsInjected;
private $sourceContextProvider;

public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, string $charset = null, RequestStack $requestStack = null, DataDumperInterface $dumper = null)
{
Expand All @@ -55,6 +57,8 @@ public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null,
&$this->isCollected,
&$this->clonesCount,
);

$this->sourceContextProvider = $dumper instanceof ServerDumper && isset($dumper->getContextProviders()['source']) ? $dumper->getContextProviders()['source'] : new SourceContextProvider($this->charset);
}

public function __clone()
Expand All @@ -71,61 +75,10 @@ public function dump(Data $data)
$this->isCollected = false;
}

$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 7);

$file = $trace[0]['file'];
$line = $trace[0]['line'];
$name = false;
$fileExcerpt = false;

for ($i = 1; $i < 7; ++$i) {
if (isset($trace[$i]['class'], $trace[$i]['function'])
&& 'dump' === $trace[$i]['function']
&& 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];

while (++$i < 7) {
if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];

break;
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) {
$template = $trace[$i]['object'];
$name = $template->getTemplateName();
$src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
$info = $template->getDebugInfo();
if (isset($info[$trace[$i - 1]['line']])) {
$line = $info[$trace[$i - 1]['line']];
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;

if ($src) {
$src = explode("\n", $src);
$fileExcerpt = array();

for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) {
$fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
}

$fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
}
}
break;
}
}
break;
}
}

if (false === $name) {
$name = str_replace('\\', '/', $file);
$name = substr($name, strrpos($name, '/') + 1);
}
list('name' => $name, 'file' => $file, 'line' => $line, 'file_excerpt' => $fileExcerpt) = $this->sourceContextProvider->getContext();

if ($this->dumper) {
$this->doDump($data, $name, $file, $line);
$this->doDump($this->dumper, $data, $name, $file, $line);
}

$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
Expand All @@ -152,14 +105,14 @@ public function collect(Request $request, Response $response, \Exception $except
|| false === strripos($response->getContent(), '</body>')
) {
if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
$this->dumper = new HtmlDumper('php://output', $this->charset);
$this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
$dumper = new HtmlDumper('php://output', $this->charset);
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
} else {
$this->dumper = new CliDumper('php://output', $this->charset);
$dumper = new CliDumper('php://output', $this->charset);
}

foreach ($this->data as $dump) {
$this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
}
}
}
Expand Down Expand Up @@ -251,25 +204,25 @@ public function __destruct()
}

if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
$this->dumper = new HtmlDumper('php://output', $this->charset);
$this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
$dumper = new HtmlDumper('php://output', $this->charset);
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
} else {
$this->dumper = new CliDumper('php://output', $this->charset);
$dumper = new CliDumper('php://output', $this->charset);
}

foreach ($this->data as $i => $dump) {
$this->data[$i] = null;
$this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
}

$this->data = array();
$this->dataCount = 0;
}
}

private function doDump($data, $name, $file, $line)
private function doDump(DataDumperInterface $dumper, $data, $name, $file, $line)
{
if ($this->dumper instanceof CliDumper) {
if ($dumper instanceof CliDumper) {
$contextDumper = function ($name, $file, $line, $fmt) {
if ($this instanceof HtmlDumper) {
if ($file) {
Expand All @@ -290,26 +243,12 @@ private function doDump($data, $name, $file, $line)
}
$this->dumpLine(0);
};
$contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
$contextDumper = $contextDumper->bindTo($dumper, $dumper);
$contextDumper($name, $file, $line, $this->fileLinkFormat);
} else {
} elseif (!$dumper instanceof ServerDumper) {
$cloner = new VarCloner();
$this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
$dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
}
$this->dumper->dump($data);
}

private function htmlEncode($s)
{
$html = '';

$dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
$dumper->setDumpHeader('');
$dumper->setDumpBoundaries('', '');

$cloner = new VarCloner();
$dumper->dump($cloner->cloneVar($s));

return substr(strip_tags($html), 1, -1);
$dumper->dump($data);
}
}

0 comments on commit 4bbdf06

Please sign in to comment.