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\Exception\ThrowingCasterException; |
| 16 | |
| 17 | /** |
| 18 | * AbstractCloner implements a generic caster mechanism for objects and resources. |
| 19 | * |
| 20 | * @author Nicolas Grekas <p@tchwork.com> |
| 21 | */ |
| 22 | abstract class AbstractCloner implements ClonerInterface |
| 23 | { |
| 24 | public static $defaultCasters = [ |
| 25 | '__PHP_Incomplete_Class' => ['Symfony\Component\VarDumper\Caster\Caster', 'castPhpIncompleteClass'], |
| 26 | |
| 27 | 'Symfony\Component\VarDumper\Caster\CutStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'], |
| 28 | 'Symfony\Component\VarDumper\Caster\CutArrayStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castCutArray'], |
| 29 | 'Symfony\Component\VarDumper\Caster\ConstStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'], |
| 30 | 'Symfony\Component\VarDumper\Caster\EnumStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castEnum'], |
| 31 | |
| 32 | 'Closure' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClosure'], |
| 33 | 'Generator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castGenerator'], |
| 34 | 'ReflectionType' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castType'], |
| 35 | 'ReflectionAttribute' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castAttribute'], |
| 36 | 'ReflectionGenerator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReflectionGenerator'], |
| 37 | 'ReflectionClass' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClass'], |
| 38 | 'ReflectionClassConstant' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClassConstant'], |
| 39 | 'ReflectionFunctionAbstract' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castFunctionAbstract'], |
| 40 | 'ReflectionMethod' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castMethod'], |
| 41 | 'ReflectionParameter' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castParameter'], |
| 42 | 'ReflectionProperty' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castProperty'], |
| 43 | 'ReflectionReference' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReference'], |
| 44 | 'ReflectionExtension' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castExtension'], |
| 45 | 'ReflectionZendExtension' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castZendExtension'], |
| 46 | |
| 47 | 'Doctrine\Common\Persistence\ObjectManager' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 48 | 'Doctrine\Common\Proxy\Proxy' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castCommonProxy'], |
| 49 | 'Doctrine\ORM\Proxy\Proxy' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castOrmProxy'], |
| 50 | 'Doctrine\ORM\PersistentCollection' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castPersistentCollection'], |
| 51 | 'Doctrine\Persistence\ObjectManager' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 52 | |
| 53 | 'DOMException' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castException'], |
| 54 | 'DOMStringList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'], |
| 55 | 'DOMNameList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'], |
| 56 | 'DOMImplementation' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castImplementation'], |
| 57 | 'DOMImplementationList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'], |
| 58 | 'DOMNode' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castNode'], |
| 59 | 'DOMNameSpaceNode' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castNameSpaceNode'], |
| 60 | 'DOMDocument' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDocument'], |
| 61 | 'DOMNodeList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'], |
| 62 | 'DOMNamedNodeMap' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castLength'], |
| 63 | 'DOMCharacterData' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castCharacterData'], |
| 64 | 'DOMAttr' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castAttr'], |
| 65 | 'DOMElement' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castElement'], |
| 66 | 'DOMText' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castText'], |
| 67 | 'DOMTypeinfo' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castTypeinfo'], |
| 68 | 'DOMDomError' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDomError'], |
| 69 | 'DOMLocator' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castLocator'], |
| 70 | 'DOMDocumentType' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDocumentType'], |
| 71 | 'DOMNotation' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castNotation'], |
| 72 | 'DOMEntity' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castEntity'], |
| 73 | 'DOMProcessingInstruction' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castProcessingInstruction'], |
| 74 | 'DOMXPath' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castXPath'], |
| 75 | |
| 76 | 'XMLReader' => ['Symfony\Component\VarDumper\Caster\XmlReaderCaster', 'castXmlReader'], |
| 77 | |
| 78 | 'ErrorException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castErrorException'], |
| 79 | 'Exception' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castException'], |
| 80 | 'Error' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castError'], |
| 81 | 'Symfony\Bridge\Monolog\Logger' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 82 | 'Symfony\Component\DependencyInjection\ContainerInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 83 | 'Symfony\Component\EventDispatcher\EventDispatcherInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 84 | 'Symfony\Component\HttpClient\CurlHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClient'], |
| 85 | 'Symfony\Component\HttpClient\NativeHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClient'], |
| 86 | 'Symfony\Component\HttpClient\Response\CurlResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'], |
| 87 | 'Symfony\Component\HttpClient\Response\NativeResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'], |
| 88 | 'Symfony\Component\HttpFoundation\Request' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castRequest'], |
| 89 | 'Symfony\Component\VarDumper\Exception\ThrowingCasterException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castThrowingCasterException'], |
| 90 | 'Symfony\Component\VarDumper\Caster\TraceStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castTraceStub'], |
| 91 | 'Symfony\Component\VarDumper\Caster\FrameStub' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castFrameStub'], |
| 92 | 'Symfony\Component\VarDumper\Cloner\AbstractCloner' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 93 | 'Symfony\Component\ErrorHandler\Exception\SilencedErrorContext' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castSilencedErrorContext'], |
| 94 | |
| 95 | 'Imagine\Image\ImageInterface' => ['Symfony\Component\VarDumper\Caster\ImagineCaster', 'castImage'], |
| 96 | |
| 97 | 'Ramsey\Uuid\UuidInterface' => ['Symfony\Component\VarDumper\Caster\UuidCaster', 'castRamseyUuid'], |
| 98 | |
| 99 | 'ProxyManager\Proxy\ProxyInterface' => ['Symfony\Component\VarDumper\Caster\ProxyManagerCaster', 'castProxy'], |
| 100 | 'PHPUnit_Framework_MockObject_MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 101 | 'PHPUnit\Framework\MockObject\MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 102 | 'PHPUnit\Framework\MockObject\Stub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 103 | 'Prophecy\Prophecy\ProphecySubjectInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 104 | 'Mockery\MockInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'], |
| 105 | |
| 106 | 'PDO' => ['Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdo'], |
| 107 | 'PDOStatement' => ['Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdoStatement'], |
| 108 | |
| 109 | 'AMQPConnection' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castConnection'], |
| 110 | 'AMQPChannel' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castChannel'], |
| 111 | 'AMQPQueue' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castQueue'], |
| 112 | 'AMQPExchange' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castExchange'], |
| 113 | 'AMQPEnvelope' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castEnvelope'], |
| 114 | |
| 115 | 'ArrayObject' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayObject'], |
| 116 | 'ArrayIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayIterator'], |
| 117 | 'SplDoublyLinkedList' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castDoublyLinkedList'], |
| 118 | 'SplFileInfo' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileInfo'], |
| 119 | 'SplFileObject' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileObject'], |
| 120 | 'SplHeap' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'], |
| 121 | 'SplObjectStorage' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'], |
| 122 | 'SplPriorityQueue' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'], |
| 123 | 'OuterIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castOuterIterator'], |
| 124 | 'WeakReference' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castWeakReference'], |
| 125 | |
| 126 | 'Redis' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'], |
| 127 | 'RedisArray' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'], |
| 128 | 'RedisCluster' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisCluster'], |
| 129 | |
| 130 | 'DateTimeInterface' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'], |
| 131 | 'DateInterval' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'], |
| 132 | 'DateTimeZone' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'], |
| 133 | 'DatePeriod' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'], |
| 134 | |
| 135 | 'GMP' => ['Symfony\Component\VarDumper\Caster\GmpCaster', 'castGmp'], |
| 136 | |
| 137 | 'MessageFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castMessageFormatter'], |
| 138 | 'NumberFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castNumberFormatter'], |
| 139 | 'IntlTimeZone' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlTimeZone'], |
| 140 | 'IntlCalendar' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlCalendar'], |
| 141 | 'IntlDateFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlDateFormatter'], |
| 142 | |
| 143 | 'Memcached' => ['Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'], |
| 144 | |
| 145 | 'Ds\Collection' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castCollection'], |
| 146 | 'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castMap'], |
| 147 | 'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPair'], |
| 148 | 'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'], |
| 149 | |
| 150 | 'CurlHandle' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], |
| 151 | ':curl' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'], |
| 152 | |
| 153 | ':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'], |
| 154 | ':dba persistent' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'], |
| 155 | |
| 156 | 'GdImage' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'], |
| 157 | ':gd' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'], |
| 158 | |
| 159 | ':mysql link' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castMysqlLink'], |
| 160 | ':pgsql large object' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'], |
| 161 | ':pgsql link' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'], |
| 162 | ':pgsql link persistent' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'], |
| 163 | ':pgsql result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'], |
| 164 | ':process' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'], |
| 165 | ':stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'], |
| 166 | |
| 167 | 'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslX509'], |
| 168 | ':OpenSSL X.509' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslX509'], |
| 169 | |
| 170 | ':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'], |
| 171 | ':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'], |
| 172 | |
| 173 | 'XmlParser' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'], |
| 174 | ':xml' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'], |
| 175 | |
| 176 | 'RdKafka' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castRdKafka'], |
| 177 | 'RdKafka\Conf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castConf'], |
| 178 | 'RdKafka\KafkaConsumer' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castKafkaConsumer'], |
| 179 | 'RdKafka\Metadata\Broker' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castBrokerMetadata'], |
| 180 | 'RdKafka\Metadata\Collection' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castCollectionMetadata'], |
| 181 | 'RdKafka\Metadata\Partition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castPartitionMetadata'], |
| 182 | 'RdKafka\Metadata\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicMetadata'], |
| 183 | 'RdKafka\Message' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castMessage'], |
| 184 | 'RdKafka\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopic'], |
| 185 | 'RdKafka\TopicPartition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicPartition'], |
| 186 | 'RdKafka\TopicConf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicConf'], |
| 187 | ]; |
| 188 | |
| 189 | protected $maxItems = 2500; |
| 190 | protected $maxString = -1; |
| 191 | protected $minDepth = 1; |
| 192 | |
| 193 | private $casters = []; |
| 194 | private $prevErrorHandler; |
| 195 | private $classInfo = []; |
| 196 | private $filter = 0; |
| 197 | |
| 198 | /** |
| 199 | * @param callable[]|null $casters A map of casters |
| 200 | * |
| 201 | * @see addCasters |
| 202 | */ |
| 203 | public function __construct(array $casters = null) |
| 204 | { |
| 205 | if (null === $casters) { |
| 206 | $casters = static::$defaultCasters; |
| 207 | } |
| 208 | $this->addCasters($casters); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Adds casters for resources and objects. |
| 213 | * |
| 214 | * Maps resources or objects types to a callback. |
| 215 | * Types are in the key, with a callable caster for value. |
| 216 | * Resource types are to be prefixed with a `:`, |
| 217 | * see e.g. static::$defaultCasters. |
| 218 | * |
| 219 | * @param callable[] $casters A map of casters |
| 220 | */ |
| 221 | public function addCasters(array $casters) |
| 222 | { |
| 223 | foreach ($casters as $type => $callback) { |
| 224 | $this->casters[$type][] = $callback; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Sets the maximum number of items to clone past the minimum depth in nested structures. |
| 230 | */ |
| 231 | public function setMaxItems(int $maxItems) |
| 232 | { |
| 233 | $this->maxItems = $maxItems; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Sets the maximum cloned length for strings. |
| 238 | */ |
| 239 | public function setMaxString(int $maxString) |
| 240 | { |
| 241 | $this->maxString = $maxString; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets the minimum tree depth where we are guaranteed to clone all the items. After this |
| 246 | * depth is reached, only setMaxItems items will be cloned. |
| 247 | */ |
| 248 | public function setMinDepth(int $minDepth) |
| 249 | { |
| 250 | $this->minDepth = $minDepth; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Clones a PHP variable. |
| 255 | * |
| 256 | * @param mixed $var Any PHP variable |
| 257 | * @param int $filter A bit field of Caster::EXCLUDE_* constants |
| 258 | * |
| 259 | * @return Data The cloned variable represented by a Data object |
| 260 | */ |
| 261 | public function cloneVar($var, int $filter = 0) |
| 262 | { |
| 263 | $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) { |
| 264 | if (\E_RECOVERABLE_ERROR === $type || \E_USER_ERROR === $type) { |
| 265 | // Cloner never dies |
| 266 | throw new \ErrorException($msg, 0, $type, $file, $line); |
| 267 | } |
| 268 | |
| 269 | if ($this->prevErrorHandler) { |
| 270 | return ($this->prevErrorHandler)($type, $msg, $file, $line, $context); |
| 271 | } |
| 272 | |
| 273 | return false; |
| 274 | }); |
| 275 | $this->filter = $filter; |
| 276 | |
| 277 | if ($gc = gc_enabled()) { |
| 278 | gc_disable(); |
| 279 | } |
| 280 | try { |
| 281 | return new Data($this->doClone($var)); |
| 282 | } finally { |
| 283 | if ($gc) { |
| 284 | gc_enable(); |
| 285 | } |
| 286 | restore_error_handler(); |
| 287 | $this->prevErrorHandler = null; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Effectively clones the PHP variable. |
| 293 | * |
| 294 | * @param mixed $var Any PHP variable |
| 295 | * |
| 296 | * @return array The cloned variable represented in an array |
| 297 | */ |
| 298 | abstract protected function doClone($var); |
| 299 | |
| 300 | /** |
| 301 | * Casts an object to an array representation. |
| 302 | * |
| 303 | * @param bool $isNested True if the object is nested in the dumped structure |
| 304 | * |
| 305 | * @return array The object casted as array |
| 306 | */ |
| 307 | protected function castObject(Stub $stub, bool $isNested) |
| 308 | { |
| 309 | $obj = $stub->value; |
| 310 | $class = $stub->class; |
| 311 | |
| 312 | if (\PHP_VERSION_ID < 80000 ? "\0" === ($class[15] ?? null) : str_contains($class, "@anonymous\0")) { |
| 313 | $stub->class = get_debug_type($obj); |
| 314 | } |
| 315 | if (isset($this->classInfo[$class])) { |
| 316 | [$i, $parents, $hasDebugInfo, $fileInfo] = $this->classInfo[$class]; |
| 317 | } else { |
| 318 | $i = 2; |
| 319 | $parents = [$class]; |
| 320 | $hasDebugInfo = method_exists($class, '__debugInfo'); |
| 321 | |
| 322 | foreach (class_parents($class) as $p) { |
| 323 | $parents[] = $p; |
| 324 | ++$i; |
| 325 | } |
| 326 | foreach (class_implements($class) as $p) { |
| 327 | $parents[] = $p; |
| 328 | ++$i; |
| 329 | } |
| 330 | $parents[] = '*'; |
| 331 | |
| 332 | $r = new \ReflectionClass($class); |
| 333 | $fileInfo = $r->isInternal() || $r->isSubclassOf(Stub::class) ? [] : [ |
| 334 | 'file' => $r->getFileName(), |
| 335 | 'line' => $r->getStartLine(), |
| 336 | ]; |
| 337 | |
| 338 | $this->classInfo[$class] = [$i, $parents, $hasDebugInfo, $fileInfo]; |
| 339 | } |
| 340 | |
| 341 | $stub->attr += $fileInfo; |
| 342 | $a = Caster::castObject($obj, $class, $hasDebugInfo, $stub->class); |
| 343 | |
| 344 | try { |
| 345 | while ($i--) { |
| 346 | if (!empty($this->casters[$p = $parents[$i]])) { |
| 347 | foreach ($this->casters[$p] as $callback) { |
| 348 | $a = $callback($obj, $a, $stub, $isNested, $this->filter); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } catch (\Exception $e) { |
| 353 | $a = [(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)] + $a; |
| 354 | } |
| 355 | |
| 356 | return $a; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Casts a resource to an array representation. |
| 361 | * |
| 362 | * @param bool $isNested True if the object is nested in the dumped structure |
| 363 | * |
| 364 | * @return array The resource casted as array |
| 365 | */ |
| 366 | protected function castResource(Stub $stub, bool $isNested) |
| 367 | { |
| 368 | $a = []; |
| 369 | $res = $stub->value; |
| 370 | $type = $stub->class; |
| 371 | |
| 372 | try { |
| 373 | if (!empty($this->casters[':'.$type])) { |
| 374 | foreach ($this->casters[':'.$type] as $callback) { |
| 375 | $a = $callback($res, $a, $stub, $isNested, $this->filter); |
| 376 | } |
| 377 | } |
| 378 | } catch (\Exception $e) { |
| 379 | $a = [(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)] + $a; |
| 380 | } |
| 381 | |
| 382 | return $a; |
| 383 | } |
| 384 | } |