Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Cloner; Chris@0: Chris@0: use Symfony\Component\VarDumper\Caster\Caster; Chris@0: Chris@0: /** Chris@0: * @author Nicolas Grekas
Chris@0: */ Chris@0: class Data implements \ArrayAccess, \Countable, \IteratorAggregate Chris@0: { Chris@0: private $data; Chris@0: private $position = 0; Chris@0: private $key = 0; Chris@0: private $maxDepth = 20; Chris@0: private $maxItemsPerDepth = -1; Chris@0: private $useRefHandles = -1; Chris@0: Chris@0: /** Chris@0: * @param array $data An array as returned by ClonerInterface::cloneVar() Chris@0: */ Chris@0: public function __construct(array $data) Chris@0: { Chris@0: $this->data = $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string The type of the value Chris@0: */ Chris@0: public function getType() Chris@0: { Chris@0: $item = $this->data[$this->position][$this->key]; Chris@0: Chris@0: if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) { Chris@0: $item = $item->value; Chris@0: } Chris@0: if (!$item instanceof Stub) { Chris@17: return \gettype($item); Chris@0: } Chris@0: if (Stub::TYPE_STRING === $item->type) { Chris@0: return 'string'; Chris@0: } Chris@0: if (Stub::TYPE_ARRAY === $item->type) { Chris@0: return 'array'; Chris@0: } Chris@0: if (Stub::TYPE_OBJECT === $item->type) { Chris@0: return $item->class; Chris@0: } Chris@0: if (Stub::TYPE_RESOURCE === $item->type) { Chris@0: return $item->class.' resource'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param bool $recursive Whether values should be resolved recursively or not Chris@0: * Chris@17: * @return string|int|float|bool|array|Data[]|null A native representation of the original value Chris@0: */ Chris@0: public function getValue($recursive = false) Chris@0: { Chris@0: $item = $this->data[$this->position][$this->key]; Chris@0: Chris@0: if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) { Chris@0: $item = $item->value; Chris@0: } Chris@0: if (!($item = $this->getStub($item)) instanceof Stub) { Chris@0: return $item; Chris@0: } Chris@0: if (Stub::TYPE_STRING === $item->type) { Chris@0: return $item->value; Chris@0: } Chris@0: Chris@17: $children = $item->position ? $this->data[$item->position] : []; Chris@0: Chris@0: foreach ($children as $k => $v) { Chris@0: if ($recursive && !($v = $this->getStub($v)) instanceof Stub) { Chris@0: continue; Chris@0: } Chris@0: $children[$k] = clone $this; Chris@0: $children[$k]->key = $k; Chris@0: $children[$k]->position = $item->position; Chris@0: Chris@0: if ($recursive) { Chris@0: if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) { Chris@0: $recursive = (array) $recursive; Chris@0: if (isset($recursive[$v->position])) { Chris@0: continue; Chris@0: } Chris@0: $recursive[$v->position] = true; Chris@0: } Chris@0: $children[$k] = $children[$k]->getValue($recursive); Chris@0: } Chris@0: } Chris@0: Chris@0: return $children; Chris@0: } Chris@0: Chris@0: public function count() Chris@0: { Chris@17: return \count($this->getValue()); Chris@0: } Chris@0: Chris@0: public function getIterator() Chris@0: { Chris@17: if (!\is_array($value = $this->getValue())) { Chris@17: throw new \LogicException(sprintf('%s object holds non-iterable type "%s".', self::class, \gettype($value))); Chris@0: } Chris@0: Chris@0: foreach ($value as $k => $v) { Chris@0: yield $k => $v; Chris@0: } Chris@0: } Chris@0: Chris@0: public function __get($key) Chris@0: { Chris@0: if (null !== $data = $this->seek($key)) { Chris@0: $item = $this->getStub($data->data[$data->position][$data->key]); Chris@0: Chris@17: return $item instanceof Stub || [] === $item ? $data : $item; Chris@0: } Chris@0: } Chris@0: Chris@0: public function __isset($key) Chris@0: { Chris@0: return null !== $this->seek($key); Chris@0: } Chris@0: Chris@0: public function offsetExists($key) Chris@0: { Chris@0: return $this->__isset($key); Chris@0: } Chris@0: Chris@0: public function offsetGet($key) Chris@0: { Chris@0: return $this->__get($key); Chris@0: } Chris@0: Chris@0: public function offsetSet($key, $value) Chris@0: { Chris@0: throw new \BadMethodCallException(self::class.' objects are immutable.'); Chris@0: } Chris@0: Chris@0: public function offsetUnset($key) Chris@0: { Chris@0: throw new \BadMethodCallException(self::class.' objects are immutable.'); Chris@0: } Chris@0: Chris@0: public function __toString() Chris@0: { Chris@0: $value = $this->getValue(); Chris@0: Chris@17: if (!\is_array($value)) { Chris@0: return (string) $value; Chris@0: } Chris@0: Chris@17: return sprintf('%s (count=%d)', $this->getType(), \count($value)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return array The raw data structure Chris@0: * Chris@0: * @deprecated since version 3.3. Use array or object access instead. Chris@0: */ Chris@0: public function getRawData() Chris@0: { Chris@17: @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the array or object access instead.', __METHOD__)); Chris@0: Chris@0: return $this->data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a depth limited clone of $this. Chris@0: * Chris@0: * @param int $maxDepth The max dumped depth level Chris@0: * Chris@0: * @return self A clone of $this Chris@0: */ Chris@0: public function withMaxDepth($maxDepth) Chris@0: { Chris@0: $data = clone $this; Chris@0: $data->maxDepth = (int) $maxDepth; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Limits the number of elements per depth level. Chris@0: * Chris@0: * @param int $maxItemsPerDepth The max number of items dumped per depth level Chris@0: * Chris@0: * @return self A clone of $this Chris@0: */ Chris@0: public function withMaxItemsPerDepth($maxItemsPerDepth) Chris@0: { Chris@0: $data = clone $this; Chris@0: $data->maxItemsPerDepth = (int) $maxItemsPerDepth; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables/disables objects' identifiers tracking. Chris@0: * Chris@0: * @param bool $useRefHandles False to hide global ref. handles Chris@0: * Chris@0: * @return self A clone of $this Chris@0: */ Chris@0: public function withRefHandles($useRefHandles) Chris@0: { Chris@0: $data = clone $this; Chris@0: $data->useRefHandles = $useRefHandles ? -1 : 0; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Seeks to a specific key in nested data structures. Chris@0: * Chris@0: * @param string|int $key The key to seek to Chris@0: * Chris@13: * @return self|null A clone of $this or null if the key is not set Chris@0: */ Chris@0: public function seek($key) Chris@0: { Chris@0: $item = $this->data[$this->position][$this->key]; Chris@0: Chris@0: if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) { Chris@0: $item = $item->value; Chris@0: } Chris@0: if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) { Chris@0: return; Chris@0: } Chris@17: $keys = [$key]; Chris@0: Chris@0: switch ($item->type) { Chris@0: case Stub::TYPE_OBJECT: Chris@0: $keys[] = Caster::PREFIX_DYNAMIC.$key; Chris@0: $keys[] = Caster::PREFIX_PROTECTED.$key; Chris@0: $keys[] = Caster::PREFIX_VIRTUAL.$key; Chris@0: $keys[] = "\0$item->class\0$key"; Chris@0: // no break Chris@0: case Stub::TYPE_ARRAY: Chris@0: case Stub::TYPE_RESOURCE: Chris@0: break; Chris@0: default: Chris@0: return; Chris@0: } Chris@0: Chris@0: $data = null; Chris@0: $children = $this->data[$item->position]; Chris@0: Chris@0: foreach ($keys as $key) { Chris@18: if (isset($children[$key]) || \array_key_exists($key, $children)) { Chris@0: $data = clone $this; Chris@0: $data->key = $key; Chris@0: $data->position = $item->position; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps data with a DumperInterface dumper. Chris@0: */ Chris@0: public function dump(DumperInterface $dumper) Chris@0: { Chris@17: $refs = [0]; Chris@0: $this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Depth-first dumping of items. Chris@0: * Chris@0: * @param DumperInterface $dumper The dumper being used for dumping Chris@0: * @param Cursor $cursor A cursor used for tracking dumper state position Chris@0: * @param array &$refs A map of all references discovered while dumping Chris@0: * @param mixed $item A Stub object or the original value being dumped Chris@0: */ Chris@0: private function dumpItem($dumper, $cursor, &$refs, $item) Chris@0: { Chris@0: $cursor->refIndex = 0; Chris@0: $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0; Chris@0: $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0; Chris@0: $firstSeen = true; Chris@0: Chris@0: if (!$item instanceof Stub) { Chris@17: $cursor->attr = []; Chris@0: $type = \gettype($item); Chris@0: if ($item && 'array' === $type) { Chris@0: $item = $this->getStub($item); Chris@0: } Chris@0: } elseif (Stub::TYPE_REF === $item->type) { Chris@0: if ($item->handle) { Chris@0: if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) { Chris@0: $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0]; Chris@0: } else { Chris@0: $firstSeen = false; Chris@0: } Chris@0: $cursor->hardRefTo = $refs[$r]; Chris@0: $cursor->hardRefHandle = $this->useRefHandles & $item->handle; Chris@0: $cursor->hardRefCount = $item->refCount; Chris@0: } Chris@0: $cursor->attr = $item->attr; Chris@17: $type = $item->class ?: \gettype($item->value); Chris@0: $item = $this->getStub($item->value); Chris@0: } Chris@0: if ($item instanceof Stub) { Chris@0: if ($item->refCount) { Chris@0: if (!isset($refs[$r = $item->handle])) { Chris@0: $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0]; Chris@0: } else { Chris@0: $firstSeen = false; Chris@0: } Chris@0: $cursor->softRefTo = $refs[$r]; Chris@0: } Chris@0: $cursor->softRefHandle = $this->useRefHandles & $item->handle; Chris@0: $cursor->softRefCount = $item->refCount; Chris@0: $cursor->attr = $item->attr; Chris@0: $cut = $item->cut; Chris@0: Chris@0: if ($item->position && $firstSeen) { Chris@0: $children = $this->data[$item->position]; Chris@0: Chris@0: if ($cursor->stop) { Chris@0: if ($cut >= 0) { Chris@17: $cut += \count($children); Chris@0: } Chris@17: $children = []; Chris@0: } Chris@0: } else { Chris@17: $children = []; Chris@0: } Chris@0: switch ($item->type) { Chris@0: case Stub::TYPE_STRING: Chris@0: $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut); Chris@0: break; Chris@0: Chris@0: case Stub::TYPE_ARRAY: Chris@0: $item = clone $item; Chris@0: $item->type = $item->class; Chris@0: $item->class = $item->value; Chris@0: // no break Chris@0: case Stub::TYPE_OBJECT: Chris@0: case Stub::TYPE_RESOURCE: Chris@0: $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth; Chris@0: $dumper->enterHash($cursor, $item->type, $item->class, $withChildren); Chris@0: if ($withChildren) { Chris@12: if ($cursor->skipChildren) { Chris@12: $withChildren = false; Chris@12: $cut = -1; Chris@12: } else { Chris@12: $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class); Chris@12: } Chris@0: } elseif ($children && 0 <= $cut) { Chris@17: $cut += \count($children); Chris@0: } Chris@12: $cursor->skipChildren = false; Chris@0: $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut); Chris@0: break; Chris@0: Chris@0: default: Chris@0: throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type)); Chris@0: } Chris@0: } elseif ('array' === $type) { Chris@0: $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false); Chris@0: $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0); Chris@0: } elseif ('string' === $type) { Chris@0: $dumper->dumpString($cursor, $item, false, 0); Chris@0: } else { Chris@0: $dumper->dumpScalar($cursor, $type, $item); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps children of hash structures. Chris@0: * Chris@0: * @param DumperInterface $dumper Chris@0: * @param Cursor $parentCursor The cursor of the parent hash Chris@0: * @param array &$refs A map of all references discovered while dumping Chris@0: * @param array $children The children to dump Chris@0: * @param int $hashCut The number of items removed from the original hash Chris@0: * @param string $hashType A Cursor::HASH_* const Chris@0: * @param bool $dumpKeys Whether keys should be dumped or not Chris@0: * Chris@0: * @return int The final number of removed items Chris@0: */ Chris@0: private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType, $dumpKeys) Chris@0: { Chris@0: $cursor = clone $parentCursor; Chris@0: ++$cursor->depth; Chris@0: $cursor->hashType = $hashType; Chris@0: $cursor->hashIndex = 0; Chris@17: $cursor->hashLength = \count($children); Chris@0: $cursor->hashCut = $hashCut; Chris@0: foreach ($children as $key => $child) { Chris@0: $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key); Chris@0: $cursor->hashKey = $dumpKeys ? $key : null; Chris@0: $this->dumpItem($dumper, $cursor, $refs, $child); Chris@0: if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) { Chris@0: $parentCursor->stop = true; Chris@0: Chris@0: return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut; Chris@0: } Chris@0: } Chris@0: Chris@0: return $hashCut; Chris@0: } Chris@0: Chris@0: private function getStub($item) Chris@0: { Chris@0: if (!$item || !\is_array($item)) { Chris@0: return $item; Chris@0: } Chris@0: Chris@0: $stub = new Stub(); Chris@0: $stub->type = Stub::TYPE_ARRAY; Chris@0: foreach ($item as $stub->class => $stub->position) { Chris@0: } Chris@0: if (isset($item[0])) { Chris@0: $stub->cut = $item[0]; Chris@0: } Chris@0: $stub->value = $stub->cut + ($stub->position ? \count($this->data[$stub->position]) : 0); Chris@0: Chris@0: return $stub; Chris@0: } Chris@0: }