Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\VarDumper\Cloner; |
| 13 | |
| 14 | use Symfony\Component\VarDumper\Caster\Caster; |
| 15 | use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider; |
| 16 | |
| 17 | /** |
| 18 | * @author Nicolas Grekas <p@tchwork.com> |
| 19 | */ |
| 20 | class Data implements \ArrayAccess, \Countable, \IteratorAggregate |
| 21 | { |
| 22 | private $data; |
| 23 | private $position = 0; |
| 24 | private $key = 0; |
| 25 | private $maxDepth = 20; |
| 26 | private $maxItemsPerDepth = -1; |
| 27 | private $useRefHandles = -1; |
| 28 | private $context = []; |
| 29 | |
| 30 | /** |
| 31 | * @param array $data An array as returned by ClonerInterface::cloneVar() |
| 32 | */ |
| 33 | public function __construct(array $data) |
| 34 | { |
| 35 | $this->data = $data; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return string|null The type of the value |
| 40 | */ |
| 41 | public function getType() |
| 42 | { |
| 43 | $item = $this->data[$this->position][$this->key]; |
| 44 | |
| 45 | if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) { |
| 46 | $item = $item->value; |
| 47 | } |
| 48 | if (!$item instanceof Stub) { |
| 49 | return \gettype($item); |
| 50 | } |
| 51 | if (Stub::TYPE_STRING === $item->type) { |
| 52 | return 'string'; |
| 53 | } |
| 54 | if (Stub::TYPE_ARRAY === $item->type) { |
| 55 | return 'array'; |
| 56 | } |
| 57 | if (Stub::TYPE_OBJECT === $item->type) { |
| 58 | return $item->class; |
| 59 | } |
| 60 | if (Stub::TYPE_RESOURCE === $item->type) { |
| 61 | return $item->class.' resource'; |
| 62 | } |
| 63 | |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param array|bool $recursive Whether values should be resolved recursively or not |
| 69 | * |
| 70 | * @return string|int|float|bool|array|Data[]|null A native representation of the original value |
| 71 | */ |
| 72 | public function getValue($recursive = false) |
| 73 | { |
| 74 | $item = $this->data[$this->position][$this->key]; |
| 75 | |
| 76 | if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) { |
| 77 | $item = $item->value; |
| 78 | } |
| 79 | if (!($item = $this->getStub($item)) instanceof Stub) { |
| 80 | return $item; |
| 81 | } |
| 82 | if (Stub::TYPE_STRING === $item->type) { |
| 83 | return $item->value; |
| 84 | } |
| 85 | |
| 86 | $children = $item->position ? $this->data[$item->position] : []; |
| 87 | |
| 88 | foreach ($children as $k => $v) { |
| 89 | if ($recursive && !($v = $this->getStub($v)) instanceof Stub) { |
| 90 | continue; |
| 91 | } |
| 92 | $children[$k] = clone $this; |
| 93 | $children[$k]->key = $k; |
| 94 | $children[$k]->position = $item->position; |
| 95 | |
| 96 | if ($recursive) { |
| 97 | if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) { |
| 98 | $recursive = (array) $recursive; |
| 99 | if (isset($recursive[$v->position])) { |
| 100 | continue; |
| 101 | } |
| 102 | $recursive[$v->position] = true; |
| 103 | } |
| 104 | $children[$k] = $children[$k]->getValue($recursive); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $children; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return int |
| 113 | */ |
| 114 | public function count() |
| 115 | { |
| 116 | return \count($this->getValue()); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return \Traversable |
| 121 | */ |
| 122 | public function getIterator() |
| 123 | { |
| 124 | if (!\is_array($value = $this->getValue())) { |
| 125 | throw new \LogicException(sprintf('"%s" object holds non-iterable type "%s".', self::class, get_debug_type($value))); |
| 126 | } |
| 127 | |
| 128 | yield from $value; |
| 129 | } |
| 130 | |
| 131 | public function __get(string $key) |
| 132 | { |
| 133 | if (null !== $data = $this->seek($key)) { |
| 134 | $item = $this->getStub($data->data[$data->position][$data->key]); |
| 135 | |
| 136 | return $item instanceof Stub || [] === $item ? $data : $item; |
| 137 | } |
| 138 | |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return bool |
| 144 | */ |
| 145 | public function __isset(string $key) |
| 146 | { |
| 147 | return null !== $this->seek($key); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return bool |
| 152 | */ |
| 153 | public function offsetExists($key) |
| 154 | { |
| 155 | return $this->__isset($key); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @return mixed |
| 160 | */ |
| 161 | public function offsetGet($key) |
| 162 | { |
| 163 | return $this->__get($key); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @return void |
| 168 | */ |
| 169 | public function offsetSet($key, $value) |
| 170 | { |
| 171 | throw new \BadMethodCallException(self::class.' objects are immutable.'); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @return void |
| 176 | */ |
| 177 | public function offsetUnset($key) |
| 178 | { |
| 179 | throw new \BadMethodCallException(self::class.' objects are immutable.'); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @return string |
| 184 | */ |
| 185 | public function __toString() |
| 186 | { |
| 187 | $value = $this->getValue(); |
| 188 | |
| 189 | if (!\is_array($value)) { |
| 190 | return (string) $value; |
| 191 | } |
| 192 | |
| 193 | return sprintf('%s (count=%d)', $this->getType(), \count($value)); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns a depth limited clone of $this. |
| 198 | * |
| 199 | * @return static |
| 200 | */ |
| 201 | public function withMaxDepth(int $maxDepth) |
| 202 | { |
| 203 | $data = clone $this; |
| 204 | $data->maxDepth = (int) $maxDepth; |
| 205 | |
| 206 | return $data; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Limits the number of elements per depth level. |
| 211 | * |
| 212 | * @return static |
| 213 | */ |
| 214 | public function withMaxItemsPerDepth(int $maxItemsPerDepth) |
| 215 | { |
| 216 | $data = clone $this; |
| 217 | $data->maxItemsPerDepth = (int) $maxItemsPerDepth; |
| 218 | |
| 219 | return $data; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Enables/disables objects' identifiers tracking. |
| 224 | * |
| 225 | * @param bool $useRefHandles False to hide global ref. handles |
| 226 | * |
| 227 | * @return static |
| 228 | */ |
| 229 | public function withRefHandles(bool $useRefHandles) |
| 230 | { |
| 231 | $data = clone $this; |
| 232 | $data->useRefHandles = $useRefHandles ? -1 : 0; |
| 233 | |
| 234 | return $data; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @return static |
| 239 | */ |
| 240 | public function withContext(array $context) |
| 241 | { |
| 242 | $data = clone $this; |
| 243 | $data->context = $context; |
| 244 | |
| 245 | return $data; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Seeks to a specific key in nested data structures. |
| 250 | * |
| 251 | * @param string|int $key The key to seek to |
| 252 | * |
| 253 | * @return static|null Null if the key is not set |
| 254 | */ |
| 255 | public function seek($key) |
| 256 | { |
| 257 | $item = $this->data[$this->position][$this->key]; |
| 258 | |
| 259 | if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) { |
| 260 | $item = $item->value; |
| 261 | } |
| 262 | if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) { |
| 263 | return null; |
| 264 | } |
| 265 | $keys = [$key]; |
| 266 | |
| 267 | switch ($item->type) { |
| 268 | case Stub::TYPE_OBJECT: |
| 269 | $keys[] = Caster::PREFIX_DYNAMIC.$key; |
| 270 | $keys[] = Caster::PREFIX_PROTECTED.$key; |
| 271 | $keys[] = Caster::PREFIX_VIRTUAL.$key; |
| 272 | $keys[] = "\0$item->class\0$key"; |
| 273 | // no break |
| 274 | case Stub::TYPE_ARRAY: |
| 275 | case Stub::TYPE_RESOURCE: |
| 276 | break; |
| 277 | default: |
| 278 | return null; |
| 279 | } |
| 280 | |
| 281 | $data = null; |
| 282 | $children = $this->data[$item->position]; |
| 283 | |
| 284 | foreach ($keys as $key) { |
| 285 | if (isset($children[$key]) || \array_key_exists($key, $children)) { |
| 286 | $data = clone $this; |
| 287 | $data->key = $key; |
| 288 | $data->position = $item->position; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return $data; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Dumps data with a DumperInterface dumper. |
| 298 | */ |
| 299 | public function dump(DumperInterface $dumper) |
| 300 | { |
| 301 | $refs = [0]; |
| 302 | $cursor = new Cursor(); |
| 303 | |
| 304 | if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) { |
| 305 | $cursor->attr['if_links'] = true; |
| 306 | $cursor->hashType = -1; |
| 307 | $dumper->dumpScalar($cursor, 'default', '^'); |
| 308 | $cursor->attr = ['if_links' => true]; |
| 309 | $dumper->dumpScalar($cursor, 'default', ' '); |
| 310 | $cursor->hashType = 0; |
| 311 | } |
| 312 | |
| 313 | $this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Depth-first dumping of items. |
| 318 | * |
| 319 | * @param mixed $item A Stub object or the original value being dumped |
| 320 | */ |
| 321 | private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs, $item) |
| 322 | { |
| 323 | $cursor->refIndex = 0; |
| 324 | $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0; |
| 325 | $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0; |
| 326 | $firstSeen = true; |
| 327 | |
| 328 | if (!$item instanceof Stub) { |
| 329 | $cursor->attr = []; |
| 330 | $type = \gettype($item); |
| 331 | if ($item && 'array' === $type) { |
| 332 | $item = $this->getStub($item); |
| 333 | } |
| 334 | } elseif (Stub::TYPE_REF === $item->type) { |
| 335 | if ($item->handle) { |
| 336 | if (!isset($refs[$r = $item->handle - (\PHP_INT_MAX >> 1)])) { |
| 337 | $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0]; |
| 338 | } else { |
| 339 | $firstSeen = false; |
| 340 | } |
| 341 | $cursor->hardRefTo = $refs[$r]; |
| 342 | $cursor->hardRefHandle = $this->useRefHandles & $item->handle; |
| 343 | $cursor->hardRefCount = 0 < $item->handle ? $item->refCount : 0; |
| 344 | } |
| 345 | $cursor->attr = $item->attr; |
| 346 | $type = $item->class ?: \gettype($item->value); |
| 347 | $item = $this->getStub($item->value); |
| 348 | } |
| 349 | if ($item instanceof Stub) { |
| 350 | if ($item->refCount) { |
| 351 | if (!isset($refs[$r = $item->handle])) { |
| 352 | $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0]; |
| 353 | } else { |
| 354 | $firstSeen = false; |
| 355 | } |
| 356 | $cursor->softRefTo = $refs[$r]; |
| 357 | } |
| 358 | $cursor->softRefHandle = $this->useRefHandles & $item->handle; |
| 359 | $cursor->softRefCount = $item->refCount; |
| 360 | $cursor->attr = $item->attr; |
| 361 | $cut = $item->cut; |
| 362 | |
| 363 | if ($item->position && $firstSeen) { |
| 364 | $children = $this->data[$item->position]; |
| 365 | |
| 366 | if ($cursor->stop) { |
| 367 | if ($cut >= 0) { |
| 368 | $cut += \count($children); |
| 369 | } |
| 370 | $children = []; |
| 371 | } |
| 372 | } else { |
| 373 | $children = []; |
| 374 | } |
| 375 | switch ($item->type) { |
| 376 | case Stub::TYPE_STRING: |
| 377 | $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut); |
| 378 | break; |
| 379 | |
| 380 | case Stub::TYPE_ARRAY: |
| 381 | $item = clone $item; |
| 382 | $item->type = $item->class; |
| 383 | $item->class = $item->value; |
| 384 | // no break |
| 385 | case Stub::TYPE_OBJECT: |
| 386 | case Stub::TYPE_RESOURCE: |
| 387 | $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth; |
| 388 | $dumper->enterHash($cursor, $item->type, $item->class, $withChildren); |
| 389 | if ($withChildren) { |
| 390 | if ($cursor->skipChildren) { |
| 391 | $withChildren = false; |
| 392 | $cut = -1; |
| 393 | } else { |
| 394 | $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class); |
| 395 | } |
| 396 | } elseif ($children && 0 <= $cut) { |
| 397 | $cut += \count($children); |
| 398 | } |
| 399 | $cursor->skipChildren = false; |
| 400 | $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut); |
| 401 | break; |
| 402 | |
| 403 | default: |
| 404 | throw new \RuntimeException(sprintf('Unexpected Stub type: "%s".', $item->type)); |
| 405 | } |
| 406 | } elseif ('array' === $type) { |
| 407 | $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false); |
| 408 | $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0); |
| 409 | } elseif ('string' === $type) { |
| 410 | $dumper->dumpString($cursor, $item, false, 0); |
| 411 | } else { |
| 412 | $dumper->dumpScalar($cursor, $type, $item); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Dumps children of hash structures. |
| 418 | * |
| 419 | * @return int The final number of removed items |
| 420 | */ |
| 421 | private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, array &$refs, array $children, int $hashCut, int $hashType, bool $dumpKeys): int |
| 422 | { |
| 423 | $cursor = clone $parentCursor; |
| 424 | ++$cursor->depth; |
| 425 | $cursor->hashType = $hashType; |
| 426 | $cursor->hashIndex = 0; |
| 427 | $cursor->hashLength = \count($children); |
| 428 | $cursor->hashCut = $hashCut; |
| 429 | foreach ($children as $key => $child) { |
| 430 | $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key); |
| 431 | $cursor->hashKey = $dumpKeys ? $key : null; |
| 432 | $this->dumpItem($dumper, $cursor, $refs, $child); |
| 433 | if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) { |
| 434 | $parentCursor->stop = true; |
| 435 | |
| 436 | return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return $hashCut; |
| 441 | } |
| 442 | |
| 443 | private function getStub($item) |
| 444 | { |
| 445 | if (!$item || !\is_array($item)) { |
| 446 | return $item; |
| 447 | } |
| 448 | |
| 449 | $stub = new Stub(); |
| 450 | $stub->type = Stub::TYPE_ARRAY; |
| 451 | foreach ($item as $stub->class => $stub->position) { |
| 452 | } |
| 453 | if (isset($item[0])) { |
| 454 | $stub->cut = $item[0]; |
| 455 | } |
| 456 | $stub->value = $stub->cut + ($stub->position ? \count($this->data[$stub->position]) : 0); |
| 457 | |
| 458 | return $stub; |
| 459 | } |
| 460 | } |