10000 Merge branch '2.6' into 2.7 · symfony/symfony@cc13cc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc13cc5

Browse files
Merge branch '2.6' into 2.7
* 2.6: [VarDumper] Fix dump output for better readability Conflicts: src/Symfony/Component/VarDumper/Tests/CliDumperTest.php src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
2 parents ad12251 + 85cb59f commit cc13cc5

File tree

5 files changed

+111
-52
lines changed

5 files changed

+111
-52
lines changed

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

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class CliDumper extends AbstractDumper
3131
'num' => '1;38;5;38',
3232
'const' => '1;38;5;208',
3333
'str' => '1;38;5;113',
34-
'cchr' => '7',
3534
'note' => '38;5;38',
3635
'ref' => '38;5;247',
3736
'public' => '',
@@ -42,7 +41,15 @@ class CliDumper extends AbstractDumper
4241
'index' => '38;5;38',
4342
);
4443

45-
protected static $controlCharsRx = '/[\x00-\x1F\x7F]/';
44+
protected static $controlCharsRx = '/[\x00-\x1F\x7F]+/';
45+
protected static $controlCharsMap = array(
46+
"\t" => '\t',
47+
"\n" => '\n',
48+
"\v" => '\v',
49+
"\f" => '\f',
50+
"\r" => '\r',
51+
"\033" => '\e',
52+
);
4653

