bug #19794 [VarDumper] Various minor fixes & cleanups (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[VarDumper] Various minor fixes & cleanups

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| Tests pass?   | yes
| License       | MIT

Minor fixes & cleanups found while working on a few VarDumper enhancements.
I'm going to merge this one quickly to unlock the other PRs I'm preparing for master.

Commits
-------

a989491 [VarDumper] Various minor fixes & cleanups
This commit is contained in:
Nicolas Grekas 2016-08-31 09:58:22 +02:00
commit ac9ac8364e
5 changed files with 22 additions and 11 deletions

View File

@ -67,7 +67,7 @@ class DumpExtension extends \Twig_Extension
}
$dump = fopen('php://memory', 'r+b');
$dumper = new HtmlDumper($dump);
$dumper = new HtmlDumper($dump, $env->getCharset());
foreach ($vars as $value) {
$dumper->dump($this->cloner->cloneVar($value));

View File

@ -170,6 +170,8 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
return 'a:0:{}';
}
$this->data[] = $this->fileLinkFormat;
$this->data[] = $this->charset;
$ser = serialize($this->data);
$this->data = array();
$this->dataCount = 0;
@ -184,8 +186,10 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
public function unserialize($data)
{
parent::unserialize($data);
$charset = array_pop($this->data);
$fileLinkFormat = array_pop($this->data);
$this->dataCount = count($this->data);
self::__construct($this->stopwatch);
self::__construct($this->stopwatch, $fileLinkFormat, $charset);
}
public function getDumpsCount()

View File

@ -49,9 +49,9 @@ class DumpDataCollectorTest extends \PHPUnit_Framework_TestCase
);
$this->assertSame($xDump, $dump);
$this->assertStringMatchesFormat('a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
$this->assertStringMatchesFormat('a:3:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
$this->assertSame(0, $collector->getDumpsCount());
$this->assertSame('a:0:{}', $collector->serialize());
$this->assertSame('a:2:{i:0;b:0;i:1;s:5:"UTF-8";}', $collector->serialize());
}
public function testCollectDefault()

View File

@ -82,11 +82,13 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
public function setCharset($charset)
{
$prev = $this->charset;
$this->charsetConverter = 'fallback';
$charset = strtoupper($charset);
$charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
if ($prev === $charset) {
return $prev;
}
$this->charsetConverter = 'fallback';
$supported = true;
set_error_handler(function () use (&$supported) {$supported = false;});

View File

@ -362,7 +362,7 @@ EOHTML;
return '';
}
$v = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
$v = esc($value);
if ('ref' === $style) {
if (empty($attr['count'])) {
@ -373,18 +373,18 @@ EOHTML;
return sprintf('<a class=sf-dump-ref href=#%s-ref%s title="%d occurrences">%s</a>', $this->dumpId, $r, 1 + $attr['count'], $v);
}
if ('const' === $style && array_key_exists('value', $attr)) {
$style .= sprintf(' title="%s"', htmlspecialchars(json_encode($attr['value']), ENT_QUOTES, 'UTF-8'));
if ('const' === $style && isset($attr['value'])) {
$style .= sprintf(' title="%s"', esc(is_scalar($attr['value']) ? $attr['value'] : json_encode($attr['value'])));
} elseif ('public' === $style) {
$style .= sprintf(' title="%s"', empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property');
} elseif ('str' === $style && 1 < $attr['length']) {
$style .= sprintf(' title="%s%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
$style .= sprintf(' title="%d%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
} elseif ('note' === $style && false !== $c = strrpos($v, '\\')) {
return sprintf('<abbr title="%s" class=sf-dump-%s>%s</abbr>', $v, $style, substr($v, $c + 1));
} elseif ('protected' === $style) {
$style .= ' title="Protected property"';
} elseif ('private' === $style) {
$style .= sprintf(' title="Private property defined in class:&#10;`%s`"', $attr['class']);
$style .= sprintf(' title="Private property defined in class:&#10;`%s`"', esc($attr['class']));
}
$map = static::$controlCharsMap;
@ -461,3 +461,8 @@ EOHTML;
AbstractDumper::dumpLine($depth);
}
}
function esc($str)
{
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}