Skip to content

Commit

Permalink
[VarDumper] add meta-data on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Oct 20, 2014
1 parent 0e6465a commit bef48b3
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 133 deletions.
Expand Up @@ -97,7 +97,7 @@ public function getDumpArgs()
array('foo' => 'bar'),
array(),
"<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-note>array:1</span> [<samp>\n"
." \"<span class=sf-dump-meta>foo</span>\" => \"<span class=sf-dump-str>bar</span>\"\n"
." \"<span class=sf-dump-meta>foo</span>\" => \"<span class=sf-dump-str title=\"3 characters\">bar</span>\"\n"
."</samp>]\n"
."</pre><script>Sfdump(\"sf-dump\")</script>\n",
),
Expand Down
Expand Up @@ -49,7 +49,7 @@ public function testDump()
$this->assertSame($xDump, $dump);

$this->assertStringStartsWith(
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":3:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}s:49:"Symfony\Component\VarDumper\Cloner\DatamaxDepth";i:-1;s:57:"Symfony\Component\VarDumper\Cloner\DatamaxItemsPerDepth";i:-1;}s:4:"name";s:25:"DumpDataCollectorTest.php";s:4:"file";s:',
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":4:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}s:49:"Symfony\Component\VarDumper\Cloner\DatamaxDepth";i:-1;s:57:"Symfony\Component\VarDumper\Cloner\DatamaxItemsPerDepth";i:-1;s:54:"Symfony\Component\VarDumper\Cloner\DatauseRefHandles";i:-1;}s:4:"name";s:25:"DumpDataCollectorTest.php";s:4:"file";s:',
str_replace("\0", '', $collector->serialize())
);

Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/ConstStub.php
@@ -0,0 +1,28 @@
<?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;

use Symfony\Component\VarDumper\Cloner\Stub;