4754
/**
4855
* {@inheritdoc}
@@ -146,7 +153,7 @@ public function dumpScalar(Cursor $cursor, $type, $value)
146153

147154
$this->line .= $this->style($style, $value, $attr);
148155

149-
$this->dumpLine($cursor->depth);
156+
$this->dumpLine($cursor->depth, true);
150157
}
151158

152159
/**
@@ -161,13 +168,17 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
161168
}
162169
if ('' === $str) {
163170
$this->line .= '""';
164-
$this->dumpLine($cursor->depth);
171+
$this->dumpLine($cursor->depth, true);
165172
} else {
166173
$attr = array(
167-
'length' => function_exists('iconv_strlen') && 0 <= $cut ? iconv_strlen($str, 'UTF-8') + $cut : 0,
174+
'length' => 0 <= $cut && function_exists('iconv_strlen') ? iconv_strlen($str, 'UTF-8') + $cut : 0,
168175
'binary' => $bin,
169176
);
170177
$str = explode("\n", $str);
178+
if (isset($str[1]) && !isset($str[2]) && !isset($str[1][0])) {
179+
unset($str[1]);
180+
$str[0] .= "\n";
181+
}
171182
$m = count($str) - 1;
172183
$i = $lineCut = 0;
173184

@@ -183,20 +194,30 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
183194
}
184195

185196
foreach ($str as $str) {
197+
if ($i < $m) {
198+
$str .= "\n";
199+
}
186200
if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = iconv_strlen($str, 'UTF-8')) {
187201
$str = iconv_substr($str, 0, $this->maxStringWidth, 'UTF-8');
188202
$lineCut = $len - $this->maxStringWidth;
189203
}
190-
191-
if ($m) {
204+
if ($m && 0 < $cursor->depth) {
192205
$this->line .= $this->indentPad;
193206
}
194-
$this->line .= $this->style('str', $str, $attr);
195-
207+
if ('' !== $str) {
208+
$this->line .= $this->style('str', $str, $attr);
209+
}
196210
if ($i++ == $m) {
197-
$this->line .= '"';
198211
if ($m) {
199-
$this->line .= '""';
212+
if ('' !== $str) {
213+
$this->dumpLine($cursor->depth);
214+
if (0 < $cursor->depth) {
215+
$this->line .= $this->indentPad;
216+
}
217+
}
218+
$this->line .= '"""';
219+
} else {
220+
$this->line .= '"';
200221
}
201222
if ($cut < 0) {
202223
$this->line .= '';
@@ -210,7 +231,7 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
210231
$lineCut = 0;
211232
}
212233

213-
$this->dumpLine($cursor->depth);
234+
$this->dumpLine($cursor->depth, $i > $m);
214235
}
215236
}
216237
}
@@ -228,7 +249,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
228249
if (Cursor::HASH_OBJECT === $type) {
229250
$prefix = 'stdClass' !== $class ? $this->style('note', $class).' {' : '{';
230251
} elseif (Cursor::HASH_RESOURCE === $type) {
231-
$prefix = $this->style('note', ':'.$class).' {';
252+
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
232253
} else {
233254
$prefix = $class ? $this->style('note', 'array:'.$class).' [' : '[';
234255
}
@@ -237,6 +258,8 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
237258
$prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), array('count' => $cursor->softRefCount));
238259
} elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
239260
$prefix .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount));
261+
} elseif (!$hasChild && Cursor::HASH_RESOURCE === $type) {
262+
$prefix = substr($prefix, 0, -1);
240263
}
241264

242265
$this->line .= $prefix;
@@ -252,8 +275,8 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
252275
public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
253276
{
254277
$this->dumpEllipsis($cursor, $hasChild, $cut);
255-
$this->line .= Cursor::HASH_OBJECT === $type || Cursor::HASH_RESOURCE === $type ? '}' : ']';
256-
$this->dumpLine($cursor->depth);
278+
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
279+
$this->dumpLine($cursor->depth, true);
257280
}
258281

259282
/**
@@ -360,12 +383,34 @@ protected function style($style, $value, $attr = array())
360383
}
361384

362385
$style = $this->styles[$style];
363-
$cchr = $this->colors ? "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m" : '%s';
364-
$value = preg_replace_callback(self::$controlCharsRx, function ($r) use ($cchr) {
365-
return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0])));
366-
}, $value);
367386

368-
return $this->colors ? sprintf("\033[%sm%s\033[m\033[%sm", $style, $value, $this->styles['default']) : $value;
387+
$map = static::$controlCharsMap;
388+
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
389+
$endCchr = $this->colors ? "\033[m\033[{$style}m" : '';
390+
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
391+
$s = $startCchr;
392+
$c = $c[$i = 0];
393+
do {
394+
$s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i]));
395+
} while (isset($c[++$i]));
396+
397+
return $s.$endCchr;
398+
}, $value, -1, $cchrCount);
399+
400+
if ($this->colors) {
401+
if ($cchrCount && "\033" === $value[0]) {
402+
$value = substr($value, strlen($startCchr));
403+
} else {
404+
$value = "\033[{$style}m".$value;
405+
}
406+
if ($cchrCount && $endCchr === substr($value, -strlen($endCchr))) {
407+
$value = substr($value, 0, -strlen($endCchr));
408+
} else {
409+
$value .= "\033[{$this->styles['default']}m";
410+
}
411+
}
412+
413+
return $value;
369414
}
370415

371416
/**
@@ -418,7 +463,7 @@ protected function supportsColors()
418463
/**
419464
* {@inheritdoc}
420465
*/
421-
protected function dumpLine($depth)
466+
protected function dumpLine($depth, $endOfValue = false)
422467
{
423468
if ($this->colors) {
424469
$this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line);

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class HtmlDumper extends CliDumper
3535
'num' => 'font-weight:bold; color:#1299DA',
3636
'const' => 'font-weight:bold',
3737
'str' => 'font-weight:bold; color:#56DB3A',
38-
'cchr' => 'color:#FF8400',
3938
'note' => 'color:#1299DA',
4039
'ref' => 'color:#A0A0A0',
4140
'public' => 'color:#FFFFFF',
@@ -376,10 +375,6 @@ protected function style($style, $value, $attr = array())
376375
}
377376

