Skip to content

Commit

Permalink
[VarDumper][HttpKernel] Fix dumping with labels
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed May 25, 2023
1 parent a93ec17 commit 834accf
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 834accf

Please sign in to comment.