8000 [VarDumper][HttpKernel] Add context to all dumps by fancyweb · Pull Request #28395 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper][HttpKernel] Add context to all dumps #28395

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

Closed
Closed
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
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpKernel/EventListener/DumpListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use Symfony\Component\VarDumper\Server\Connection;
use Symfony\Component\VarDumper\VarDumper;
Expand Down Expand Up @@ -46,6 +48,20 @@ public function configure()
$data = $cloner->cloneVar($var);

if (!$connection || !$connection->write($data)) {
list('name' => $name, 'file' => $file, 'line' => $line) = (new SourceContextProvider())->getContext();
if (\is_string($file)) {
$name = $file;
}

if ($dumper instanceof CliDumper) {
(function (string $name, int $line) {
$this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
$this->dumpLine(0);
})->bindTo($dumper, $dumper)($name, $line);
} else {
$dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
}

$dumper->dump($data);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public function testConfigure()

VarDumper::dump('foo');
VarDumper::dump('bar');

$this->assertSame('+foo-+bar-', ob_get_clean());
$this->assertSame('+'.__FILE__.' on line '.(__LINE__ - 2).':-+foo-+'.__FILE__.' on line '.(__LINE__ - 1).':-+bar-', ob_get_clean());
} catch (\Exception $exception) {
}

Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

// Load the global dump() function
Expand All @@ -37,6 +38,26 @@ public static function dump($var)
}

self::$handler = function ($var) use ($cloner, $dumper) {
(function () {
list('name' => $name, 'file' => $file, 'line' => $line) = (new SourceContextProvider())->getContext();

$attr = array();
if ($this instanceof HtmlDumper) {
if (\is_string($file)) {
$attr = array(
'file' => $file,
'line' => $line,
'title' => $file,
);
}
} else {
$name = $file;
}

$this->line = $this->style('meta', $name, $attr).' on line '.$this->style('meta', $line).':';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this scenario, we are 100% sure that the dumper is an instance of CliDumper so those methods are safe to use.

$this->dumpLine(0);
})->bindTo($dumper, $dumper)();

$dumper->dump($cloner->cloneVar($var));
};
}
Expand Down
0