378377
$v = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
379-
$v = preg_replace_callback(self::$controlCharsRx, function ($r) {
380-
// Use Unicode Control Pictures - see http://www.unicode.org/charts/PDF/U2400.pdf
381-
return sprintf('<span class=sf-dump-cchr title=\\x%02X>&#%d;</span>', ord($r[0]), "\x7F" !== $r[0] ? 0x2400 + ord($r[0]) : 0x2421);
382-
}, $v);
383378

384379
if ('ref' === $style) {
385380
if (empty($attr['count'])) {
@@ -396,25 +391,44 @@ protected function style($style, $value, $attr = array())
396391
$style .= sprintf(' title="%s"', empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property');
397392
} elseif ('str' === $style && 1 < $attr['length']) {
398393
$style .= sprintf(' title="%s%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
399-
} elseif ('note' === $style) {
400-
if (false !== $c = strrpos($v, '\\')) {
401-
return sprintf('<abbr title="%s" class=sf-dump-%s>%s</abbr>', $v, $style, substr($v, $c + 1));
402-
} elseif (':' === $v[0]) {
403-
return sprintf('<abbr title="`%s` resource" class=sf-dump-%s>%s</abbr>', substr($v, 1), $style, $v);
404-
}
394+
} elseif ('note' === $style && false !== $c = strrpos($v, '\\')) {
395+
return sprintf('<abbr title="%s" class=sf-dump-%s>%s</abbr>', $v, $style, substr($v, $c + 1));
405396
} elseif ('protected' === $style) {
406397
$style .= ' title="Protected property"';
407398
} elseif ('private' === $style) {
408399
$style .= sprintf(' title="Private property defined in class:&#10;`%s`"', $attr['class']);
409400
}
410401

411-
return "<span class=sf-dump-$style>$v</span>";
402+
$map = static::$controlCharsMap;
403+
$style = "<span class=sf-dump-{$style}>";
404+
$v = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $style) {
405+
$s = '</span>';
406+
$c = $c[$i = 0];
407+
do {
408+
$s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i]));
409+
} while (isset($c[++$i]));
410+
411+
return $s.$style;
412+
}, $v, -1, $cchrCount);
413+
414+
if ($cchrCount && '<' === $v[0]) {
415+
$v = substr($v, 7);
416+
} else {
417+
$v = $style.$v;
418+
}
419+
if ($cchrCount && '>' === substr($v, -1)) {
420+
$v = substr($v, 0, -strlen($style));
421+
} else {
422+
$v .= '</span>';
423+
}
424+
425+
return $v;
412426
}
413427

