Skip to content

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

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

Merged
merged 3 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/DebugBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}
Loading