Skip to content

Commit

Permalink
bug #50397 [HttpKernel][VarDumper] Fix dumping with labels (nicolas-g…
Browse files Browse the repository at this point in the history
…rekas)

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
  • Loading branch information
nicolas-grekas committed May 25, 2023
2 parents 5008fa8 + 834accf commit 81fb69e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion 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 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 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 comments on commit 81fb69e

Please sign in to comment.