Skip to content

[VarDumper] Add LinkStub to create links in HTML dumps #19816

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

Merged
merged 1 commit into from
Sep 1, 2016
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -912,11 +912,6 @@ table.logs .sf-call-stack abbr {
#collector-content .sf-dump .trace li.selected {
background: rgba(255, 255, 153, 0.5);
}
#collector-content .sf-dump-expanded code { color: #222; }
#collector-content .sf-dump-expanded code .sf-dump-const {
background: rgba(255, 255, 153, 0.5);
font-weight: normal;
}

{# Search Results page
========================================================================= #}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Caster/DOMCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function castNode(\DOMNode $dom, array $a, Stub $stub, $isNested)
'namespaceURI' => $dom->namespaceURI,
'prefix' => $dom->prefix,
'localName' => $dom->localName,
'baseURI' => $dom->baseURI,
'baseURI' => $dom->baseURI ? new LinkStub($dom->baseURI) : $dom->baseURI,
'textContent' => new CutStub($dom->textContent),
);

Expand Down Expand Up @@ -144,7 +144,7 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $is
'version' => $dom->version,
'xmlVersion' => $dom->xmlVersion,
'strictErrorChecking' => $dom->strictErrorChecking,
'documentURI' => $dom->documentURI,
'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
'config' => $dom->config,
'formatOutput' => $dom->formatOutput,
'validateOnParse' => $dom->validateOnParse,
Expand Down Expand Up @@ -237,7 +237,7 @@ public static function castLocator(\DOMLocator $dom, array $a, Stub $stub, $isNe
'columnNumber' => $dom->columnNumber,
'offset' => $dom->offset,
'relatedNode' => $dom->relatedNode,
'uri' => $dom->uri,
'uri' => $dom->uri ? new LinkStub($dom->uri, $dom->lineNumber) : $dom->uri,
);

return $a;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
}
unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);

$a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);

return $a;
}

Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/LinkStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

