10000 [VarDumper][HttpKernel] Fix dumping with labels · symfony/symfony@e824eb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e824eb0

Browse files
[VarDumper][HttpKernel] Fix dumping with labels
1 parent 2979e1e commit e824eb0

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

src/Symfony/Bundle/DebugBundle/Resources/views/Profiler/dump.html.twig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
{% for dump in collector.getDumps('html') %}
1212
<div class="sf-toolbar-info-piece">
1313
<span>
14+
{% if dump.label is defined and '' != dump.label %}
15+
<span class="sf-toolbar-file-line"><strong>{{ dump.label }}</strong> in </span>
16+
{% endif %}
1417
{% if dump.file %}
1518
{% set link = dump.file|file_link(dump.line) %}
1619
{% if link %}
@@ -45,7 +48,12 @@
4548

4649
{% for dump in collector.getDumps('html') %}
4750
<div class="sf-dump sf-reset">
48-
<span class="metadata">In
51+
<span class="metadata">
52+
{% if dump.label is defined and '' != dump.label %}
53+
<strong>{{ dump.label }}</strong> in
54+
{% else %}
55+
In
56+
{% endif %}
4957
{% if dump.line %}
5058
{% set link = dump.file|file_link(dump.line) %}
5159
{% if link %}

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,23 @@ public function dump(Data $data): ?string
7474

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

77-
if ($this->dumper instanceof Connection) {
78-
if (!$this->dumper->write($data)) {
79-
$this->isCollected = false;
80-
}
81-
} elseif ($this->dumper) {
82-
$this->doDump($this->dumper, $data, $name, $file, $line);
83-
} else {
77+
if (!$this->dumper || $this->dumper instanceof Connection && !$this->dumper->write($data)) {
8478
$this->isCollected = false;
8579
}
8680

81+
$context = $data->getContext();
82+
$label = $context['label'] ?? '';
83+
unset($context['label']);
84+
$data = $data->withContext($context);
85+
86+
if ($this->dumper && !$this->dumper instanceof Connection) {
87+
$this->doDump($this->dumper, $data, $name, $file, $line, $label);
88+
}
89+
8790
if (!$this->dataCount) {
8891
$this->data = [];
8992
}
90-
$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
93+
$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt', 'label');
9194
++$this->dataCount;
9295

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

127130
foreach ($this->data as $dump) {
128-
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
131+
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line'], $dump['label'] ?? '');
129132
}
130133
}
131134
}
@@ -242,18 +245,20 @@ public function __destruct()
242245

243246
foreach ($this->data as $i => $dump) {
244247
$this->data[$i] = null;
245-
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
248+
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line'], $dump['label'] ?? '');
246249
}
247250

248251
$this->data = [];
249252
$this->dataCount = 0;
250253
}
251254
}
252255

253-
private function doDump(DataDumperInterface $dumper, Data $data, string $name, string $file, int $line): void
256+
private function doDump(DataDumperInterface $dumper, Data $data, string $name, string $file, int $line, string $label): void
254257
{
255258
if ($dumper instanceof CliDumper) {
256-
$contextDumper = function ($name, $file, $line, $fmt) {
259+
$contextDumper = function ($name, $file, $line, $fmt, $label) {
260+
$this->line = '' !== $label ? $this->style('meta', $label).' in ' : '';
261+
257262
if ($this instanceof HtmlDumper) {
258263
if ($file) {
259264
$s = $this->style('meta', '%s');
@@ -267,17 +272,17 @@ private function doDump(DataDumperInterface $dumper, Data $data, string $name, s
267272
} else {
268273
$name = $this->style('meta', $name);
269274
}
270-
$this->line = $name.' on line '.$this->style('meta', $line).':';
275+
$this->line .= $name.' on line '.$this->style('meta', $line).':';
271276
} else {
272-
$this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
277+
$this->line .= $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
273278
}
274279
$this->dumpLine(0);
275280
};
276281
$contextDumper = $contextDumper->bindTo($dumper, $dumper);
277-
$contextDumper($name, $file, $line, $this->fileLinkFormat);
282+
$contextDumper($name, $file, $line, $this->fileLinkFormat, $label);
278283
} else {
279284
$cloner = new VarCloner();
280-
$dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
285+
$dumper->dump($cloner->cloneVar(('' !== $label ? $label.' in ' : '').$name.' on line '.$line.':'));
281286
}
282287
$dumper->dump($data);
283288
}

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DumpDataCollectorTest extends TestCase
2828
public function testDump()
2929
{
3030
$data = new Data([[123]]);
31+
$data = $data->withContext(['label' => 'foo']);
3132

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

@@ -49,11 +50,12 @@ public function testDump()
4950
'file' => __FILE__,
5051
'line' => $line,
5152
'fileExcerpt' => false,
53+
'label' => 'foo',
5254
],
5355
];
5456
$this->assertEquals($xDump, $dump);
5557

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

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

8385
public function testCollectDefault()

src/Symfony/Component/VarDumper/Cloner/Data.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ public function dump(DumperInterface $dumper)
271271
$cursor = new Cursor();
272272
$cursor->hashType = -1;
273273
$cursor->attr = $this->context[SourceContextProvider::class] ?? [];
274-
$dumper->dumpScalar($cursor, 'label', $this->context['label'] ?? '');
274+
$label = $this->context['label'] ?? '';
275+
276+
if ($cursor->attr || '' !== $label) {
277+
$dumper->dumpScalar($cursor, 'label', $label);
278+
}
275279
$cursor->hashType = 0;
276280
$this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);
277281
}

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|n
139139
$attr = $cursor->attr;
140140

141141
switch ($type) {
142-
case 'label': $style = 'label'; break;
143-
case 'default': $style = 'default'; break;
142+
case 'default':
143+
$style = 'default';
144+
break;
145+
146+
case 'label':
147+
$this->styles += ['label' => $this->styles['default']];
148+
$style = 'label';
149+
break;
144150

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

468474
$map = static::$controlCharsMap;
469475
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
470-
$endCchr = $this->colors ? "\033[m\033[{$this->styles['label' === $style ? 'default' : $style]}m" : '';
476+
$endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : '';
471477
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
472478
$s = $startCchr;
473479
$c = $c[$i = 0];
@@ -490,7 +496,7 @@ protected function style(string $style, string $value, array $attr = []): string
490496
if ($cchrCount && "\033" === $value[0]) {
491497
$value = substr($value, \strlen($startCchr));
492498
} else {
493-
$value = "\033[{$this->styles['label' === $style ? 'default' : $style]}m".$value;
499+
$value = "\033[{$this->styles[$style]}m".$value;
494500
}
495501
if ($cchrCount && str_ends_with($value, $endCchr)) {
496502
$value = substr($value, 0, -\strlen($endCchr));

src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ protected function style(string $style, string $value, array $attr = []): string
903903
}
904904

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

948951
return $v;
949952
}

0 commit comments

Comments
 (0)
0