Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7cc20a

Browse files
committed
POC adding of href link to the dump call location
1 parent 79814a2 commit c7cc20a

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
namespace Symfony\Component\VarDumper\Dumper;
1313

1414
use Symfony\Component\VarDumper\Cloner\Cursor;
15+
use Symfony\Component\VarDumper\Cloner\Data;
1516
use Symfony\Component\VarDumper\Cloner\Stub;
17+
use Symfony\Component\VarDumper\Cloner\VarCloner;
18+
use Symfony\Component\VarDumper\VarDumper;
1619

1720
/**
1821
* CliDumper dumps variables for command line output.
@@ -61,6 +64,8 @@ class CliDumper extends AbstractDumper
6164

6265
private $handlesHrefGracefully;
6366

67+
protected $attr = [];
68+
6469
/**
6570
* {@inheritdoc}
6671
*/
@@ -126,6 +131,37 @@ public function setDisplayOptions(array $displayOptions)
126131
$this->displayOptions = $displayOptions + $this->displayOptions;
127132
}
128133

134+
/**
135+
* {@inheritdoc}
136+
*/
137+
public function dump(Data $data, $output = null, array $extraDisplayOptions = [])
138+
{
139+
$this->extraDisplayOptions = $extraDisplayOptions;
140+
$result = parent::dump($data, $output);
141+
142+
return $result;
143+
}
144+
145+
/**
146+
* Configures file of the dump call function.
147+
*
148+
* @param string $file File path issued from debug_backtrace
149+
*/
150+
public function setFile(string $file)
151+
{
152+
$this->attr['file'] = $file;
153+
}
154+
155+
/**
156+
* Configures line of the dump call function.
157+
*
158+
* @param int $line File line issued from debug_backtrace
159+
*/
160+
public function setLine(int $line)
161+
{
162+
$this->attr['line'] = $line;
163+
}
164+
129165
/**
130166
* {@inheritdoc}
131167
*/
@@ -134,7 +170,7 @@ public function dumpScalar(Cursor $cursor, $type, $value)
134170
$this->dumpKey($cursor);
135171

136172
$style = 'const';
137-
$attr = $cursor->attr;
173+
$attr = $this->attr + $cursor->attr;
138174

139175
switch ($type) {
140176
case 'default':
@@ -186,7 +222,7 @@ public function dumpScalar(Cursor $cursor, $type, $value)
186222
public function dumpString(Cursor $cursor, $str, $bin, $cut)
187223
{
188224
$this->dumpKey($cursor);
189-
$attr = $cursor->attr;
225+
$attr = $this->attr + $cursor->attr;
190226

191227
if ($bin) {
192228
$str = $this->utf8Encode($str);
@@ -274,7 +310,7 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
274310
public function enterHash(Cursor $cursor, $type, $class, $hasChild)
275311
{
276312
$this->dumpKey($cursor);
277-
$attr = $cursor->attr;
313+
$attr = $this->attr + $cursor->attr;
278314

279315
if ($this->collapseNextHash) {
280316
$cursor->skipChildren = true;
@@ -491,7 +527,7 @@ protected function style($style, $value, $attr = [])
491527
}
492528
}
493529
if (isset($attr['href'])) {
494-
$value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\";
530+
$value .= " \033]8;;{$attr['href']}\033\\[^]\033]8;;\033\\";
495531
}
496532
}
497533

src/Symfony/Component/VarDumper/Resources/functions/dump.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
*/
1818
function dump($var, ...$moreVars)
1919
{
20-
VarDumper::dump($var);
20+
$backtrace = debug_backtrace();
21+
VarDumper::dump($var, $backtrace);
2122

2223
foreach ($moreVars as $v) {
23-
VarDumper::dump($v);
24+
VarDumper::dump($v, $backtrace);
2425
}
2526

2627
if (1 < func_num_args()) {
@@ -34,8 +35,9 @@ function dump($var, ...$moreVars)
3435
if (!function_exists('dd')) {
3536
function dd(...$vars)
3637
{
38+
$backtrace = debug_backtrace();
3739
foreach ($vars as $v) {
38-
VarDumper::dump($v);
40+
VarDumper::dump($v, $backtrace);
3941
}
4042

4143
die(1);

src/Symfony/Component/VarDumper/Tests/Caster/IntlCasterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testCastNumberFormatter()
4040

4141
$expectedLocale = $var->getLocale();
4242
$expectedPattern = $var->getPattern();
43+
dump("Here is a dummy dump.");
4344

4445
$expectedAttribute1 = $var->getAttribute(\NumberFormatter::PARSE_INT_ONLY);
4546
$expectedAttribute2 = $var->getAttribute(\NumberFormatter::GROUPING_USED);

src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class VarDumper
2626
{
2727
private static $handler;
2828

29-
public static function dump($var)
29+
public static function dump($var, $backtrace = null)
3030
{
3131
if (null === self::$handler) {
3232
$cloner = new VarCloner();
@@ -35,10 +35,15 @@ public static function dump($var)
3535
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
3636
$dumper = 'html' === $_SERVER['VAR_DUMPER_FORMAT'] ? new HtmlDumper() : new CliDumper();
3737
} else {
38-
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper() : new HtmlDumper();
38+
$dumper = in_array(\PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper() : new HtmlDumper();
3939
}
4040

41-
self::$handler = function ($var) use ($cloner, $dumper) {
41+
self::$handler = function ($var) use ($cloner, $dumper, $backtrace) {
42+
if ($dumper instanceof CliDumper && null !== $backtrace) {
43+
$caller = array_shift($backtrace);
44+
$dumper->setFile($caller['file']);
45+
$dumper->setLine($caller['line']);
46+
}
4247
$dumper->dump($cloner->cloneVar($var));
4348
};
4449
}

0 commit comments

Comments
 (0)