8000 [VarDumper][HttpKernel] Add context to all dumps · symfony/symfony@2f49e78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f49e78

Browse files
committed
[VarDumper][HttpKernel] Add context to all dumps
1 parent 373dad3 commit 2f49e78

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/Symfony/Component/HttpKernel/EventListener/DumpListener.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Console\ConsoleEvents;
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1616
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
17+
use Symfony\Component\VarDumper\Dumper\CliDumper;
18+
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
1719
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
1820
use Symfony\Component\VarDumper\Server\Connection;
1921
use Symfony\Component\VarDumper\VarDumper;
@@ -46,6 +48,20 @@ public function configure()
4648
$data = $cloner->cloneVar($var);
4749

4850
if (!$connection || !$connection->write($data)) {
51+
list('name' => $name, 'file' => $file, 'line' => $line) = (new SourceContextProvider())->getContext();
52+
if (\is_string($file)) {
53+
$name = $file;
54+
}
55+
56+
if ($dumper instanceof CliDumper) {
57+
(function (string $name, int $line) {
58+
$this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
59+
$this->dumpLine(0);
60+
})->bindTo($dumper, $dumper)($name, $line);
61+
} else {
62+
$dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
63+
}
64+
4965
$dumper->dump($data);
5066
}
5167
});

src/Symfony/Component/HttpKernel/Tests/EventListener/DumpListenerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public function testConfigure()
5151

5252
VarDumper::dump('foo');
5353
VarDumper::dump('bar');
54-
55-
$this->assertSame('+foo-+bar-', ob_get_clean());
54+
$this->assertSame('+'.__FILE__.' on line '.(__LINE__ - 2).':-+foo-+'.__FILE__.' on line '.(__LINE__ - 1).':-+bar-', ob_get_clean());
5655
} catch (\Exception $exception) {
5756
}
5857

src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\VarDumper\Cloner\VarCloner;
1515
use Symfony\Component\VarDumper\Dumper\CliDumper;
16+
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
1617
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
1718

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

3940
self::$handler = function ($var) use ($cloner, $dumper) {
41+
(function () {
42+
list('name' => $name, 'file' => $file, 'line' => $line) = (new SourceContextProvider())->getContext();
43+
44+
$attr = array();
45+
if ($this instanceof HtmlDumper) {
46+
if (\is_string($file)) {
47+
$attr = array(
48+
'file' => $file,
49+
'line' => $line,
50+
'title' => $file,
51+
);
52+
}
53+
} else {
54+
$name = $file;
55+
}
56+
57+
$this->line = $this->style('meta', $name, $attr).' on line '.$this->style('meta', $line).':';
58+
$this->dumpLine(0);
59+
})->bindTo($dumper, $dumper)();
60+
4061
$dumper->dump($cloner->cloneVar($var));
4162
};
4263
}

0 commit comments

Comments
 (0)
0