414428
/**
415429
* {@inheritdoc}
416430
*/
417-
protected function dumpLine($depth)
431+
protected function dumpLine($depth, $endOfValue = false)
418432
{
419433
if (-1 === $this->lastDepth) {
420434
$this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
6565
4 => INF
6666
5 => -INF
6767
6 => {$intMax}
68-
"str" => "déjà"
69-
7 => b"é@"
68+
"str" => "déjà\\n"
69+
7 => b"é\\x00"
7070
"[]" => []
71-
"res" => :stream {@{$res1}
71+
"res" => stream resource {@{$res1}
7272
wrapper_type: "plainfile"
7373
stream_type: "STDIO"
7474
mode: "r"
@@ -79,7 +79,7 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
7979
eof: false
8080
options: []
8181
}
82-
8 => :Unknown {@{$res2}}
82+
8 => Unknown resource @{$res2}
8383
"obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
8484
+foo: "foo"
8585
+"bar": "bar"
@@ -126,7 +126,7 @@ public function testXmlResource()
126126

127127
$this->assertDumpEquals(
128128
<<<EOTXT
129-
:xml {
129+
xml resource {
130130
current_byte_index: 0
131131
current_column_number: 1
132132
current_line_number: 1
@@ -161,7 +161,7 @@ public function testThrowingCaster()
161161

162162
$this->assertStringMatchesFormat(
163163
<<<EOTXT
164-
:stream {@{$ref}
164+
stream resource {@{$ref}
165165
wrapper_type: "PHP"
166166
stream_type: "MEMORY"
167167
mode: "w+b"
@@ -192,7 +192,7 @@ public function testThrowingCaster()
192192
public function testRefsInProperties()
193193
{
194194
$var = (object) array('foo' => 'foo');
195-
$var->bar =& $var->foo;
195+
$var->bar = &$var->foo;
196196

197197
$dumper = new CliDumper();
198198
$dumper->setColors(false);
@@ -343,7 +343,7 @@ private function getSpecialVars()
343343

344344
$var = function &() {
345345
$var = array();
346-
$var[] =& $var;
346+
$var[] = &$var;
347347

348348
return $var;
349349
};

src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DumbFoo
1919
$var = array(
2020
'number' => 1, null,
2121
'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX,
22-
'str' => "déjà", "\xE9\x00",
22+
'str' => "déjà\n", "\xE9\x00",
2323
'[]' => array(),
2424
'res' => $g,
2525
$h,
@@ -30,14 +30,14 @@ class DumbFoo
3030
);
3131

3232
$r = array();
33-
$r[] =& $r;
33+
$r[] = &$r;
3434

35-
$var['recurs'] =& $r;
36-
$var[] =& $var[0];
35+
$var['recurs'] = &$r;
36+
$var[] = &$var[0];
3737
$var['sobj'] = $var['obj'];
38-
$var['snobj'] =& $var['nobj'][0];
38+
$var['snobj'] = &$var['nobj'][0];
3939
$var['snobj2'] = $var['nobj'][0];
4040
$var['file'] = __FILE__;
41-
$var["bin-key-\xE9"] = "";
41+
$var["bin-key-\xE9"] = '';
4242

4343
unset($g, $h, $r);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public function testGet()
6868
<span class=sf-dump-key>4</span> => <span class=sf-dump-num>INF</span>
6969
<span class=sf-dump-key>5</span> => <span class=sf-dump-num>-INF</span>
7070
<span class=sf-dump-key>6</span> => <span class=sf-dump-num>{$intMax}</span>
71-
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="4 characters">d&#233;j&#224;</span>"
72-
<span class=sf-dump-key>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;<span class=sf-dump-cchr title=\\x00>&#9216;</span></span>"
71+
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="5 characters">d&#233;j&#224;</span>\\n"
72+
<span class=sf-dump-key>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;</span>\\x00"
7373
"<span class=sf-dump-key>[]</span>" => []
74-
"<span class=sf-dump-key>res</span>" => <abbr title="`stream` resource" class=sf-dump-note>:stream</abbr> {<a class=sf-dump-ref>@{$res1}</a><samp>
74+
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res1}</a><samp>
7575
<span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
7676
<span class=sf-dump-meta>stream_type</span>: "<span class=sf-dump-str title="5 characters">STDIO</span>"
7777
<span class=sf-dump-meta>mode</span>: "<span class=sf-dump-str>r</span>"
@@ -82,7 +82,7 @@ public function testGet()
8282
<span class=sf-dump-meta>eof</span>: <span class=sf-dump-const>false</span>
8383
<span class=sf-dump-meta>options</span>: []
8484
</samp>}
85-
<span class=sf-dump-key>8</span> => <abbr title="`Unknown` resource" class=sf-dump-note>:Unknown</abbr> {<a class=sf-dump-ref>@{$res2}</a>}
85+
<span class=sf-dump-key>8</span> => <span class=sf-dump-note>Unknown resource</span> <a class=sf-dump-ref>@{$res2}</a>
8686
"<span class=sf-dump-key>obj</span>" => <abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="2 occurrences">#%d</a><samp id={$dumpId}-ref2%d>
8787
+<span class=sf-dump-public title="Public property">foo</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
8888
+"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"

0 commit comments

Comments
 (0)
0