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