/**
* Represents a file or a URL.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class LinkStub extends ConstStub
{
public function __construct($file, $line = 0)
{
$this->value = $file;

if (is_string($file)) {
$this->type = self::TYPE_STRING;
$this->class = preg_match('//u', $file) ? self::STRING_UTF8 : self::STRING_BINARY;

if (0 === strpos($file, 'file://')) {
$file = substr($file, 7);
} elseif (false !== strpos($file, '://')) {
$this->attr['href'] = $file;

return;
}
if (file_exists($file)) {
if ($line) {
$this->attr['line'] = $line;
}
$this->attr['file'] = realpath($file);

if ($this->attr['file'] === $file) {
$ellipsis = explode(DIRECTORY_SEPARATOR, $file);
$this->attr['ellipsis'] = 3 < count($ellipsis) ? 2 + strlen(implode(array_slice($ellipsis, -2))) : 0;
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
}

if ($f = $c->getFileName()) {
$a[$prefix.'file'] = $f;
$a[$prefix.'file'] = new LinkStub($f, $c->getStartLine());
$a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
}

Expand Down Expand Up @@ -287,7 +287,7 @@ private static function addExtra(&$a, \Reflector $c)
$x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();

if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
$x['file'] = $m;
$x['file'] = new LinkStub($m, $c->getStartLine());
$x['line'] = $c->getStartLine().' to '.$c->getEndLine();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/VarDumper/Caster/ResourceCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public static function castProcess($process, array $a, Stub $stub, $isNested)

public static function castStream($stream, array $a, Stub $stub, $isNested)
{
return stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
$a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
$a['uri'] = new LinkStub($a['uri']);

return $a;
}

public static function castStreamContext($stream, array $a, Stub $stub, $isNested)
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/SplCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNe
}
}

if (isset($a[$prefix.'realPath'])) {
$a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
}

if (isset($a[$prefix.'perms'])) {
$a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $
'attributeCount' => $reader->attributeCount,
'value' => $reader->value,
'namespaceURI' => $reader->namespaceURI,
'baseURI' => $reader->baseURI,
'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
$props => array(
'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD),
'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS),
Expand Down
59 changes: 33 additions & 26 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class HtmlDumper extends CliDumper
'meta' => 'color:#B729D9',
'key' => 'color:#56DB3A',
'index' => 'color:#1299DA',
'expanded code.hljs' => 'display:inline; padding:0; background:none',
);

private $displayOptions = array(
Expand Down Expand Up @@ -190,14 +189,18 @@ function toggle(a, recursive) {
options = {$options},
elt = root.getElementsByTagName('A'),
len = elt.length,
i = 0, s, h,
i = 0, s, h, fmt,
t = [];

while (i < len) t.push(elt[i++]);

for (i in x) {
options[i] = x[i];
}
fmt = options.fileLinkFormat;
if (fmt && 'string' == typeof fmt) {
fmt = [fmt];
}

function a(e, f) {
addEventListener(root, e, function (e) {
Expand All @@ -218,20 +221,6 @@ function isCtrlKey(e) {
refStyle.innerHTML = '';
}
});
if (options.fileLinkFormat) {
addEventListener(root, 'click', function (e) {
e = e.target;
while (root != e && 'CODE' != e.tagName) {
e = e.parentNode;
}
if ('CODE' == e.tagName) {
var f = e.getAttribute('data-file'), l = e.getAttribute('data-line');
if (f && l) {
location.href = options.fileLinkFormat.replace('%f', f).replace('%l', l);
}
}
});
}
a('mouseover', function (a) {
if (a = idRx.exec(a.className)) {
try {
Expand Down Expand Up @@ -332,6 +321,16 @@ function isCtrlKey(e) {
}
}
}
} else if (fmt && (a = elt.getAttribute('data-file'))) {
if (fmt[1]) {
for (x in fmt[1]) {
if (0 === a.indexOf(x)) {
a = fmt[1][x] + a.substr(x.length);
break;
}
}
}
elt.href = fmt[0].replace('%l', elt.getAttribute('data-line')).replace('%f', a);
}
}

Expand Down Expand Up @@ -387,6 +386,7 @@ function isCtrlKey(e) {
cursor: pointer;
border: 0;
outline: none;
color: inherit;
}
pre.sf-dump .sf-dump-ellipsis {
display: inline-block;
Expand All @@ -397,6 +397,11 @@ function isCtrlKey(e) {
overflow: hidden;
vertical-align: top;
}
pre.sf-dump code {
display:inline;
padding:0;
background:none;
}
.sf-dump-str-collapse .sf-dump-str-collapse {
display: none;
}
Expand Down Expand Up @@ -480,14 +485,15 @@ protected function style($style, $value, $attr = array())
} elseif ('private' === $style) {
$style .= sprintf(' title="Private property defined in class:&#10;`%s`"', esc($attr['class']));
}
$map = static::$controlCharsMap;
$style = "<span class=sf-dump-{$style}>";

if (isset($attr['ellipsis'])) {
$label = esc(substr($value, -$attr['ellipsis']));

return sprintf('<span class=sf-dump-%s><abbr title="%s" class=sf-dump-ellipsis>%2$s</abbr>%s</span>', $style, substr($v, 0, -strlen($label)), $label);
$v = sprintf('</span>%s<abbr title="%s" class=sf-dump-ellipsis>%2$s</abbr>%s</span>%1$s', $style, substr($v, 0, -strlen($label)), $label);
}

$map = static::$controlCharsMap;
$style = "<span class=sf-dump-{$style}>";
$v = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $style) {
$s = '</span>';
$c = $c[$i = 0];
Expand All @@ -498,22 +504,23 @@ protected function style($style, $value, $attr = array())
return $s.$style;
}, $v, -1, $cchrCount);

if ($cchrCount && '<' === $v[0]) {
if ('<' === $v[0]) {
$v = substr($v, 7);
} else {
$v = $style.$v;
}
if ($cchrCount && '>' === substr($v, -1)) {
if ('>' === substr($v, -1)) {
$v = substr($v, 0, -strlen($style));
} else {
$v .= '</span>';
}
if (isset($attr['file'])) {
$v = sprintf('<a data-file="%s" data-line="%d">%s</a>', esc($attr['file']), isset($attr['line']) ? $attr['line'] : 1, $v);
} elseif (isset($attr['href'])) {
$v = sprintf('<a href="%s">%s</a>', esc($attr['href']), $v);
}
if (isset($attr['lang'])) {
if (isset($attr['file'], $attr['line'])) {
$v = sprintf('<code class="%s" data-file="%s" data-line="%d">%s</code>', esc($attr['lang']), esc($attr['file']), $attr['line'], $v);
} else {
$v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
}
$v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
}

return $v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testHtmlDump()
<foo></foo><bar><span class=sf-dump-note>Exception</span> {<samp>
#<span class=sf-dump-protected title="Protected property">message</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
#<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
#<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%d characters">%sExceptionCasterTest.php</span>"
#<span class=sf-dump-protected title="Protected property">file</span>: "<a data-file="%sExceptionCasterTest.php" data-line="25"><span class=sf-dump-str title="%d characters"><abbr title="%sTests" class=sf-dump-ellipsis>%sTests</abbr>%eCaster%eExceptionCasterTest.php</span></a>"
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>25</span>
-<span class=sf-dump-private title="Private property defined in class:&#10;`Exception`">trace</span>: {<samp>
<span class=sf-dump-meta title="Stack level %d."><abbr title="%sVarDumper%eTests" class=sf-dump-ellipsis>%sVarDumper%eTests</abbr>%eCaster%eExceptionCasterTest.php</span>: <span class=sf-dump-num>25</span>
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testGet()
<span class=sf-dump-meta>default</span>: <span class=sf-dump-const>null</span>
</samp>}
</samp>}
<span class=sf-dump-meta>file</span>: "<span class=sf-dump-str title="%d characters">{$var['file']}</span>"
<span class=sf-dump-meta>file</span>: "<a data-file="{$var['file']}" data-line="24"><span class=sf-dump-str title="%d characters"><abbr title="%sTests" class=sf-dump-ellipsis>%sTests</abbr>%eFixtures%edumb-var.php</span></a>"
<span class=sf-dump-meta>line</span>: "<span class=sf-dump-str title="%d characters">{$var['line']} to {$var['line']}</span>"
</samp>}
"<span class=sf-dump-key>line</span>" => <span class=sf-dump-num>{$var['line']}</span>
Expand Down