From 8a8b5cdfafbadf7ad6c51407f931a0827cc36cab Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Tue, 16 May 2023 18:25:25 +0200 Subject: [PATCH] [VarDumper][DebugBundle] Fix dump labels compatibility --- .../Bundle/DebugBundle/DebugBundle.php | 13 ++++++++---- .../HttpKernel/EventListener/DumpListener.php | 5 ++++- .../Component/VarDumper/Cloner/Data.php | 15 ++++---------- .../Component/VarDumper/Dumper/CliDumper.php | 20 +++++++++++-------- .../Component/VarDumper/Dumper/HtmlDumper.php | 9 ++++++--- .../VarDumper/Resources/functions/dump.php | 2 +- .../VarDumper/Tests/Dumper/HtmlDumperTest.php | 2 +- 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Bundle/DebugBundle/DebugBundle.php b/src/Symfony/Bundle/DebugBundle/DebugBundle.php index a24321e22910d..9782bf8e39899 100644 --- a/src/Symfony/Bundle/DebugBundle/DebugBundle.php +++ b/src/Symfony/Bundle/DebugBundle/DebugBundle.php @@ -35,14 +35,19 @@ public function boot() // The dump data collector is used by default, so dump output is sent to // the WDT. In a CLI context, if dump is used too soon, the data collector // will buffer it, and release it at the end of the script. - VarDumper::setHandler(function ($var) use ($container) { + VarDumper::setHandler(function ($var, string $label = null) use ($container) { $dumper = $container->get('data_collector.dump'); $cloner = $container->get('var_dumper.cloner'); - $handler = function ($var) use ($dumper, $cloner) { - $dumper->dump($cloner->cloneVar($var)); + $handler = function ($var, string $label = null) use ($dumper, $cloner) { + $var = $cloner->cloneVar($var); + if (null !== $label) { + $var = $var->withContext(['label' => $label]); + } + + $dumper->dump($var); }; VarDumper::setHandler($handler); - $handler($var); + $handler($var, $label); }); } } diff --git a/src/Symfony/Component/HttpKernel/EventListener/DumpListener.php b/src/Symfony/Component/HttpKernel/EventListener/DumpListener.php index 4a4090915770e..b10bd37f439e5 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/DumpListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/DumpListener.php @@ -45,8 +45,11 @@ public function configure() $dumper = $this->dumper; $connection = $this->connection; - VarDumper::setHandler(static function ($var) use ($cloner, $dumper, $connection) { + VarDumper::setHandler(static function ($var, string $label = null) use ($cloner, $dumper, $connection) { $data = $cloner->cloneVar($var); + if (null !== $label) { + $data = $data->withContext(['label' => $label]); + } if (!$connection || !$connection->write($data)) { $dumper->dump($data); diff --git a/src/Symfony/Component/VarDumper/Cloner/Data.php b/src/Symfony/Component/VarDumper/Cloner/Data.php index 64b595d99287d..3bf0a09cb5594 100644 --- a/src/Symfony/Component/VarDumper/Cloner/Data.php +++ b/src/Symfony/Component/VarDumper/Cloner/Data.php @@ -269,17 +269,10 @@ public function dump(DumperInterface $dumper) { $refs = [0]; $cursor = new Cursor(); - $label = $this->context['label'] ?? ''; - - if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) { - $cursor->attr['if_links'] = true; - $cursor->hashType = -1; - $dumper->dumpScalar($cursor, 'default', $label.'^'); - $cursor->attr = ['if_links' => true]; - $dumper->dumpScalar($cursor, 'default', ' '); - $cursor->hashType = 0; - } - + $cursor->hashType = -1; + $cursor->attr = $this->context[SourceContextProvider::class] ?? []; + $dumper->dumpScalar($cursor, 'label', $this->context['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 c337bfed8d659..d9ce7ae4ed8fc 100644 --- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php @@ -139,9 +139,8 @@ public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|n $attr = $cursor->attr; switch ($type) { - case 'default': - $style = 'default'; - break; + case 'label': $style = 'label'; break; + case 'default': $style = 'default'; break; case 'integer': $style = 'num'; @@ -468,7 +467,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[$style]}m" : ''; + $endCchr = $this->colors ? "\033[m\033[{$this->styles['label' === $style ? 'default' : $style]}m" : ''; $value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) { $s = $startCchr; $c = $c[$i = 0]; @@ -487,11 +486,11 @@ protected function style(string $style, string $value, array $attr = []): string }, $value); } - if ($this->colors) { + if ($this->colors && '' !== $value) { if ($cchrCount && "\033" === $value[0]) { $value = substr($value, \strlen($startCchr)); } else { - $value = "\033[{$this->styles[$style]}m".$value; + $value = "\033[{$this->styles['label' === $style ? 'default' : $style]}m".$value; } if ($cchrCount && str_ends_with($value, $endCchr)) { $value = substr($value, 0, -\strlen($endCchr)); @@ -510,10 +509,15 @@ protected function style(string $style, string $value, array $attr = []): string } } if (isset($attr['href'])) { + if ('label' === $style) { + $value .= '^'; + } $value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\"; } - } elseif ($attr['if_links'] ?? false) { - return ''; + } + + if ('label' === $style && '' !== $value) { + $value .= ' '; } return $value; diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php index 61910dcd7a9ee..34ef644ad0837 100644 --- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php @@ -849,7 +849,7 @@ public function leaveHash(Cursor $cursor, int $type, string|int|null $class, boo protected function style(string $style, string $value, array $attr = []): string { - if ('' === $value) { + if ('' === $value && ('label' !== $style || !isset($attr['file']) && !isset($attr['href']))) { return ''; } @@ -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 = '\u{'.strtoupper(dechex(mb_ord($c[0]))).'}'; + return '\u{'.strtoupper(dechex(mb_ord($c[0]))).'}'; }, $v); } @@ -935,6 +935,9 @@ protected function style(string $style, string $value, array $attr = []): string $attr['href'] = $href; } if (isset($attr['href'])) { + if ('label' === $style) { + $v .= '^'; + } $target = isset($attr['file']) ? '' : ' target="_blank"'; $v = sprintf('%s', esc($this->utf8Encode($attr['href'])), $target, $v); } diff --git a/src/Symfony/Component/VarDumper/Resources/functions/dump.php b/src/Symfony/Component/VarDumper/Resources/functions/dump.php index 31613f206b880..d933bb7b10f62 100644 --- a/src/Symfony/Component/VarDumper/Resources/functions/dump.php +++ b/src/Symfony/Component/VarDumper/Resources/functions/dump.php @@ -25,7 +25,7 @@ function dump(mixed ...$vars): mixed return null; } - if (isset($vars[0]) && 1 === count($vars)) { + if (array_key_exists(0, $vars) && 1 === count($vars)) { VarDumper::dump($vars[0]); $k = 0; } else { diff --git a/src/Symfony/Component/VarDumper/Tests/Dumper/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/Dumper/HtmlDumperTest.php index 13d69f833195b..c31d07d2f26a5 100644 --- a/src/Symfony/Component/VarDumper/Tests/Dumper/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Dumper/HtmlDumperTest.php @@ -69,7 +69,7 @@ public function testGet() é\\x01test\\t\\n ing """ - "bo\\u{FEFF}m" => "te\\u{FEFF}st" + "bo\\u{FEFF}m" => "te\\u{FEFF}st" "[]" => [] "res" => stream resource @{$res} %A wrapper_type: "plainfile"