Skip to content

Commit 81fb69e

Browse files
bug #50397 [HttpKernel][VarDumper] Fix dumping with labels (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpKernel][VarDumper] Fix dumping with labels | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Should fix symfony/symfony#50347 (comment) /cc `@bobthecow` Also improves the display in the WDT: ![image](https://github.com/symfony/symfony/assets/243674/956f7c7d-5569-4ea2-97f3-01d000f34532) ![image](https://github.com/symfony/symfony/assets/243674/6a497d1e-5291-45a7-8af3-1cf11cafded6) Commits ------- e824eb051e [VarDumper][HttpKernel] Fix dumping with labels
2 parents 5008fa8 + 834accf commit 81fb69e

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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
}

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));

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)