AmqpCaster.php
2 years ago
ArgsStub.php
1 year ago
Caster.php
1 year ago
ClassStub.php
1 year ago
ConstStub.php
2 years ago
CutArrayStub.php
2 years ago
CutStub.php
1 year ago
DOMCaster.php
1 year ago
DateCaster.php
2 years ago
DoctrineCaster.php
2 years ago
DsCaster.php
2 years ago
DsPairStub.php
2 years ago
EnumStub.php
1 year ago
ExceptionCaster.php
1 year ago
FiberCaster.php
2 years ago
FrameStub.php
1 year ago
GmpCaster.php
2 years ago
ImagineCaster.php
2 years ago
ImgStub.php
2 years ago
IntlCaster.php
2 years ago
LinkStub.php
1 year ago
MemcachedCaster.php
2 years ago
MysqliCaster.php
2 years ago
PdoCaster.php
2 years ago
PgSqlCaster.php
1 year ago
ProxyManagerCaster.php
2 years ago
RdKafkaCaster.php
1 year ago
RedisCaster.php
2 years ago
ReflectionCaster.php
1 year ago
ResourceCaster.php
1 year ago
SplCaster.php
1 year ago
StubCaster.php
2 years ago
SymfonyCaster.php
2 years ago
TraceStub.php
1 year ago
UuidCaster.php
2 years ago
XmlReaderCaster.php
1 year ago
XmlResourceCaster.php
2 years ago
SplCaster.php
158 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\VarDumper\Caster; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\VarDumper\Cloner\Stub; |
| 14 | /** |
| 15 | * Casts SPL related classes to array representation. |
| 16 | * |
| 17 | * @author Nicolas Grekas <p@tchwork.com> |
| 18 | * |
| 19 | * @final |
| 20 | */ |
| 21 | class SplCaster |
| 22 | { |
| 23 | private const SPL_FILE_OBJECT_FLAGS = [\SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE', \SplFileObject::READ_AHEAD => 'READ_AHEAD', \SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY', \SplFileObject::READ_CSV => 'READ_CSV']; |
| 24 | public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, bool $isNested) |
| 25 | { |
| 26 | return self::castSplArray($c, $a, $stub, $isNested); |
| 27 | } |
| 28 | public static function castArrayIterator(\ArrayIterator $c, array $a, Stub $stub, bool $isNested) |
| 29 | { |
| 30 | return self::castSplArray($c, $a, $stub, $isNested); |
| 31 | } |
| 32 | public static function castHeap(\Iterator $c, array $a, Stub $stub, bool $isNested) |
| 33 | { |
| 34 | $a += [Caster::PREFIX_VIRTUAL . 'heap' => iterator_to_array(clone $c)]; |
| 35 | return $a; |
| 36 | } |
| 37 | public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, bool $isNested) |
| 38 | { |
| 39 | $prefix = Caster::PREFIX_VIRTUAL; |
| 40 | $mode = $c->getIteratorMode(); |
| 41 | $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE); |
| 42 | $a += [$prefix . 'mode' => new ConstStub(($mode & \SplDoublyLinkedList::IT_MODE_LIFO ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO') . ' | ' . ($mode & \SplDoublyLinkedList::IT_MODE_DELETE ? 'IT_MODE_DELETE' : 'IT_MODE_KEEP'), $mode), $prefix . 'dllist' => iterator_to_array($c)]; |
| 43 | $c->setIteratorMode($mode); |
| 44 | return $a; |
| 45 | } |
| 46 | public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, bool $isNested) |
| 47 | { |
| 48 | static $map = ['path' => 'getPath', 'filename' => 'getFilename', 'basename' => 'getBasename', 'pathname' => 'getPathname', 'extension' => 'getExtension', 'realPath' => 'getRealPath', 'aTime' => 'getATime', 'mTime' => 'getMTime', 'cTime' => 'getCTime', 'inode' => 'getInode', 'size' => 'getSize', 'perms' => 'getPerms', 'owner' => 'getOwner', 'group' => 'getGroup', 'type' => 'getType', 'writable' => 'isWritable', 'readable' => 'isReadable', 'executable' => 'isExecutable', 'file' => 'isFile', 'dir' => 'isDir', 'link' => 'isLink', 'linkTarget' => 'getLinkTarget']; |
| 49 | $prefix = Caster::PREFIX_VIRTUAL; |
| 50 | unset($a["\x00SplFileInfo\x00fileName"]); |
| 51 | unset($a["\x00SplFileInfo\x00pathName"]); |
| 52 | if (\PHP_VERSION_ID < 80000) { |
| 53 | if (\false === $c->getPathname()) { |
| 54 | $a[$prefix . '⚠'] = 'The parent constructor was not called: the object is in an invalid state'; |
| 55 | return $a; |
| 56 | } |
| 57 | } else { |
| 58 | try { |
| 59 | $c->isReadable(); |
| 60 | } catch (\RuntimeException $e) { |
| 61 | if ('Object not initialized' !== $e->getMessage()) { |
| 62 | throw $e; |
| 63 | } |
| 64 | $a[$prefix . '⚠'] = 'The parent constructor was not called: the object is in an invalid state'; |
| 65 | return $a; |
| 66 | } catch (\Error $e) { |
| 67 | if ('Object not initialized' !== $e->getMessage()) { |
| 68 | throw $e; |
| 69 | } |
| 70 | $a[$prefix . '⚠'] = 'The parent constructor was not called: the object is in an invalid state'; |
| 71 | return $a; |
| 72 | } |
| 73 | } |
| 74 | foreach ($map as $key => $accessor) { |
| 75 | try { |
| 76 | $a[$prefix . $key] = $c->{$accessor}(); |
| 77 | } catch (\Exception $e) { |
| 78 | } |
| 79 | } |
| 80 | if ($a[$prefix . 'realPath'] ?? \false) { |
| 81 | $a[$prefix . 'realPath'] = new LinkStub($a[$prefix . 'realPath']); |
| 82 | } |
| 83 | if (isset($a[$prefix . 'perms'])) { |
| 84 | $a[$prefix . 'perms'] = new ConstStub(sprintf('0%o', $a[$prefix . 'perms']), $a[$prefix . 'perms']); |
| 85 | } |
| 86 | static $mapDate = ['aTime', 'mTime', 'cTime']; |
| 87 | foreach ($mapDate as $key) { |
| 88 | if (isset($a[$prefix . $key])) { |
| 89 | $a[$prefix . $key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix . $key]), $a[$prefix . $key]); |
| 90 | } |
| 91 | } |
| 92 | return $a; |
| 93 | } |
| 94 | public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, bool $isNested) |
| 95 | { |
| 96 | static $map = ['csvControl' => 'getCsvControl', 'flags' => 'getFlags', 'maxLineLen' => 'getMaxLineLen', 'fstat' => 'fstat', 'eof' => 'eof', 'key' => 'key']; |
| 97 | $prefix = Caster::PREFIX_VIRTUAL; |
| 98 | foreach ($map as $key => $accessor) { |
| 99 | try { |
| 100 | $a[$prefix . $key] = $c->{$accessor}(); |
| 101 | } catch (\Exception $e) { |
| 102 | } |
| 103 | } |
| 104 | if (isset($a[$prefix . 'flags'])) { |
| 105 | $flagsArray = []; |
| 106 | foreach (self::SPL_FILE_OBJECT_FLAGS as $value => $name) { |
| 107 | if ($a[$prefix . 'flags'] & $value) { |
| 108 | $flagsArray[] = $name; |
| 109 | } |
| 110 | } |
| 111 | $a[$prefix . 'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix . 'flags']); |
| 112 | } |
| 113 | if (isset($a[$prefix . 'fstat'])) { |
| 114 | $a[$prefix . 'fstat'] = new CutArrayStub($a[$prefix . 'fstat'], ['dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks']); |
| 115 | } |
| 116 | return $a; |
| 117 | } |
| 118 | public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, bool $isNested) |
| 119 | { |
| 120 | $storage = []; |
| 121 | unset($a[Caster::PREFIX_DYNAMIC . "\x00gcdata"]); |
| 122 | // Don't hit https://bugs.php.net/65967 |
| 123 | unset($a["\x00SplObjectStorage\x00storage"]); |
| 124 | $clone = clone $c; |
| 125 | foreach ($clone as $obj) { |
| 126 | $storage[] = ['object' => $obj, 'info' => $clone->getInfo()]; |
| 127 | } |
| 128 | $a += [Caster::PREFIX_VIRTUAL . 'storage' => $storage]; |
| 129 | return $a; |
| 130 | } |
| 131 | public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub, bool $isNested) |
| 132 | { |
| 133 | $a[Caster::PREFIX_VIRTUAL . 'innerIterator'] = $c->getInnerIterator(); |
| 134 | return $a; |
| 135 | } |
| 136 | public static function castWeakReference(\WeakReference $c, array $a, Stub $stub, bool $isNested) |
| 137 | { |
| 138 | $a[Caster::PREFIX_VIRTUAL . 'object'] = $c->get(); |
| 139 | return $a; |
| 140 | } |
| 141 | private static function castSplArray($c, array $a, Stub $stub, bool $isNested) : array |
| 142 | { |
| 143 | $prefix = Caster::PREFIX_VIRTUAL; |
| 144 | $flags = $c->getFlags(); |
| 145 | if (!($flags & \ArrayObject::STD_PROP_LIST)) { |
| 146 | $c->setFlags(\ArrayObject::STD_PROP_LIST); |
| 147 | $a = Caster::castObject($c, \get_class($c), method_exists($c, '__debugInfo'), $stub->class); |
| 148 | $c->setFlags($flags); |
| 149 | } |
| 150 | unset($a["\x00ArrayObject\x00storage"], $a["\x00ArrayIterator\x00storage"]); |
| 151 | $a += [$prefix . 'storage' => $c->getArrayCopy(), $prefix . 'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST), $prefix . 'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS)]; |
| 152 | if ($c instanceof \ArrayObject) { |
| 153 | $a[$prefix . 'iteratorClass'] = new ClassStub($c->getIteratorClass()); |
| 154 | } |
| 155 | return $a; |
| 156 | } |
| 157 | } |
| 158 |