Upgrade framework
This commit is contained in:
279
vendor/symfony/var-dumper/Cloner/Data.php
vendored
279
vendor/symfony/var-dumper/Cloner/Data.php
vendored
@@ -12,31 +12,30 @@
|
||||
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, \Serializable
|
||||
class Data implements \ArrayAccess, \Countable, \IteratorAggregate
|
||||
{
|
||||
private $data;
|
||||
private $position = 0;
|
||||
private $key = 0;
|
||||
private $maxDepth = 20;
|
||||
private $maxItemsPerDepth = -1;
|
||||
private $useRefHandles = -1;
|
||||
private array $data;
|
||||
private int $position = 0;
|
||||
private int|string $key = 0;
|
||||
private int $maxDepth = 20;
|
||||
private int $maxItemsPerDepth = -1;
|
||||
private int $useRefHandles = -1;
|
||||
private array $context = [];
|
||||
|
||||
/**
|
||||
* @param array $data A array as returned by ClonerInterface::cloneVar()
|
||||
* @param array $data An array as returned by ClonerInterface::cloneVar()
|
||||
*/
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The type of the value.
|
||||
*/
|
||||
public function getType()
|
||||
public function getType(): ?string
|
||||
{
|
||||
$item = $this->data[$this->position][$this->key];
|
||||
|
||||
@@ -44,7 +43,7 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
$item = $item->value;
|
||||
}
|
||||
if (!$item instanceof Stub) {
|
||||
return gettype($item);
|
||||
return \gettype($item);
|
||||
}
|
||||
if (Stub::TYPE_STRING === $item->type) {
|
||||
return 'string';
|
||||
@@ -58,31 +57,35 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
if (Stub::TYPE_RESOURCE === $item->type) {
|
||||
return $item->class.' resource';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $recursive Whether values should be resolved recursively or not.
|
||||
* Returns a native representation of the original value.
|
||||
*
|
||||
* @return scalar|array|null|Data[] A native representation of the original value.
|
||||
* @param array|bool $recursive Whether values should be resolved recursively or not
|
||||
*
|
||||
* @return string|int|float|bool|array|Data[]|null
|
||||
*/
|
||||
public function getValue($recursive = false)
|
||||
public function getValue(array|bool $recursive = false): string|int|float|bool|array|null
|
||||
{
|
||||
$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) {
|
||||
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] : array();
|
||||
$children = $item->position ? $this->data[$item->position] : [];
|
||||
|
||||
foreach ($children as $k => $v) {
|
||||
if ($recursive && !$v instanceof Stub) {
|
||||
if ($recursive && !($v = $this->getStub($v)) instanceof Stub) {
|
||||
continue;
|
||||
}
|
||||
$children[$k] = clone $this;
|
||||
@@ -90,12 +93,12 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
$children[$k]->position = $item->position;
|
||||
|
||||
if ($recursive) {
|
||||
if ($v instanceof Stub && Stub::TYPE_REF === $v->type && $v->value instanceof Stub) {
|
||||
if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) {
|
||||
$recursive = (array) $recursive;
|
||||
if (isset($recursive[$v->value->position])) {
|
||||
if (isset($recursive[$v->position])) {
|
||||
continue;
|
||||
}
|
||||
$recursive[$v->value->position] = true;
|
||||
$recursive[$v->position] = true;
|
||||
}
|
||||
$children[$k] = $children[$k]->getValue($recursive);
|
||||
}
|
||||
@@ -104,105 +107,85 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
return $children;
|
||||
}
|
||||
|
||||
public function count()
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->getValue());
|
||||
return \count($this->getValue());
|
||||
}
|
||||
|
||||
public function getIterator()
|
||||
public function getIterator(): \Traversable
|
||||
{
|
||||
if (!is_array($value = $this->getValue())) {
|
||||
throw new \LogicException(sprintf('%s object holds non-iterable type "%s".', self::class, gettype($value)));
|
||||
if (!\is_array($value = $this->getValue())) {
|
||||
throw new \LogicException(sprintf('"%s" object holds non-iterable type "%s".', self::class, get_debug_type($value)));
|
||||
}
|
||||
|
||||
foreach ($value as $k => $v) {
|
||||
yield $k => $v;
|
||||
}
|
||||
yield from $value;
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
public function __get(string $key)
|
||||
{
|
||||
if (null !== $data = $this->seek($key)) {
|
||||
$item = $data->data[$data->position][$data->key];
|
||||
$item = $this->getStub($data->data[$data->position][$data->key]);
|
||||
|
||||
return $item instanceof Stub || array() === $item ? $data : $item;
|
||||
return $item instanceof Stub || [] === $item ? $data : $item;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function __isset($key)
|
||||
public function __isset(string $key): bool
|
||||
{
|
||||
return null !== $this->seek($key);
|
||||
}
|
||||
|
||||
public function offsetExists($key)
|
||||
public function offsetExists(mixed $key): bool
|
||||
{
|
||||
return $this->__isset($key);
|
||||
}
|
||||
|
||||
public function offsetGet($key)
|
||||
public function offsetGet(mixed $key): mixed
|
||||
{
|
||||
return $this->__get($key);
|
||||
}
|
||||
|
||||
public function offsetSet($key, $value)
|
||||
public function offsetSet(mixed $key, mixed $value): void
|
||||
{
|
||||
throw new \BadMethodCallException(self::class.' objects are immutable.');
|
||||
}
|
||||
|
||||
public function offsetUnset($key)
|
||||
public function offsetUnset(mixed $key): void
|
||||
{
|
||||
throw new \BadMethodCallException(self::class.' objects are immutable.');
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
public function __toString(): string
|
||||
{
|
||||
$value = $this->getValue();
|
||||
|
||||
if (!is_array($value)) {
|
||||
if (!\is_array($value)) {
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
return sprintf('%s (count=%d)', $this->getType(), count($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array The raw data structure
|
||||
*
|
||||
* @deprecated since version 3.3. Use array or object access instead.
|
||||
*/
|
||||
public function getRawData()
|
||||
{
|
||||
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the array or object access instead.', __METHOD__));
|
||||
|
||||
return $this->data;
|
||||
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 self A clone of $this
|
||||
*/
|
||||
public function withMaxDepth($maxDepth)
|
||||
public function withMaxDepth(int $maxDepth): static
|
||||
{
|
||||
$data = clone $this;
|
||||
$data->maxDepth = (int) $maxDepth;
|
||||
$data->maxDepth = $maxDepth;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Limits the number of elements per depth level.
|
||||
*
|
||||
* @param int $maxItemsPerDepth The max number of items dumped per depth level
|
||||
*
|
||||
* @return self A clone of $this
|
||||
*/
|
||||
public function withMaxItemsPerDepth($maxItemsPerDepth)
|
||||
public function withMaxItemsPerDepth(int $maxItemsPerDepth): static
|
||||
{
|
||||
$data = clone $this;
|
||||
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;
|
||||
$data->maxItemsPerDepth = $maxItemsPerDepth;
|
||||
|
||||
return $data;
|
||||
}
|
||||
@@ -211,10 +194,8 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
* Enables/disables objects' identifiers tracking.
|
||||
*
|
||||
* @param bool $useRefHandles False to hide global ref. handles
|
||||
*
|
||||
* @return self A clone of $this
|
||||
*/
|
||||
public function withRefHandles($useRefHandles)
|
||||
public function withRefHandles(bool $useRefHandles): static
|
||||
{
|
||||
$data = clone $this;
|
||||
$data->useRefHandles = $useRefHandles ? -1 : 0;
|
||||
@@ -222,24 +203,28 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function withContext(array $context): static
|
||||
{
|
||||
$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 self|null A clone of $this of null if the key is not set
|
||||
*/
|
||||
public function seek($key)
|
||||
public function seek(string|int $key): ?static
|
||||
{
|
||||
$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 || !$item->position) {
|
||||
return;
|
||||
if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) {
|
||||
return null;
|
||||
}
|
||||
$keys = array($key);
|
||||
$keys = [$key];
|
||||
|
||||
switch ($item->type) {
|
||||
case Stub::TYPE_OBJECT:
|
||||
@@ -247,18 +232,19 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
$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;
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = null;
|
||||
$children = $this->data[$item->position];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (isset($children[$key]) || array_key_exists($key, $children)) {
|
||||
if (isset($children[$key]) || \array_key_exists($key, $children)) {
|
||||
$data = clone $this;
|
||||
$data->key = $key;
|
||||
$data->position = $item->position;
|
||||
@@ -274,70 +260,27 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
*/
|
||||
public function dump(DumperInterface $dumper)
|
||||
{
|
||||
$refs = array(0);
|
||||
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
|
||||
}
|
||||
$refs = [0];
|
||||
$cursor = new Cursor();
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
$data = $this->data;
|
||||
|
||||
foreach ($data as $i => $values) {
|
||||
foreach ($values as $k => $v) {
|
||||
if ($v instanceof Stub) {
|
||||
if (Stub::TYPE_ARRAY === $v->type) {
|
||||
$v = self::mapStubConsts($v, false);
|
||||
$data[$i][$k] = array($v->class, $v->position, $v->cut);
|
||||
} else {
|
||||
$v = self::mapStubConsts($v, false);
|
||||
$data[$i][$k] = array($v->class, $v->position, $v->cut, $v->type, $v->value, $v->handle, $v->refCount, $v->attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return serialize(array($data, $this->position, $this->key, $this->maxDepth, $this->maxItemsPerDepth, $this->useRefHandles));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
list($data, $this->position, $this->key, $this->maxDepth, $this->maxItemsPerDepth, $this->useRefHandles) = unserialize($serialized);
|
||||
|
||||
foreach ($data as $i => $values) {
|
||||
foreach ($values as $k => $v) {
|
||||
if ($v && is_array($v)) {
|
||||
$s = new Stub();
|
||||
if (3 === count($v)) {
|
||||
$s->type = Stub::TYPE_ARRAY;
|
||||
$s = self::mapStubConsts($s, false);
|
||||
list($s->class, $s->position, $s->cut) = $v;
|
||||
$s->value = $s->cut + count($data[$s->position]);
|
||||
} else {
|
||||
list($s->class, $s->position, $s->cut, $s->type, $s->value, $s->handle, $s->refCount, $s->attr) = $v;
|
||||
}
|
||||
$data[$i][$k] = self::mapStubConsts($s, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->data = $data;
|
||||
$this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Depth-first dumping of items.
|
||||
*
|
||||
* @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
|
||||
* @param mixed $item A Stub object or the original value being dumped
|
||||
*/
|
||||
private function dumpItem($dumper, $cursor, &$refs, $item)
|
||||
private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs, mixed $item)
|
||||
{
|
||||
$cursor->refIndex = 0;
|
||||
$cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
|
||||
@@ -345,22 +288,25 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
$firstSeen = true;
|
||||
|
||||
if (!$item instanceof Stub) {
|
||||
$cursor->attr = array();
|
||||
$type = gettype($item);
|
||||
$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)])) {
|
||||
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 = $item->refCount;
|
||||
$cursor->hardRefCount = 0 < $item->handle ? $item->refCount : 0;
|
||||
}
|
||||
$cursor->attr = $item->attr;
|
||||
$type = $item->class ?: gettype($item->value);
|
||||
$item = $item->value;
|
||||
$type = $item->class ?: \gettype($item->value);
|
||||
$item = $this->getStub($item->value);
|
||||
}
|
||||
if ($item instanceof Stub) {
|
||||
if ($item->refCount) {
|
||||
@@ -381,12 +327,12 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
|
||||
if ($cursor->stop) {
|
||||
if ($cut >= 0) {
|
||||
$cut += count($children);
|
||||
$cut += \count($children);
|
||||
}
|
||||
$children = array();
|
||||
$children = [];
|
||||
}
|
||||
} else {
|
||||
$children = array();
|
||||
$children = [];
|
||||
}
|
||||
switch ($item->type) {
|
||||
case Stub::TYPE_STRING:
|
||||
@@ -397,21 +343,27 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
$item = clone $item;
|
||||
$item->type = $item->class;
|
||||
$item->class = $item->value;
|
||||
// No break;
|
||||
// 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) {
|
||||
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
|
||||
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);
|
||||
$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));
|
||||
throw new \RuntimeException(sprintf('Unexpected Stub type: "%s".', $item->type));
|
||||
}
|
||||
} elseif ('array' === $type) {
|
||||
$dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
|
||||
@@ -426,23 +378,15 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
/**
|
||||
* Dumps children of hash structures.
|
||||
*
|
||||
* @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
|
||||
* @param bool $dumpKeys Whether keys should be dumped or not
|
||||
*
|
||||
* @return int The final number of removed items
|
||||
*/
|
||||
private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType, $dumpKeys)
|
||||
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->hashLength = \count($children);
|
||||
$cursor->hashCut = $hashCut;
|
||||
foreach ($children as $key => $child) {
|
||||
$cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
|
||||
@@ -458,21 +402,20 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializabl
|
||||
return $hashCut;
|
||||
}
|
||||
|
||||
private static function mapStubConsts(Stub $stub, $resolve)
|
||||
private function getStub(mixed $item)
|
||||
{
|
||||
static $stubConstIndexes, $stubConstValues;
|
||||
|
||||
if (null === $stubConstIndexes) {
|
||||
$r = new \ReflectionClass(Stub::class);
|
||||
$stubConstIndexes = array_flip(array_values($r->getConstants()));
|
||||
$stubConstValues = array_flip($stubConstIndexes);
|
||||
if (!$item || !\is_array($item)) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
$map = $resolve ? $stubConstValues : $stubConstIndexes;
|
||||
|
||||
$stub = clone $stub;
|
||||
$stub->type = $map[$stub->type];
|
||||
$stub->class = isset($map[$stub->class]) ? $map[$stub->class] : $stub->class;
|
||||
$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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user