| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 | <?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;use Symfony\Component\VarDumper\Caster\Caster;use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;/** * @author Nicolas Grekas <p@tchwork.com> */class Data implements \ArrayAccess, \Countable, \IteratorAggregate{    private $data;    private $position = 0;    private $key = 0;    private $maxDepth = 20;    private $maxItemsPerDepth = -1;    private $useRefHandles = -1;    private $context = [];    /**     * @param array $data An array as returned by ClonerInterface::cloneVar()     */    public function __construct(array $data)    {        $this->data = $data;    }    /**     * @return string|null The type of the value     */    public function getType()    {        $item = $this->data[$this->position][$this->key];        if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {            $item = $item->value;        }        if (!$item instanceof Stub) {            return \gettype($item);        }        if (Stub::TYPE_STRING === $item->type) {            return 'string';        }        if (Stub::TYPE_ARRAY === $item->type) {            return 'array';        }        if (Stub::TYPE_OBJECT === $item->type) {            return $item->class;        }        if (Stub::TYPE_RESOURCE === $item->type) {            return $item->class.' resource';        }        return null;    }    /**     * @param array|bool $recursive Whether values should be resolved recursively or not     *     * @return string|int|float|bool|array|Data[]|null A native representation of the original value     */    public function getValue($recursive = false)    {        $item = $this->data[$this->position][$this->key];        if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {            $item = $item->value;        }        if (!($item = $this->getStub($item)) instanceof Stub) {            return $item;        }        if (Stub::TYPE_STRING === $item->type) {            return $item->value;        }        $children = $item->position ? $this->data[$item->position] : [];        foreach ($children as $k => $v) {            if ($recursive && !($v = $this->getStub($v)) instanceof Stub) {                continue;            }            $children[$k] = clone $this;            $children[$k]->key = $k;            $children[$k]->position = $item->position;            if ($recursive) {                if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) {                    $recursive = (array) $recursive;                    if (isset($recursive[$v->position])) {                        continue;                    }                    $recursive[$v->position] = true;                }                $children[$k] = $children[$k]->getValue($recursive);            }        }        return $children;    }    /**     * @return int     */    #[\ReturnTypeWillChange]    public function count()    {        return \count($this->getValue());    }    /**     * @return \Traversable     */    #[\ReturnTypeWillChange]    public function getIterator()    {        if (!\is_array($value = $this->getValue())) {            throw new \LogicException(sprintf('"%s" object holds non-iterable type "%s".', self::class, \gettype($value)));        }        yield from $value;    }    public function __get($key)    {        if (null !== $data = $this->seek($key)) {            $item = $this->getStub($data->data[$data->position][$data->key]);            return $item instanceof Stub || [] === $item ? $data : $item;        }        return null;    }    /**     * @return bool     */    public function __isset($key)    {        return null !== $this->seek($key);    }    /**     * @return bool     */    #[\ReturnTypeWillChange]    public function offsetExists($key)    {        return $this->__isset($key);    }    /**     * @return mixed     */    #[\ReturnTypeWillChange]    public function offsetGet($key)    {        return $this->__get($key);    }    /**     * @return void     */    #[\ReturnTypeWillChange]    public function offsetSet($key, $value)    {        throw new \BadMethodCallException(self::class.' objects are immutable.');    }    /**     * @return void     */    #[\ReturnTypeWillChange]    public function offsetUnset($key)    {        throw new \BadMethodCallException(self::class.' objects are immutable.');    }    /**     * @return string     */    public function __toString()    {        $value = $this->getValue();        if (!\is_array($value)) {            return (string) $value;        }        return sprintf('%s (count=%d)', $this->getType(), \count($value));    }    /**     * Returns a depth limited clone of $this.     *     * @param int $maxDepth The max dumped depth level     *     * @return static     */    public function withMaxDepth($maxDepth)    {        $data = clone $this;        $data->maxDepth = (int) $maxDepth;        return $data;    }    /**     * Limits the number of elements per depth level.     *     * @param int $maxItemsPerDepth The max number of items dumped per depth level     *     * @return static     */    public function withMaxItemsPerDepth($maxItemsPerDepth)    {        $data = clone $this;        $data->maxItemsPerDepth = (int) $maxItemsPerDepth;        return $data;    }    /**     * Enables/disables objects' identifiers tracking.     *     * @param bool $useRefHandles False to hide global ref. handles     *     * @return static     */    public function withRefHandles($useRefHandles)    {        $data = clone $this;        $data->useRefHandles = $useRefHandles ? -1 : 0;        return $data;    }    /**     * @return static     */    public function withContext(array $context)    {        $data = clone $this;        $data->context = $context;        return $data;    }    /**     * Seeks to a specific key in nested data structures.     *     * @param string|int $key The key to seek to     *     * @return static|null Null if the key is not set     */    public function seek($key)    {        $item = $this->data[$this->position][$this->key];        if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {            $item = $item->value;        }        if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) {            return null;        }        $keys = [$key];        switch ($item->type) {            case Stub::TYPE_OBJECT:                $keys[] = Caster::PREFIX_DYNAMIC.$key;                $keys[] = Caster::PREFIX_PROTECTED.$key;                $keys[] = Caster::PREFIX_VIRTUAL.$key;                $keys[] = "\0$item->class\0$key";                // no break            case Stub::TYPE_ARRAY:            case Stub::TYPE_RESOURCE:                break;            default:                return null;        }        $data = null;        $children = $this->data[$item->position];        foreach ($keys as $key) {            if (isset($children[$key]) || \array_key_exists($key, $children)) {                $data = clone $this;                $data->key = $key;                $data->position = $item->position;                break;            }        }        return $data;    }    /**     * Dumps data with a DumperInterface dumper.     */    public function dump(DumperInterface $dumper)    {        $refs = [0];        $cursor = new Cursor();        if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) {            $cursor->attr['if_links'] = true;            $cursor->hashType = -1;            $dumper->dumpScalar($cursor, 'default', '^');            $cursor->attr = ['if_links' => true];            $dumper->dumpScalar($cursor, 'default', ' ');            $cursor->hashType = 0;        }        $this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);    }    /**     * Depth-first dumping of items.     *     * @param mixed $item A Stub object or the original value being dumped     */    private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs, $item)    {        $cursor->refIndex = 0;        $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;        $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;        $firstSeen = true;        if (!$item instanceof Stub) {            $cursor->attr = [];            $type = \gettype($item);            if ($item && 'array' === $type) {                $item = $this->getStub($item);            }        } elseif (Stub::TYPE_REF === $item->type) {            if ($item->handle) {                if (!isset($refs[$r = $item->handle - (\PHP_INT_MAX >> 1)])) {                    $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];                } else {                    $firstSeen = false;                }                $cursor->hardRefTo = $refs[$r];                $cursor->hardRefHandle = $this->useRefHandles & $item->handle;                $cursor->hardRefCount = 0 < $item->handle ? $item->refCount : 0;            }            $cursor->attr = $item->attr;            $type = $item->class ?: \gettype($item->value);            $item = $this->getStub($item->value);        }        if ($item instanceof Stub) {            if ($item->refCount) {                if (!isset($refs[$r = $item->handle])) {                    $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];                } else {                    $firstSeen = false;                }                $cursor->softRefTo = $refs[$r];            }            $cursor->softRefHandle = $this->useRefHandles & $item->handle;            $cursor->softRefCount = $item->refCount;            $cursor->attr = $item->attr;            $cut = $item->cut;            if ($item->position && $firstSeen) {                $children = $this->data[$item->position];                if ($cursor->stop) {                    if ($cut >= 0) {                        $cut += \count($children);                    }                    $children = [];                }            } else {                $children = [];            }            switch ($item->type) {                case Stub::TYPE_STRING:                    $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);                    break;                case Stub::TYPE_ARRAY:                    $item = clone $item;                    $item->type = $item->class;                    $item->class = $item->value;                    // no break                case Stub::TYPE_OBJECT:                case Stub::TYPE_RESOURCE:                    $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;                    $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);                    if ($withChildren) {                        if ($cursor->skipChildren) {                            $withChildren = false;                            $cut = -1;                        } else {                            $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);                        }                    } elseif ($children && 0 <= $cut) {                        $cut += \count($children);                    }                    $cursor->skipChildren = false;                    $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);                    break;                default:                    throw new \RuntimeException(sprintf('Unexpected Stub type: "%s".', $item->type));            }        } 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);        }    }    /**     * Dumps children of hash structures.     *     * @return int The final number of removed items     */    private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, array &$refs, array $children, int $hashCut, int $hashType, bool $dumpKeys): int    {        $cursor = clone $parentCursor;        ++$cursor->depth;        $cursor->hashType = $hashType;        $cursor->hashIndex = 0;        $cursor->hashLength = \count($children);        $cursor->hashCut = $hashCut;        foreach ($children as $key => $child) {            $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);            $cursor->hashKey = $dumpKeys ? $key : null;            $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;            }        }        return $hashCut;    }    private function getStub($item)    {        if (!$item || !\is_array($item)) {            return $item;        }        $stub = new Stub();        $stub->type = Stub::TYPE_ARRAY;        foreach ($item as $stub->class => $stub->position) {        }        if (isset($item[0])) {            $stub->cut = $item[0];        }        $stub->value = $stub->cut + ($stub->position ? \count($this->data[$stub->position]) : 0);        return $stub;    }}
 |