4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Cloner;
15 * @author Nicolas Grekas <p@tchwork.com>
20 private $maxDepth = 20;
21 private $maxItemsPerDepth = -1;
22 private $useRefHandles = -1;
25 * @param array $data A array as returned by ClonerInterface::cloneVar()
27 public function __construct(array $data)
33 * @return array The raw data structure
35 public function getRawData()
41 * Returns a depth limited clone of $this.
43 * @param int $maxDepth The max dumped depth level
45 * @return self A clone of $this
47 public function withMaxDepth($maxDepth)
50 $data->maxDepth = (int) $maxDepth;
56 * Limits the number of elements per depth level.
58 * @param int $maxItemsPerDepth The max number of items dumped per depth level
60 * @return self A clone of $this
62 public function withMaxItemsPerDepth($maxItemsPerDepth)
65 $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
71 * Enables/disables objects' identifiers tracking.
73 * @param bool $useRefHandles False to hide global ref. handles
75 * @return self A clone of $this
77 public function withRefHandles($useRefHandles)
80 $data->useRefHandles = $useRefHandles ? -1 : 0;
86 * Returns a depth limited clone of $this.
88 * @param int $maxDepth The max dumped depth level
89 * @param int $maxItemsPerDepth The max number of items dumped per depth level
90 * @param bool $useRefHandles False to hide ref. handles
92 * @return self A depth limited clone of $this
94 * @deprecated since Symfony 2.7, to be removed in 3.0. Use withMaxDepth, withMaxItemsPerDepth or withRefHandles instead.
96 public function getLimitedClone($maxDepth, $maxItemsPerDepth, $useRefHandles = true)
98 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0. Use withMaxDepth, withMaxItemsPerDepth or withRefHandles methods instead.', E_USER_DEPRECATED);
101 $data->maxDepth = (int) $maxDepth;
102 $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
103 $data->useRefHandles = $useRefHandles ? -1 : 0;
109 * Dumps data with a DumperInterface dumper.
111 public function dump(DumperInterface $dumper)
114 $this->dumpItem($dumper, new Cursor(), $refs, $this->data[0][0]);
118 * Depth-first dumping of items.
120 * @param DumperInterface $dumper The dumper being used for dumping
121 * @param Cursor $cursor A cursor used for tracking dumper state position
122 * @param array &$refs A map of all references discovered while dumping
123 * @param mixed $item A Stub object or the original value being dumped
125 private function dumpItem($dumper, $cursor, &$refs, $item)
127 $cursor->refIndex = 0;
128 $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
129 $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
132 if (!$item instanceof Stub) {
133 $type = gettype($item);
134 } elseif (Stub::TYPE_REF === $item->type) {
136 if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
137 $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
141 $cursor->hardRefTo = $refs[$r];
142 $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
143 $cursor->hardRefCount = $item->refCount;
145 $type = $item->class ?: gettype($item->value);
146 $item = $item->value;
148 if ($item instanceof Stub) {
149 if ($item->refCount) {
150 if (!isset($refs[$r = $item->handle])) {
151 $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
155 $cursor->softRefTo = $refs[$r];
157 $cursor->softRefHandle = $this->useRefHandles & $item->handle;
158 $cursor->softRefCount = $item->refCount;
161 if ($item->position && $firstSeen) {
162 $children = $this->data[$item->position];
166 $cut += count($children);
173 switch ($item->type) {
174 case Stub::TYPE_STRING:
175 $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
178 case Stub::TYPE_ARRAY:
180 $item->type = $item->class;
181 $item->class = $item->value;
183 case Stub::TYPE_OBJECT:
184 case Stub::TYPE_RESOURCE:
185 $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
186 $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
188 $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type);
189 } elseif ($children && 0 <= $cut) {
190 $cut += count($children);
192 $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
196 throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
198 } elseif ('array' === $type) {
199 $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
200 $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
201 } elseif ('string' === $type) {
202 $dumper->dumpString($cursor, $item, false, 0);
204 $dumper->dumpScalar($cursor, $type, $item);
209 * Dumps children of hash structures.
211 * @param DumperInterface $dumper
212 * @param Cursor $parentCursor The cursor of the parent hash
213 * @param array &$refs A map of all references discovered while dumping
214 * @param array $children The children to dump
215 * @param int $hashCut The number of items removed from the original hash
216 * @param string $hashType A Cursor::HASH_* const
218 * @return int The final number of removed items
220 private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
222 $cursor = clone $parentCursor;
224 $cursor->hashType = $hashType;
225 $cursor->hashIndex = 0;
226 $cursor->hashLength = count($children);
227 $cursor->hashCut = $hashCut;
228 foreach ($children as $key => $child) {
229 $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
230 $cursor->hashKey = $key;
231 $this->dumpItem($dumper, $cursor, $refs, $child);
232 if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
233 $parentCursor->stop = true;
235 return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;