8000 [VarDumper] Add LinkStub to create links in HTML dumps · symfony/symfony@2b5d642 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b5d642

Browse files
[VarDumper] Add LinkStub to create links in HTML dumps
1 parent 03a824a commit 2b5d642

File tree

11 files changed

+100
-40
lines changed

11 files changed

+100
-40
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,11 +912,6 @@ table.logs .sf-call-stack abbr {
912912
#collector-content .sf-dump .trace li.selected {
913913
background: rgba(255, 255, 153, 0.5);
914914
}
915-
#collector-content .sf-dump-expanded code { color: #222; }
916-
#collector-content .sf-dump-expanded code .sf-dump-const {
917-
background: rgba(255, 255, 153, 0.5);
918-
font-weight: normal;
919-
}
920915

921916
{# Search Results page
922917
========================================================================= #}

src/Symfony/Component/VarDumper/Caster/DOMCaster.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static function castNode(\DOMNode $dom, array $a, Stub $stub, $isNested)
107107
'namespaceURI' => $dom->namespaceURI,
108108
'prefix' => $dom->prefix,
109109
'localName' => $dom->localName,
110-
'baseURI' => $dom->baseURI,
110+
'baseURI' => $dom->baseURI ? new LinkStub($dom->baseURI) : $dom->baseURI,
111111
'textContent' => new CutStub($dom->textContent),
112112
);
113113

@@ -144,7 +144,7 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $is
144144
'version' => $dom->version,
145145
'xmlVersion' => $dom->xmlVersion,
146146
'strictErrorChecking' => $dom->strictErrorChecking,
147-
'documentURI' => $dom->documentURI,
147+
'documentURI' => $dom->documentURI ? new LinkStub($dom->documentURI) : $dom->documentURI,
148148
'config' => $dom->config,
149149
'formatOutput' => $dom->formatOutput,
150150
'validateOnParse' => $dom->validateOnParse,
@@ -237,7 +237,7 @@ public static function castLocator(\DOMLocator $dom, array $a, Stub $stub, $isNe
237237
'columnNumber' => $dom->columnNumber,
238238
'offset' => $dom->offset,
239239
'relatedNode' => $dom->relatedNode,
240-
'uri' => $dom->uri,
240+
'uri' => $dom->uri ? new LinkStub($dom->uri, $dom->lineNumber) : $dom->uri,
241241
);
242242

243243
return $a;

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
221221
}
222222
unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
223223

