2014-04-05 19:35:00 +01:00
|
|
|
<?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\Cloner;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
|
*/
|
|
|
|
class Data
|
|
|
|
{
|
|
|
|
private $data;
|
2015-01-08 10:41:30 +00:00
|
|
|
private $maxDepth = 20;
|
2014-09-12 13:12:12 +01:00
|
|
|
private $maxItemsPerDepth = -1;
|
2014-10-19 16:37:57 +01:00
|
|
|
private $useRefHandles = -1;
|
2014-04-05 19:35:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data A array as returned by ClonerInterface::cloneVar().
|
|
|
|
*/
|
|
|
|
public function __construct(array $data)
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
2014-09-12 13:12:12 +01:00
|
|
|
/**
|
|
|
|
* @return array The raw data structure.
|
|
|
|
*/
|
2014-04-05 19:35:00 +01:00
|
|
|
public function getRawData()
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
2014-04-05 20:13:51 +01:00
|
|
|
|
2014-09-12 13:12:12 +01:00
|
|
|
/**
|
|
|
|
* Returns a depth limited clone of $this.
|
|
|
|
*
|
2014-10-19 16:37:57 +01:00
|
|
|
* @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.
|
2014-09-12 13:12:12 +01:00
|
|
|
*
|
|
|
|
* @return self A depth limited clone of $this.
|
|
|
|
*/
|
2014-10-19 16:37:57 +01:00
|
|
|
public function getLimitedClone($maxDepth, $maxItemsPerDepth, $useRefHandles = true)
|
2014-09-12 13:12:12 +01:00
|
|
|
{
|
|
|
|
$data = clone $this;
|
|
|
|
$data->maxDepth = (int) $maxDepth;
|
|
|
|
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;
|
2014-10-19 16:37:57 +01:00
|
|
|
$data->useRefHandles = $useRefHandles ? -1 : 0;
|
2014-09-12 13:12:12 +01:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2014-04-05 20:13:51 +01:00
|
|
|
/**
|
2014-09-17 09:28:09 +01:00
|
|
|
* Dumps data with a DumperInterface dumper.
|
2014-04-05 20:13:51 +01:00
|
|
|
*/
|
2014-09-17 09:28:09 +01:00
|
|
|
public function dump(DumperInterface $dumper)
|
2014-04-05 20:13:51 +01:00
|
|
|
{
|
|
|
|
$refs = array(0);
|
2014-10-10 16:31:59 +01:00
|
|
|
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[0][0]);
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-17 09:28:09 +01:00
|
|
|
* Depth-first dumping of items.
|
2014-04-05 20:13:51 +01:00
|
|
|
*
|
2014-09-17 09:28:09 +01:00
|
|
|
* @param DumperInterface $dumper The dumper being used for dumping.
|
|
|
|
* @param Cursor $cursor A cursor used for tracking dumper state position.
|
|
|
|
* @param array &$refs A map of all references discovered while dumping.
|
|
|
|
* @param mixed $item A Stub object or the original value being dumped.
|
2014-04-05 20:13:51 +01:00
|
|
|
*/
|
|
|
|
private function dumpItem($dumper, $cursor, &$refs, $item)
|
|
|
|
{
|
2014-10-10 16:31:59 +01:00
|
|
|
$cursor->refIndex = 0;
|
|
|
|
$cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
|
|
|
|
$cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
|
|
|
|
$firstSeen = true;
|
2014-09-08 19:39:52 +01:00
|
|
|
|
|
|
|
if (!$item instanceof Stub) {
|
|
|
|
$type = gettype($item);
|
|
|
|
} elseif (Stub::TYPE_REF === $item->type) {
|
2014-10-10 16:31:59 +01:00
|
|
|
if ($item->handle) {
|
|
|
|
if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
|
|
|
|
$cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
|
2014-09-08 19:39:52 +01:00
|
|
|
} else {
|
2014-10-10 16:31:59 +01:00
|
|
|
$firstSeen = false;
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
2014-10-10 16:31:59 +01:00
|
|
|
$cursor->hardRefTo = $refs[$r];
|
2014-10-19 16:37:57 +01:00
|
|
|
$cursor->hardRefHandle = $this->useRefHandles & $item->handle;
|
2014-10-10 16:31:59 +01:00
|
|
|
$cursor->hardRefCount = $item->refCount;
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
2014-09-08 19:39:52 +01:00
|
|
|
$type = $item->class ?: gettype($item->value);
|
|
|
|
$item = $item->value;
|
|
|
|
}
|
|
|
|
if ($item instanceof Stub) {
|
2014-10-10 16:31:59 +01:00
|
|
|
if ($item->refCount) {
|
|
|
|
if (!isset($refs[$r = $item->handle])) {
|
|
|
|
$cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
|
2014-04-05 20:13:51 +01:00
|
|
|
} else {
|
2014-10-10 16:31:59 +01:00
|
|
|
$firstSeen = false;
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
2014-10-10 16:31:59 +01:00
|
|
|
$cursor->softRefTo = $refs[$r];
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
2014-10-19 16:37:57 +01:00
|
|
|
$cursor->softRefHandle = $this->useRefHandles & $item->handle;
|
2014-10-10 16:31:59 +01:00
|
|
|
$cursor->softRefCount = $item->refCount;
|
2014-09-08 19:39:52 +01:00
|
|
|
$cut = $item->cut;
|
2014-04-05 20:13:51 +01:00
|
|
|
|
2014-10-10 16:31:59 +01:00
|
|
|
if ($item->position && $firstSeen) {
|
2014-09-08 19:39:52 +01:00
|
|
|
$children = $this->data[$item->position];
|
2014-04-05 20:13:51 +01:00
|
|
|
|
|
|
|
if ($cursor->stop) {
|
|
|
|
if ($cut >= 0) {
|
|
|
|
$cut += count($children);
|
|
|
|
}
|
|
|
|
$children = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$children = array();
|
|
|
|
}
|
2014-09-08 19:39:52 +01:00
|
|
|
switch ($item->type) {
|
|
|
|
case Stub::TYPE_STRING:
|
|
|
|
$dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stub::TYPE_ARRAY:
|
2014-10-10 16:31:59 +01:00
|
|
|
$dumper->enterHash($cursor, $item->class, $item->value, (bool) $children);
|
2014-09-08 19:39:52 +01:00
|
|
|
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->class);
|
2014-10-10 16:31:59 +01:00
|
|
|
$dumper->leaveHash($cursor, $item->class, $item->value, (bool) $children, $cut);
|
2014-09-08 19:39:52 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Stub::TYPE_OBJECT:
|
|
|
|
case Stub::TYPE_RESOURCE:
|
2014-10-10 16:31:59 +01:00
|
|
|
$dumper->enterHash($cursor, $item->type, $item->class, (bool) $children);
|
|
|
|
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type);
|
|
|
|
$dumper->leaveHash($cursor, $item->type, $item->class, (bool) $children, $cut);
|
2014-09-08 19:39:52 +01:00
|
|
|
break;
|
2014-04-05 20:13:51 +01:00
|
|
|
|
2014-09-08 19:39:52 +01:00
|
|
|
default:
|
|
|
|
throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
2014-09-08 19:39:52 +01:00
|
|
|
} elseif ('array' === $type) {
|
2014-10-10 16:31:59 +01:00
|
|
|
$dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
|
|
|
|
$dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
|
2014-10-19 16:37:57 +01:00
|
|
|
} elseif ('string' === $type) {
|
|
|
|
$dumper->dumpString($cursor, $item, false, 0);
|
2014-04-05 20:13:51 +01:00
|
|
|
} else {
|
|
|
|
$dumper->dumpScalar($cursor, $type, $item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumps children of hash structures.
|
|
|
|
*
|
2014-09-17 09:28:09 +01:00
|
|
|
* @param DumperInterface $dumper
|
|
|
|
* @param Cursor $parentCursor The cursor of the parent hash.
|
|
|
|
* @param array &$refs A map of all references discovered while dumping.
|
|
|
|
* @param array $children The children to dump.
|
|
|
|
* @param int $hashCut The number of items removed from the original hash.
|
|
|
|
* @param string $hashType A Cursor::HASH_* const.
|
2014-04-05 20:13:51 +01:00
|
|
|
*
|
|
|
|
* @return int The final number of removed items.
|
|
|
|
*/
|
|
|
|
private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
|
|
|
|
{
|
|
|
|
if ($children) {
|
2014-09-12 13:12:12 +01:00
|
|
|
if ($parentCursor->depth !== $this->maxDepth && $this->maxItemsPerDepth) {
|
|
|
|
$cursor = clone $parentCursor;
|
|
|
|
++$cursor->depth;
|
|
|
|
$cursor->hashType = $hashType;
|
|
|
|
$cursor->hashIndex = 0;
|
|
|
|
$cursor->hashLength = count($children);
|
|
|
|
$cursor->hashCut = $hashCut;
|
2014-10-19 16:37:57 +01:00
|
|
|
foreach ($children as $key => $child) {
|
|
|
|
$cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
|
2015-01-18 18:50:58 +00:00
|
|
|
$cursor->hashKey = $key;
|
2014-09-12 13:12:12 +01:00
|
|
|
$this->dumpItem($dumper, $cursor, $refs, $child);
|
|
|
|
if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
|
|
|
|
$parentCursor->stop = true;
|
|
|
|
|
|
|
|
return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
|
|
|
|
}
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
2014-09-12 13:12:12 +01:00
|
|
|
} elseif ($hashCut >= 0) {
|
|
|
|
$hashCut += count($children);
|
2014-04-05 20:13:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hashCut;
|
|
|
|
}
|
2014-04-05 19:35:00 +01:00
|
|
|
}
|