8000 [DebugBundle][VarDumper] Fix dump labels compatibility by fancyweb · Pull Request #50347 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DebugBundle][VarDumper] Fix dump labels compatibility #50347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Symfony/Bundle/DebugBundle/DebugBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 4 additions & 11 deletions src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
20 changes: 12 additions & 8 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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];
Expand All @@ -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));
Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}

Expand Down Expand Up @@ -903,7 +903,7 @@ protected function style(string $style, string $value, array $attr = []): string
}

$map = static::$controlCharsMap;
$v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
$v = '<span class=sf-dump-'.('label' === $style ? 'default' : $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 All @@ -927,14 +927,17 @@ protected function style(string $style, string $value, array $attr = []): string

if (!($attr['binary'] ?? false)) {
$v = preg_replace_callback(static::$unicodeCharsRx, function ($c) {
return '<span class="sf-dump-default">\u{'.strtoupper(dechex(mb_ord($c[0]))).'}</span>';
return '<span class=sf-dump-default>\u{'.strtoupper(dechex(mb_ord($c[0]))).'}</span>';
}, $v);
}

if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) {
$attr['href'] = $href;
}
if (isset($attr['href'])) {
if ('label' === $style) {
$v .= '^';
}
$target = isset($attr['file']) ? '' : ' target="_blank"';
$v = sprintf('<a href="%s"%s rel="noopener noreferrer">%s</a>', esc($this->utf8Encode($attr['href'])), $target, $v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix dump(null) showing 1^ null

VarDumper::dump($vars[0]);
$k = 0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testGet()
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">&#233;<span class="sf-dump-default">\\x01</span>test<span class="sf-dump-default">\\t</span><span class="sf-dump-default sf-dump-ns">\\n</span></span>
<span class=sf-dump-str title="11 binary or non-UTF-8 characters">ing</span>
"""
"<span class=sf-dump-key>bo<span class="sf-dump-default">\\u{FEFF}</span>m</span>" => "<span class=sf-dump-str title="5 characters">te<span class="sf-dump-default">\\u{FEFF}</span>st</span>"
"<span class=sf-dump-key>bo<span class=sf-dump-default>\\u{FEFF}</span>m</span>" => "<span class=sf-dump-str title="5 characters">te<span class=sf-dump-default>\\u{FEFF}</span>st</span>"
"<span class=sf-dump-key>[]</span>" => []
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp data-depth=2 class=sf-dump-compact>
%A <span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
Expand Down
0