224+
$a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
225+
224226
return $a;
225227
}
226228

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
/**
15+
* Represents a file or a URL.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class LinkStub extends ConstStub
20+
{
21+
public function __construct($file, $line = 0)
22+
{
23+
$this->value = $file;
24+
25+
if (is_string($file)) {
26+
$this->type = self::TYPE_STRING;
27+
$this->class = preg_match('//u', $file) ? self::STRING_UTF8 : self::STRING_BINARY;
28+
29+
if (0 === strpos($file, 'file://')) {
30+
$file = substr($file, 7);
31+
} elseif (false !== strpos($file, '://')) {
32+
$this->attr['href'] = $file;
33+
34+
return;
35+
}
36+
if (file_exists($file)) {
37+
if ($line) {
38+
$this->attr['line'] = $line;
39+
}
40+
$this->attr['file'] = realpath($file);
41+
42+
if ($this->attr['file'] === $file) {
43+
$ellipsis = explode(DIRECTORY_SEPARATOR, $file);
44+
$this->attr['ellipsis'] = 3 < count($ellipsis) ? 2 + strlen(implode(array_slice($ellipsis, -2))) : 0;
45+
}
46+
}
47+
}
48+
}
49+
}

src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
5353
}
5454

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

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

289289
if (method_exis F987 ts($c, 'getFileName') && $m = $c->getFileName()) {
290-
$x['file'] = $m;
290+
$x['file'] = new LinkStub($m, $c->getStartLine());
291291
$x['line'] = $c->getStartLine().' to '.$c->getEndLine();
292292
}
293293

src/Symfony/Component/VarDumper/Caster/ResourceCaster.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public static function castProcess($process, array $a, Stub $stub, $isNested)
4040

4141
public static function castStream($stream, array $a, Stub $stub, $isNested)
4242
{
43-
return stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
43+
$a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
44+
$a['uri'] = new LinkStub($a['uri']);
45+
46+
return $a;
4447
}
4548

4649
public static function castStreamContext($stream, array $a, Stub $stub, $isNested)

src/Symfony/Component/VarDumper/Caster/SplCaster.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNe
115115
}
116116
}
117117

118+
if (isset($a[$prefix.'realPath'])) {
119+
$a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
120+
}
121+
118122
if (isset($a[$prefix.'perms'])) {
119123
$a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
120124
}

src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $
5454
'attributeCount' => $reader->attributeCount,
5555
'value' => $reader->value,
5656
'namespaceURI' => $reader->namespaceURI,
57-
'baseURI' => $reader->baseURI,
57+
'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
5858
$props => array(
5959
'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD),
6060
'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS),

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class HtmlDumper extends CliDumper
4343
'meta' => 'color:#B729D9',
4444
'key' => 'color:#56DB3A',
4545
'index' => 'color:#1299DA',
46-
'expanded code.hljs' => 'display:inline; padding:0; background:none',
4746
);
4847

4948
private $displayOptions = array(
@@ -190,14 +189,18 @@ function toggle(a, recursive) {
190189
options = {$options},
191190
elt = root.getElementsByTagName('A'),
192191
len = elt.length,
193-
i = 0, s, h,
192+
i = 0, s, h, fmt,
194193
t = [];
195194
196195
while (i < len) t.push(elt[i++]);
197196
198197
for (i in x) {
199198
options[i] = x[i];
200199
}
200+
fmt = options.fileLinkFormat;
201+
if (fmt && 'string' == typeof fmt) {
202+
fmt = [fmt];
203+
}
201204
202205
function a(e, f) {
203206
addEventListener(root, e, function (e) {
@@ -218,20 +221,6 @@ function isCtrlKey(e) {
218221
refStyle.innerHTML = '';
219222
}
220223
});
221-
if (options.fileLinkFormat) {
222-
addEventListener(root, 'click', function (e) {
223-
e = e.target;
224-
while (root != e && 'CODE' != e.tagName) {
225-
e = e.parentNode;
226-
}
227-
if ('CODE' == e.tagName) {
228-
var f = e.getAttribute('data-file'), l = e.getAttribute('data-line');
229-
if (f && l) {
230-
location.href = options.fileLinkFormat.replace('%f', f).replace('%l', l);
231-
}
232-
}
233-
});
234-
}
235224
a('mouseover', function (a) {
236225
if (a = idRx.exec(a.className)) {
237226
try {
@@ -332,6 +321,16 @@ function isCtrlKey(e) {
332321
}
333322
}
334323
}
324+
} else if (fmt && (a = elt.getAttribute('data-file'))) {
325+
if (fmt[1]) {
326+
for (x in fmt[1]) {
327+
if (0 === a.indexOf(x)) {
328+
a = fmt[1][x] + a.substr(x.length);
329+
break;
330+
}
331+
}
332+
}
333+
elt.href = fmt[0].replace('%l', elt.getAttribute('data-line')).replace('%f', a);
335334
}
336335
}
337336
@@ -387,6 +386,7 @@ function isCtrlKey(e) {
387386
cursor: pointer;
388387
border: 0;
389388
outline: none;
389+
color: inherit;
390390
}
391391
pre.sf-dump .sf-dump-ellipsis {
392392
display: inline-block;
@@ -397,6 +397,11 @@ function isCtrlKey(e) {
397397
overflow: hidden;
398398
vertical-align: top;
399399
}
400+
pre.sf-dump code {
401+
display:inline;
402+
padding:0;
403+
background:none;
404+
}
400405
.sf-dump-str-collapse .sf-dump-str-collapse {
401406
display: none;
402407
}
@@ -480,14 +485,15 @@ protected function style($style, $value, $attr = array())
480485
} elseif ('private' === $style) {
481486
$style .= sprintf(' title="Private property defined in class:&#10;`%s`"', esc($attr['class']));
482487
}
488+
$map = static::$controlCharsMap;
489+
$style = "<span class=sf-dump-{$style}>";
490+
483491
if (isset($attr['ellipsis'])) {
484492
$label = esc(substr($value, -$attr['ellipsis']));
485493

486-
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);
494+
$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);
487495
}
488496

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

501-
if ($cchrCount && '<' === $v[0]) {
507+
if ('<' === $v[0]) {
502508
$v = substr($v, 7);
503509
} else {
504510
$v = $style.$v;
505511
}
506-
if ($cchrCount && '>' === substr($v, -1)) {
512+
if ('>' === substr($v, -1)) {
507513
$v = substr($v, 0, -strlen($style));
508514
} else {
509515
$v .= '</span>';
510516
}
517+
if (isset($attr['file'])) {
518+
$v = sprintf('<a data-file="%s" data-line="%d">%s</a>', esc($attr['file']), isset($attr['line']) ? $attr['line'] : 1, $v);
519+
} elseif (isset($attr['href'])) {
520+
$v = sprintf('<a href="%s">%s</a>', esc($attr['href']), $v);
521+
}
511522
if (isset($attr['lang'])) {
512-
if (isset($attr['file'], $attr['line'])) {
513-
$v = sprintf('<code class="%s" data-file="%s" data-line="%d">%s</code>', esc($attr['lang']), esc($attr['file']), $attr['line'], $v);
514-
} else {
515-
$v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
516-
}
523+
$v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
517524
}
518525

519526
return $v;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testHtmlDump()
153153
<foo></foo><bar><span class=sf-dump-note>Exception</span> {<samp>
154154
#<span class=sf-dump-protected title="Protected property">message</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
155155
#<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
156-
#<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%d characters">%sExceptionCasterTest.php</span>"
156+
#<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>"
157157
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>25</span>
158158
-<span class=sf-dump-private title="Private property defined in class:&#10;`Exception`">trace</span>: {<samp>
159159
<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>

src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testGet()
8484
<span class=sf-dump-meta>default</span>: <span class=sf-dump-const>null</span>
8585
</samp>}
8686
</samp>}
87-
<span class=sf-dump-meta>file</span>: "<span class=sf-dump-str title="%d characters">{$var['file']}</span>"
87+
<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>"
8888
<span class=sf-dump-meta>line</span>: "<span class=sf-dump-str title="%d characters">{$var['line']} to {$var['line']}</span>"
8989
</samp>}
9090
"<span class=sf-dump-key>line</span>" => <span class=sf-dump-num>{$var['line']}</span>

0 commit comments

Comments
 (0)
0