8000 [HttpKernel][VarDumper] Fix dumping with labels by nicolas-grekas · Pull Request #50397 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel][VarDumper] Fix dumping with labels #50397

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 1 commit into from
May 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
{% for dump in collector.getDumps('html') %}
<div class="sf-toolbar-info-piece">
<span>
{% if dump.label is defined and '' != dump.label %}
<span class="sf-toolbar-file-line"><strong>{{ dump.label }}</strong> in </span>
{% endif %}
{% if dump.file %}
{% set link = dump.file|file_link(dump.line) %}
{% if link %}
Expand Down Expand Up @@ -45,7 +48,12 @@

{% for dump in collector.getDumps('html') %}
<div class="sf-dump sf-reset">
<span class="metadata">In
<span class="metadata">
{% if dump.label is defined and '' != dump.label %}
<strong>{{ dump.label }}</strong> in
{% else %}
In
{% endif %}
{% if dump.line %}
{% set link = dump.file|file_link(dump.line) %}
{% if link %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,23 @@ public function dump(Data $data): ?string

['name' => $name, 'file' => $file, 'line' => $line, 'file_excerpt' => $fileExcerpt] = $this->sourceContextProvider->getContext();

if ($this->dumper instanceof Connection) {
if (!$this->dumper->write($data)) {
$this->isCollected = false;
}
} elseif ($this->dumper) {
$this->doDump($this->dumper, $data, $name, $file, $line);
} else {
if (!$this->dumper || $this->dumper instanceof Connection && !$this->dumper->write($data)) {
$this->isCollected = false;
}

$context = $data->getContext();
$label = $context['label'] ?? '';
unset($context['label']);
$data = $data->withContext($context);

if ($this->dumper && !$this->dumper instanceof Connection) {
$this->doDump($this->dumper, $data, $name, $file, $line, $label);
}

if (!$this->dataCount) {
$this->data = [];
}
$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt', 'label');
++$this->dataCount;

$this->stopwatch?->stop('dump');
Expand Down Expand Up @@ -125,7 +128,7 @@ public function collect(Request $request, Response $response, \Throwable $except
}

foreach ($this->data as $dump) {
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line'], $dump['label'] ?? '');
}
}
}
Expand Down Expand Up @@ -242,18 +245,20 @@ public function __destruct()

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

$this->data = [];
$this->dataCount = 0;
}
}

private function doDump(DataDumperInterface $dumper, Data $data, string $name, string $file, int $line): void
private function doDump(DataDumperInterface $dumper, Data $data, string $name, string $file, int $line, string $label): void
{
if ($dumper instanceof CliDumper) {
$contextDumper = function ($name, $file, $line, $fmt) {
$contextDumper = function ($name, $file, $line, $fmt, $label) {
$this->line = '' !== $label ? $this->style('meta', $label).' in ' : '';

if ($this instanceof HtmlDumper) {
if ($file) {
$s = $this->style('meta', '%s');
Expand All @@ -267,17 +272,17 @@ private function doDump(DataDumperInterface $dumper, Data $data, string $name, s
} else {
$name = $this->style('meta', $name);
}
$this->line = $name.' on line '.$this->style('meta', $line).':';
$this->line .= $name.' on line '.$this->style('meta', $line).':';
} else {
$this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
$this->line .= $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
}
$this->dumpLine(0);
};
$contextDumper = $contextDumper->bindTo($dumper, $dumper);
$contextDumper($name, $file, $line, $this->fileLinkFormat);
$contextDumper($name, $file, $line, $this->fileLinkFormat, $label);
} else {
$cloner = new VarCloner();
$dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
$dumper->dump($cloner->cloneVar(('' !== $label ? $label.' in ' : '').$name.' on line '.$line.':'));
}
$dumper->dump($data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DumpDataCollectorTest extends TestCase
public function testDump()
{
$data = new Data([[123]]);
$data = $data->withContext(['label' => 'foo']);

$collector = new DumpDataCollector(null, new FileLinkFormatter([]));

Expand All @@ -49,11 +50,12 @@ public function testDump()
'file' => __FILE__,
'line' => $line,
'fileExcerpt' => false,
'label' => 'foo',
],
];
$this->assertEquals($xDump, $dump);

$this->assertStringMatchesFormat('%a;a:%d:{i:0;a:5:{s:4:"data";%c:39:"Symfony\Component\VarDumper\Cloner\Data":%a', serialize($collector));
$this->assertStringMatchesFormat('%a;a:%d:{i:0;a:6:{s:4:"data";%c:39:"Symfony\Component\VarDumper\Cloner\Data":%a', serialize($collector));
$this->assertSame(0, $collector->getDumpsCount());

$serialized = serialize($collector);
Expand All @@ -77,7 +79,7 @@ public function testDumpWithServerConnection()
ob_start();
$collector->collect(new Request(), new Response());
$this->assertEmpty(ob_get_clean());
$this->assertStringMatchesFormat('%a;a:%d:{i:0;a:5:{s:4:"data";%c:39:"Symfony\Component\VarDumper\Cloner\Data":%a', serialize($collector));
$this->assertStringMatchesFormat('%a;a:%d:{i:0;a:6:{s:4:"data";%c:39:"Symfony\Component\VarDumper\Cloner\Data":%a', serialize($collector));
}

public function testCollectDefault()
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ public function dump(DumperInterface $dumper)
$cursor = new Cursor();
$cursor->hashType = -1;
$cursor->attr = $this->context[SourceContextProvider::class] ?? [];
$dumper->dumpScalar($cursor, 'label', $this->context['label'] ?? '');
$label = $this->context['label'] ?? '';

if ($cursor->attr || '' !== $label) {
$dumper->dumpScalar($cursor, 'label', $label);
}
$cursor->hashType = 0;
$this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);
}
Expand Down
14 changes: 10 additions & 4 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,14 @@ public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|n
$attr = $cursor->attr;

switch ($type) {
case 'label': $style = 'label'; break;
case 'default': $style = 'default'; break;
case 'default':
$style = 'default';
break;

case 'label':
$this->styles += ['label' => $this->styles['default']];
$style = 'label';
break;

case 'integer':
$style = 'num';
Expand Down Expand Up @@ -467,7 +473,7 @@ protected function style(string $style, string $value, array $attr = []): string

$map = static::$controlCharsMap;
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
$endCchr = $this->colors ? "\033[m\033[{$this->styles['label' === $style ? 'default' : $style]}m" : '';
$endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : '';
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
$s = $startCchr;
$c = $c[$i = 0];
Expand All @@ -490,7 +496,7 @@ protected function style(string $style, string $value, array $attr = []): string
if ($cchrCount && "\033" === $value[0]) {
$value = substr($value, \strlen($startCchr));
} else {
$value = "\033[{$this->styles['label' === $style ? 'default' : $style]}m".$value;
$value = "\033[{$this->styles[$style]}m".$value;
}
if ($cchrCount && str_ends_with($value, $endCchr)) {
$value = substr($value, 0, -\strlen($endCchr));
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ protected function style(string $style, string $value, array $attr = []): string
}

$map = static::$controlCharsMap;
$v = '<span class=sf-dump-'.('label' === $style ? 'default' : $style).'>'.preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
$v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
$s = $b = '<span class="sf-dump-default';
$c = $c[$i = 0];
if ($ns = "\r" === $c[$i] || "\n" === $c[$i]) {
Expand Down Expand Up @@ -944,6 +944,9 @@ protected function style(string $style, string $value, array $attr = []): string
if (isset($attr['lang'])) {
$v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
}
if ('label' === $style) {
$v .= ' ';
}

return $v;
}
Expand Down
0