Skip to content

Commit 47bffc0

Browse files
[VarDumper] add support for links in CliDumper
1 parent dbf053b commit 47bffc0

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/Symfony/Bundle/DebugBundle/DependencyInjection/DebugExtension.php

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Extension\Extension;
1818
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1919
use Symfony\Component\DependencyInjection\Reference;
20+
use Symfony\Component\VarDumper\Dumper\CliDumper;
2021

2122
/**
2223
* DebugExtension.
@@ -69,6 +70,14 @@ public function load(array $configs, ContainerBuilder $container)
6970
->setClass(ServerDumpPlaceholderCommand::class)
7071
;
7172
}
73+
74+
if (method_exists(CliDumper::class, 'setDisplayOptions')) {
75+
$container->getDefinition('var_dumper.cli_dumper')
76+
->addMethodCall('setDisplayOptions', array(array(
77+
'fileLinkFormat' => new Reference('debug.file_link_formatter', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE),
78+
)))
79+
;
80+
}
7281
}
7382

7483
/**

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public function collect(Request $request, Response $response, \Exception $except
113113
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
114114
} else {
115115
$dumper = new CliDumper('php://output', $this->charset);
116+
if (method_exists($dumper, 'setDisplayOptions')) {
117+
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
118+
}
116119
}
117120

118121
foreach ($this->data as $dump) {
@@ -215,6 +218,9 @@ public function __destruct()
215218
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
216219
} else {
217220
$dumper = new CliDumper('php://output', $this->charset);
221+
if (method_exists($dumper, 'setDisplayOptions')) {
222+
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
223+
}
218224
}
219225

220226
foreach ($this->data as $i => $dump) {

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class CliDumper extends AbstractDumper
5555
protected $collapseNextHash = false;
5656
protected $expandNextHash = false;
5757

58+
private $displayOptions = array(
59+
'fileLinkFormat' => null,
60+
);
61+
5862
/**
5963
* {@inheritdoc}
6064
*/
@@ -76,6 +80,8 @@ public function __construct($output = null, string $charset = null, int $flags =
7680
'index' => '34',
7781
));
7882
}
83+
84+
$this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f';
7985
}
8086

8187
/**
@@ -108,6 +114,16 @@ public function setStyles(array $styles)
108114
$this->styles = $styles + $this->styles;
109115
}
110116

117+
/**
118+
* Configures display options.
119+
*
120+
* @param array $displayOptions A map of display options to customize the behavior
121+
*/
122+
public function setDisplayOptions(array $displayOptions)
123+
{
124+
$this->displayOptions = $displayOptions + $this->displayOptions;
125+
}
126+
111127
/**
112128
* {@inheritdoc}
113129
*/
@@ -427,7 +443,9 @@ protected function style($style, $value, $attr = array())
427443
$value = substr($value, -$attr['ellipsis']);
428444
}
429445

430-
return $this->style('default', $prefix).$this->style($style, $value);
446+
$value = $this->style('default', $prefix).$this->style($style, $value);
447+
448+
goto href;
431449
}
432450

433451
$style = $this->styles[$style];
@@ -458,6 +476,16 @@ protected function style($style, $value, $attr = array())
458476
}
459477
}
460478

479+
href:
480+
if ($this->colors) {
481+
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], isset($attr['line']) ? $attr['line'] : 0)) {
482+
$attr['href'] = $href;
483+
}
484+
if (isset($attr['href'])) {
485+
$value = "\033]8;;{$attr['href']}\007{$value}\033]8;;\007";
486+
}
487+
}
488+
461489
return $value;
462490
}
463491

@@ -594,4 +622,13 @@ private function isWindowsTrueColor()
594622

595623
return $result;
596624
}
625+
626+
private function getSourceLink($file, $line)
627+
{
628+
if ($fmt = $this->displayOptions['fileLinkFormat']) {
629+
return \is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line);
630+
}
631+
632+
return false;
633+
}
597634
}

0 commit comments

Comments
 (0)