/**
* Represents a PHP constant and its value.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class ConstStub extends Stub
{
public function __construct($name, $value)
{
$this->class = $name;
$this->value = $value;
}
}
Expand Up @@ -18,11 +18,10 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CasterStub extends Stub
class CutStub extends Stub
{
public function __construct($value, $class = '')
public function __construct($value)
{
$this->class = $class;
$this->value = $value;

switch (gettype($value)) {
Expand All @@ -47,12 +46,10 @@ public function __construct($value, $class = '')
break;

case 'string':
if ('' === $class) {
$this->type = self::TYPE_STRING;
$this->class = preg_match('//u', $value) ? self::STRING_UTF8 : self::STRING_BINARY;
$this->cut = self::STRING_BINARY === $this->class ? strlen($value) : (function_exists('iconv_strlen') ? iconv_strlen($value, 'UTF-8') : -1);
$this->value = '';
}
$this->type = self::TYPE_STRING;
$this->class = preg_match('//u', $value) ? self::STRING_UTF8 : self::STRING_BINARY;
$this->cut = self::STRING_BINARY === $this->class ? strlen($value) : (function_exists('iconv_strlen') ? iconv_strlen($value, 'UTF-8') : -1);
$this->value = '';
break;
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/Symfony/Component/VarDumper/Caster/DOMCaster.php
Expand Up @@ -64,7 +64,7 @@ class DOMCaster
public static function castException(\DOMException $e, array $a, Stub $stub, $isNested)
{
if (isset($a["\0*\0code"], self::$errorCodes[$a["\0*\0code"]])) {
$a["\0*\0code"] = new CasterStub(self::$errorCodes[$a["\0*\0code"]], 'const');
$a["\0*\0code"] = new ConstStub(self::$errorCodes[$a["\0*\0code"]], $a["\0*\0code"]);
}

return $a;
Expand Down Expand Up @@ -93,21 +93,21 @@ public static function castNode(\DOMNode $dom, array $a, Stub $stub, $isNested)
{
$a += array(
'nodeName' => $dom->nodeName,
'nodeValue' => new CasterStub($dom->nodeValue),
'nodeType' => new CasterStub(self::$nodeTypes[$dom->nodeType], 'const'),
'parentNode' => new CasterStub($dom->parentNode),
'nodeValue' => new CutStub($dom->nodeValue),
'nodeType' => new ConstStub(self::$nodeTypes[$dom->nodeType], $dom->nodeType),
'parentNode' => new CutStub($dom->parentNode),
'childNodes' => $dom->childNodes,
'firstChild' => new CasterStub($dom->firstChild),
'lastChild' => new CasterStub($dom->lastChild),
'previousSibling' => new CasterStub($dom->previousSibling),
'nextSibling' => new CasterStub($dom->nextSibling),
'firstChild' => new CutStub($dom->firstChild),
'lastChild' => new CutStub($dom->lastChild),
'previousSibling' => new CutStub($dom->previousSibling),
'nextSibling' => new CutStub($dom->nextSibling),
'attributes' => $dom->attributes,
'ownerDocument' => new CasterStub($dom->ownerDocument),
'ownerDocument' => new CutStub($dom->ownerDocument),
'namespaceURI' => $dom->namespaceURI,
'prefix' => $dom->prefix,
'localName' => $dom->localName,
'baseURI' => $dom->baseURI,
'textContent' => new CasterStub($dom->textContent),
'textContent' => new CutStub($dom->textContent),
);

return $a;
Expand All @@ -119,13 +119,13 @@ public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub

$a += array(
'nodeName' => $dom->nodeName,
'nodeValue' => new CasterStub($dom->nodeValue),
'nodeType' => new CasterStub(self::$nodeTypes[$dom->nodeType], 'const'),
'nodeValue' => new CutStub($dom->nodeValue),
'nodeType' => new ConstStub(self::$nodeTypes[$dom->nodeType], $dom->nodeType),
'prefix' => $dom->prefix,
'localName' => $dom->localName,
'namespaceURI' => $dom->namespaceURI,
'ownerDocument' => new CasterStub($dom->ownerDocument),
'parentNode' => new CasterStub($dom->parentNode),
'ownerDocument' => new CutStub($dom->ownerDocument),
'parentNode' => new CutStub($dom->parentNode),
);

return $a;
Expand All @@ -139,7 +139,7 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $is
$a += array(
'doctype' => $dom->doctype,
'implementation' => $dom->implementation,
'documentElement' => new CasterStub($dom->documentElement),
'documentElement' => new CutStub($dom->documentElement),
'actualEncoding' => $dom->actualEncoding,
'encoding' => $dom->encoding,
'xmlEncoding' => $dom->xmlEncoding,
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Caster/DoctrineCaster.php
Expand Up @@ -50,9 +50,9 @@ public static function castPersistentCollection(PersistentCollection $coll, arra
{
$prefix = "\0Doctrine\\ORM\\PersistentCollection\0";

$a[$prefix.'snapshot'] = new CasterStub($a[$prefix.'snapshot']);
$a[$prefix.'association'] = new CasterStub($a[$prefix.'association']);
$a[$prefix.'typeClass'] = new CasterStub($a[$prefix.'typeClass']);
$a[$prefix.'snapshot'] = new CutStub($a[$prefix.'snapshot']);
$a[$prefix.'association'] = new CutStub($a[$prefix.'association']);
$a[$prefix.'typeClass'] = new CutStub($a[$prefix.'typeClass']);

return $a;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Expand Up @@ -61,7 +61,7 @@ public static function castException(\Exception $e, array $a, Stub $stub, $isNes
public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
{
if (isset($a[$s = "\0*\0severity"], self::$errorTypes[$a[$s]])) {
$a[$s] = new CasterStub(self::$errorTypes[$a[$s]], 'const');
$a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
}

return $a;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/PdoCaster.php
Expand Up @@ -72,7 +72,7 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
try {
$a[$attr] = 'ERRMODE' === $attr ? $errmode : $c->getAttribute(constant("PDO::ATTR_{$attr}"));
if (isset($values[$a[$attr]])) {
$a[$attr] = new CasterStub($values[$a[$attr]], 'const');
$a[$attr] = new ConstStub($values[$a[$attr]], $a[$attr]);
}
} catch (\Exception $m) {
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/SplCaster.php
Expand Up @@ -72,7 +72,7 @@ public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, S
$c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);

$a += array(
"\0~\0mode" => new CasterStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), 'const'),
"\0~\0mode" => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), $mode),
"\0~\0dllist" => iterator_to_array($c),
);
$c->setIteratorMode($mode);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Caster/StubCaster.php
Expand Up @@ -14,13 +14,13 @@
use Symfony\Component\VarDumper\Cloner\Stub;

/**
* Casts a CasterStub.
* Casts a caster's Stub.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class StubCaster
{
public static function castStub(CasterStub $c, array $a, Stub $stub, $isNested)
public static function castStub(Stub $c, array $a, Stub $stub, $isNested)
{
if ($isNested) {
$stub->type = $c->type;
Expand All @@ -33,7 +33,7 @@ public static function castStub(CasterStub $c, array $a, Stub $stub, $isNested)
}
}

public static function castNestedFat($obj, array $a, Stub $stub, $isNested)
public static function cutInternals($obj, array $a, Stub $stub, $isNested)
{
if ($isNested) {
$stub->cut += count($a);
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -21,12 +21,13 @@
abstract class AbstractCloner implements ClonerInterface
{
public static $defaultCasters = array(
'Symfony\Component\VarDumper\Caster\CasterStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
'Symfony\Component\VarDumper\Caster\CutStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',

'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
'Reflector' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castReflector',

'Doctrine\Common\Persistence\ObjectManager' => 'Symfony\Component\VarDumper\Caster\StubCaster::castNestedFat',
'Doctrine\Common\Persistence\ObjectManager' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
'Doctrine\Common\Proxy\Proxy' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castCommonProxy',
'Doctrine\ORM\Proxy\Proxy' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castOrmProxy',
'Doctrine\ORM\PersistentCollection' => 'Symfony\Component\VarDumper\Caster\DoctrineCaster::castPersistentCollection',
Expand Down Expand Up @@ -57,7 +58,7 @@ abstract class AbstractCloner implements ClonerInterface
'ErrorException' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castErrorException',
'Exception' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castException',
'Symfony\Component\DependencyInjection\ContainerInterface'
=> 'Symfony\Component\VarDumper\Caster\StubCaster::castNestedFat',
=> 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
'Symfony\Component\VarDumper\Exception\ThrowingCasterException'
=> 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castThrowingCasterException',

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/Cloner/Cursor.php
Expand Up @@ -33,6 +33,7 @@ class Cursor
public $hardRefHandle = 0;
public $hashType;
public $hashKey;
public $hashKeyIsBinary;
public $hashIndex = 0;
public $hashLength = 0;
public $hashCut = 0;
Expand Down
19 changes: 13 additions & 6 deletions src/Symfony/Component/VarDumper/Cloner/Data.php
Expand Up @@ -19,6 +19,7 @@ class Data
private $data;
private $maxDepth = -1;
private $maxItemsPerDepth = -1;
private $useRefHandles = -1;

/**
* @param array $data A array as returned by ClonerInterface::cloneVar().
Expand All @@ -39,16 +40,18 @@ public function getRawData()
/**
* Returns a depth limited clone of $this.
*
* @param int $maxDepth The max dumped depth level.
* @param int $maxItemsPerDepth The max number of items dumped per depth level.
* @param int $maxDepth The max dumped depth level.
* @param int $maxItemsPerDepth The max number of items dumped per depth level.
* @param bool $useRefHandles False to hide ref. handles.
*
* @return self A depth limited clone of $this.
*/
public function getLimitedClone($maxDepth, $maxItemsPerDepth)
public function getLimitedClone($maxDepth, $maxItemsPerDepth, $useRefHandles = true)
{
$data = clone $this;
$data->maxDepth = (int) $maxDepth;
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;
$data->useRefHandles = $useRefHandles ? -1 : 0;

return $data;
}
Expand Down Expand Up @@ -87,7 +90,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
$firstSeen = false;
}
$cursor->hardRefTo = $refs[$r];
$cursor->hardRefHandle = $item->handle;
$cursor->hardRefHandle = $this->useRefHandles & $item->handle;
$cursor->hardRefCount = $item->refCount;
}
$type = $item->class ?: gettype($item->value);
Expand All @@ -102,7 +105,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
}
$cursor->softRefTo = $refs[$r];
}
$cursor->softRefHandle = $item->handle;
$cursor->softRefHandle = $this->useRefHandles & $item->handle;
$cursor->softRefCount = $item->refCount;
$cut = $item->cut;

Expand Down Expand Up @@ -142,6 +145,8 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
} elseif ('array' === $type) {
$dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
$dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
} elseif ('string' === $type) {
$dumper->dumpString($cursor, $item, false, 0);
} else {
$dumper->dumpScalar($cursor, $type, $item);
}
Expand Down Expand Up @@ -169,7 +174,9 @@ private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCu
$cursor->hashIndex = 0;
$cursor->hashLength = count($children);
$cursor->hashCut = $hashCut;
foreach ($children as $cursor->hashKey => $child) {
foreach ($children as $key => $child) {
$cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
$cursor->hashKey = $cursor->hashKeyIsBinary ? self::utf8Encode($key) : $key;
$this->dumpItem($dumper, $cursor, $refs, $child);
if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
$parentCursor->stop = true;
Expand Down

0 comments on commit bef48b3

Please sign in to comment.