From 834accf025b633ca870c257edb5cab68851589d5 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 23 May 2023 10:33:52 +0200 Subject: [PATCH] [VarDumper][HttpKernel] Fix dumping with labels --- Cloner/Data.php | 6 +++++- Dumper/CliDumper.php | 14 ++++++++++---- Dumper/HtmlDumper.php | 5 ++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cloner/Data.php b/Cloner/Data.php index 3bf0a09c..928f72d7 100644 --- a/Cloner/Data.php +++ b/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/Dumper/CliDumper.php b/Dumper/CliDumper.php index d9ce7ae4..c155d4c7 100644 --- a/Dumper/CliDumper.php +++ b/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/Dumper/HtmlDumper.php b/Dumper/HtmlDumper.php index 34ef644a..345a89cf 100644 --- a/Dumper/HtmlDumper.php +++ b/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; }