+ {% if dump.label is defined and '' != dump.label %}
+ {{ dump.label }} in
+ {% endif %}
{% if dump.file %}
{% set link = dump.file|file_link(dump.line) %}
{% if link %}
@@ -45,7 +48,12 @@
{% for dump in collector.getDumps('html') %}
- In
+
+ {% if dump.label is defined and '' != dump.label %}
+ {{ dump.label }} in
+ {% else %}
+ In
+ {% endif %}
{% if dump.line %}
{% set link = dump.file|file_link(dump.line) %}
{% if link %}
diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php
index dc7aae68cc62e..d52b02b773abe 100644
--- a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php
+++ b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php
@@ -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');
@@ -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'] ?? '');
}
}
}
@@ -242,7 +245,7 @@ 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 = [];
@@ -250,10 +253,12 @@ public function __destruct()
}
}
- 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');
@@ -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);
}
diff --git a/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php b/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php
index 106420c8e3a1f..eaee994a9e3bf 100644
--- a/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php
+++ b/src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php
@@ -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([]));
@@ -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);
@@ -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()
diff --git a/src/Symfony/Component/VarDumper/Cloner/Data.php b/src/Symfony/Component/VarDumper/Cloner/Data.php
index 3bf0a09cb5594..928f72d7a850e 100644
--- a/src/Symfony/Component/VarDumper/Cloner/Data.php
+++ b/src/Symfony/Component/VarDumper/Cloner/Data.php
@@ -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]);
}
diff --git a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
index d9ce7ae4ed8fc..c155d4c79a7cb 100644
--- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
+++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
@@ -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';
@@ -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];
@@ -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));
diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
index 34ef644ad0837..345a89cf726e0 100644
--- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
+++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
@@ -903,7 +903,7 @@ protected function style(string $style, string $value, array $attr = []): string
}
$map = static::$controlCharsMap;
- $v = ''.preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
+ $v = "".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
$s = $b = '%s', esc($attr['lang']), $v);
}
+ if ('label' === $style) {
+ $v .= ' ';
+ }
return $v;
}