Skip to content

Commit

Permalink
feature #29235 [VarDumper] add support for links in CliDumper (nicola…
Browse files Browse the repository at this point in the history
…s-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[VarDumper] add support for links in CliDumper

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Same as #29168 for VarDumper:

![capture d ecran de 2018-11-15 20-10-08](https://user-images.githubusercontent.com/243674/48576196-24c04c00-e914-11e8-8a61-c1304c876243.png)

Thanks @ostrolucky for this nice discovery!

Commits
-------

e7cd44f [VarDumper] add support for links in CliDumper
  • Loading branch information
nicolas-grekas committed Dec 13, 2018
2 parents da4019a + e7cd44f commit 58c7ad4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\VarDumper\Dumper\CliDumper;

/**
* DebugExtension.
Expand Down Expand Up @@ -69,6 +70,14 @@ public function load(array $configs, ContainerBuilder $container)
->setClass(ServerDumpPlaceholderCommand::class)
;
}

if (method_exists(CliDumper::class, 'setDisplayOptions')) {
$container->getDefinition('var_dumper.cli_dumper')
->addMethodCall('setDisplayOptions', array(array(
'fileLinkFormat' => new Reference('debug.file_link_formatter', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE),
)))
;
}
}

/**
Expand Down
Expand Up @@ -113,6 +113,9 @@ public function collect(Request $request, Response $response, \Exception $except
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
} else {
$dumper = new CliDumper('php://output', $this->charset);
if (method_exists($dumper, 'setDisplayOptions')) {
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
}
}

foreach ($this->data as $dump) {
Expand Down Expand Up @@ -215,6 +218,9 @@ public function __destruct()
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
} else {
$dumper = new CliDumper('php://output', $this->charset);
if (method_exists($dumper, 'setDisplayOptions')) {
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
}
}

foreach ($this->data as $i => $dump) {
Expand Down
39 changes: 38 additions & 1 deletion src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Expand Up @@ -55,6 +55,10 @@ class CliDumper extends AbstractDumper
protected $collapseNextHash = false;
protected $expandNextHash = false;

private $displayOptions = array(
'fileLinkFormat' => null,
);

/**
* {@inheritdoc}
*/
Expand All @@ -76,6 +80,8 @@ public function __construct($output = null, string $charset = null, int $flags =
'index' => '34',
));
}

$this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f';
}

/**
Expand Down Expand Up @@ -108,6 +114,16 @@ public function setStyles(array $styles)
$this->styles = $styles + $this->styles;
}

/**
* Configures display options.
*
* @param array $displayOptions A map of display options to customize the behavior
*/
public function setDisplayOptions(array $displayOptions)
{
$this->displayOptions = $displayOptions + $this->displayOptions;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -427,7 +443,9 @@ protected function style($style, $value, $attr = array())
$value = substr($value, -$attr['ellipsis']);
}

return $this->style('default', $prefix).$this->style($style, $value);
$value = $this->style('default', $prefix).$this->style($style, $value);

goto href;
}

$style = $this->styles[$style];
Expand Down Expand Up @@ -458,6 +476,16 @@ protected function style($style, $value, $attr = array())
}
}

href:
if ($this->colors) {
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], isset($attr['line']) ? $attr['line'] : 0)) {
$attr['href'] = $href;
}
if (isset($attr['href'])) {
$value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\";
}
}

return $value;
}

Expand Down Expand Up @@ -594,4 +622,13 @@ private function isWindowsTrueColor()

return $result;
}

private function getSourceLink($file, $line)
{
if ($fmt = $this->displayOptions['fileLinkFormat']) {
return \is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line);
}

return false;
}
}

0 comments on commit 58c7ad4

Please sign in to comment.