AbstractCloner.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Cloner;
  11. use Symfony\Component\VarDumper\Caster\Caster;
  12. use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
  13. /**
  14. * AbstractCloner implements a generic caster mechanism for objects and resources.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. abstract class AbstractCloner implements ClonerInterface
  19. {
  20. public static array $defaultCasters = [
  21. '__PHP_Incomplete_Class' => ['Symfony\Component\VarDumper\Caster\Caster', 'castPhpIncompleteClass'],
  22. 'AddressInfo' => ['Symfony\Component\VarDumper\Caster\AddressInfoCaster', 'castAddressInfo'],
  23. 'Socket' => ['Symfony\Component\VarDumper\Caster\SocketCaster', 'castSocket'],
  24. 'Symfony\Component\VarDumper\Caster\CutStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'],
  25. 'Symfony\Component\VarDumper\Caster\CutArrayStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castCutArray'],
  26. 'Symfony\Component\VarDumper\Caster\ConstStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'],
  27. 'Symfony\Component\VarDumper\Caster\EnumStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castEnum'],
  28. 'Symfony\Component\VarDumper\Caster\ScalarStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castScalar'],
  29. 'Fiber' => ['Symfony\Component\VarDumper\Caster\FiberCaster', 'castFiber'],
  30. 'Closure' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClosure'],
  31. 'Generator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castGenerator'],
  32. 'ReflectionType' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castType'],
  33. 'ReflectionAttribute' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castAttribute'],
  34. 'ReflectionGenerator' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReflectionGenerator'],
  35. 'ReflectionClass' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClass'],
  36. 'ReflectionClassConstant' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castClassConstant'],
  37. 'ReflectionFunctionAbstract' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castFunctionAbstract'],
  38. 'ReflectionMethod' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castMethod'],
  39. 'ReflectionParameter' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castParameter'],
  40. 'ReflectionProperty' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castProperty'],
  41. 'ReflectionReference' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castReference'],
  42. 'ReflectionExtension' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castExtension'],
  43. 'ReflectionZendExtension' => ['Symfony\Component\VarDumper\Caster\ReflectionCaster', 'castZendExtension'],
  44. 'Doctrine\Common\Persistence\ObjectManager' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  45. 'Doctrine\Common\Proxy\Proxy' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castCommonProxy'],
  46. 'Doctrine\ORM\Proxy\Proxy' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castOrmProxy'],
  47. 'Doctrine\ORM\PersistentCollection' => ['Symfony\Component\VarDumper\Caster\DoctrineCaster', 'castPersistentCollection'],
  48. 'Doctrine\Persistence\ObjectManager' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  49. 'DOMException' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castException'],
  50. 'Dom\Exception' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castException'],
  51. 'DOMStringList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  52. 'DOMNameList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  53. 'DOMImplementation' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castImplementation'],
  54. 'Dom\Implementation' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castImplementation'],
  55. 'DOMImplementationList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  56. 'DOMNode' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  57. 'Dom\Node' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  58. 'DOMNameSpaceNode' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  59. 'DOMDocument' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDocument'],
  60. 'Dom\XMLDocument' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castXMLDocument'],
  61. 'Dom\HTMLDocument' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castHTMLDocument'],
  62. 'DOMNodeList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  63. 'Dom\NodeList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  64. 'DOMNamedNodeMap' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  65. 'Dom\DTDNamedNodeMap' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  66. 'DOMXPath' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  67. 'Dom\XPath' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  68. 'Dom\HTMLCollection' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  69. 'Dom\TokenList' => ['Symfony\Component\VarDumper\Caster\DOMCaster', 'castDom'],
  70. 'XMLReader' => ['Symfony\Component\VarDumper\Caster\XmlReaderCaster', 'castXmlReader'],
  71. 'ErrorException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castErrorException'],
  72. 'Exception' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castException'],
  73. 'Error' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castError'],
  74. 'Symfony\Bridge\Monolog\Logger' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  75. 'Symfony\Component\DependencyInjection\ContainerInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  76. 'Symfony\Component\EventDispatcher\EventDispatcherInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  77. 'Symfony\Component\HttpClient\AmpHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClient'],
  78. 'Symfony\Component\HttpClient\CurlHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClient'],
  79. 'Symfony\Component\HttpClient\NativeHttpClient' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClient'],
  80. 'Symfony\Component\HttpClient\Response\AmpResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
  81. 'Symfony\Component\HttpClient\Response\AmpResponseV4' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
  82. 'Symfony\Component\HttpClient\Response\AmpResponseV5' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
  83. 'Symfony\Component\HttpClient\Response\CurlResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
  84. 'Symfony\Component\HttpClient\Response\NativeResponse' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castHttpClientResponse'],
  85. 'Symfony\Component\HttpFoundation\Request' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castRequest'],
  86. 'Symfony\Component\Uid\Ulid' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castUlid'],
  87. 'Symfony\Component\Uid\Uuid' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castUuid'],
  88. 'Symfony\Component\VarExporter\Internal\LazyObjectState' => ['Symfony\Component\VarDumper\Caster\SymfonyCaster', 'castLazyObjectState'],
  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\FlattenException' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castFlattenException'],
  94. 'Symfony\Component\ErrorHandler\Exception\SilencedErrorContext' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castSilencedErrorContext'],
  95. 'Imagine\Image\ImageInterface' => ['Symfony\Component\VarDumper\Caster\ImagineCaster', 'castImage'],
  96. 'Ramsey\Uuid\UuidInterface' => ['Symfony\Component\VarDumper\Caster\UuidCaster', 'castRamseyUuid'],
  97. 'ProxyManager\Proxy\ProxyInterface' => ['Symfony\Component\VarDumper\Caster\ProxyManagerCaster', 'castProxy'],
  98. 'PHPUnit_Framework_MockObject_MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  99. 'PHPUnit\Framework\MockObject\MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  100. 'PHPUnit\Framework\MockObject\Stub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  101. 'Prophecy\Prophecy\ProphecySubjectInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  102. 'Mockery\MockInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
  103. 'PDO' => ['Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdo'],
  104. 'PDOStatement' => ['Symfony\Component\VarDumper\Caster\PdoCaster', 'castPdoStatement'],
  105. 'AMQPConnection' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castConnection'],
  106. 'AMQPChannel' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castChannel'],
  107. 'AMQPQueue' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castQueue'],
  108. 'AMQPExchange' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castExchange'],
  109. 'AMQPEnvelope' => ['Symfony\Component\VarDumper\Caster\AmqpCaster', 'castEnvelope'],
  110. 'ArrayObject' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayObject'],
  111. 'ArrayIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castArrayIterator'],
  112. 'SplDoublyLinkedList' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castDoublyLinkedList'],
  113. 'SplFileInfo' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileInfo'],
  114. 'SplFileObject' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileObject'],
  115. 'SplHeap' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
  116. 'SplObjectStorage' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'],
  117. 'SplPriorityQueue' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
  118. 'OuterIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castOuterIterator'],
  119. 'WeakMap' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castWeakMap'],
  120. 'WeakReference' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castWeakReference'],
  121. 'Redis' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'],
  122. 'Relay\Relay' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'],
  123. 'RedisArray' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'],
  124. 'RedisCluster' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisCluster'],
  125. 'DateTimeInterface' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'],
  126. 'DateInterval' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'],
  127. 'DateTimeZone' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'],
  128. 'DatePeriod' => ['Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'],
  129. 'GMP' => ['Symfony\Component\VarDumper\Caster\GmpCaster', 'castGmp'],
  130. 'MessageFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castMessageFormatter'],
  131. 'NumberFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castNumberFormatter'],
  132. 'IntlTimeZone' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlTimeZone'],
  133. 'IntlCalendar' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlCalendar'],
  134. 'IntlDateFormatter' => ['Symfony\Component\VarDumper\Caster\IntlCaster', 'castIntlDateFormatter'],
  135. 'Memcached' => ['Symfony\Component\VarDumper\Caster\MemcachedCaster', 'castMemcached'],
  136. 'Ds\Collection' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castCollection'],
  137. 'Ds\Map' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castMap'],
  138. 'Ds\Pair' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPair'],
  139. 'Symfony\Component\VarDumper\Caster\DsPairStub' => ['Symfony\Component\VarDumper\Caster\DsCaster', 'castPairStub'],
  140. 'mysqli_driver' => ['Symfony\Component\VarDumper\Caster\MysqliCaster', 'castMysqliDriver'],
  141. 'CurlHandle' => ['Symfony\Component\VarDumper\Caster\CurlCaster', 'castCurl'],
  142. 'Dba\Connection' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],
  143. 'GdImage' => ['Symfony\Component\VarDumper\Caster\GdCaster', 'castGd'],
  144. 'SQLite3Result' => ['Symfony\Component\VarDumper\Caster\SqliteCaster', 'castSqlite3Result'],
  145. 'PgSql\Lob' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'],
  146. 'PgSql\Connection' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
  147. 'PgSql\Result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'],
  148. ':process' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'],
  149. ':stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
  150. 'OpenSSLAsymmetricKey' => ['Symfony\Component\VarDumper\Caster\OpenSSLCaster', 'castOpensslAsymmetricKey'],
  151. 'OpenSSLCertificateSigningRequest' => ['Symfony\Component\VarDumper\Caster\OpenSSLCaster', 'castOpensslCsr'],
  152. 'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\OpenSSLCaster', 'castOpensslX509'],
  153. ':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
  154. ':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'],
  155. 'XmlParser' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
  156. 'RdKafka' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castRdKafka'],
  157. 'RdKafka\Conf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castConf'],
  158. 'RdKafka\KafkaConsumer' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castKafkaConsumer'],
  159. 'RdKafka\Metadata\Broker' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castBrokerMetadata'],
  160. 'RdKafka\Metadata\Collection' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castCollectionMetadata'],
  161. 'RdKafka\Metadata\Partition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castPartitionMetadata'],
  162. 'RdKafka\Metadata\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicMetadata'],
  163. 'RdKafka\Message' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castMessage'],
  164. 'RdKafka\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopic'],
  165. 'RdKafka\TopicPartition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicPartition'],
  166. 'RdKafka\TopicConf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicConf'],
  167. 'FFI\CData' => ['Symfony\Component\VarDumper\Caster\FFICaster', 'castCTypeOrCData'],
  168. 'FFI\CType' => ['Symfony\Component\VarDumper\Caster\FFICaster', 'castCTypeOrCData'],
  169. ];
  170. protected int $maxItems = 2500;
  171. protected int $maxString = -1;
  172. protected int $minDepth = 1;
  173. /**
  174. * @var array<string, list<callable>>
  175. */
  176. private array $casters = [];
  177. /**
  178. * @var callable|null
  179. */
  180. private $prevErrorHandler;
  181. private array $classInfo = [];
  182. private int $filter = 0;
  183. /**
  184. * @param callable[]|null $casters A map of casters
  185. *
  186. * @see addCasters
  187. */
  188. public function __construct(?array $casters = null)
  189. {
  190. $this->addCasters($casters ?? static::$defaultCasters);
  191. }
  192. /**
  193. * Adds casters for resources and objects.
  194. *
  195. * Maps resources or object types to a callback.
  196. * Use types as keys and callable casters as values.
  197. * Prefix types with `::`,
  198. * see e.g. self::$defaultCasters.
  199. *
  200. * @param array<string, callable> $casters A map of casters
  201. */
  202. public function addCasters(array $casters): void
  203. {
  204. foreach ($casters as $type => $callback) {
  205. $this->casters[$type][] = $callback;
  206. }
  207. }
  208. /**
  209. * Adds default casters for resources and objects.
  210. *
  211. * Maps resources or object types to a callback.
  212. * Use types as keys and callable casters as values.
  213. * Prefix types with `::`,
  214. * see e.g. self::$defaultCasters.
  215. *
  216. * @param array<string, callable> $casters A map of casters
  217. */
  218. public static function addDefaultCasters(array $casters): void
  219. {
  220. self::$defaultCasters = [...self::$defaultCasters, ...$casters];
  221. }
  222. /**
  223. * Sets the maximum number of items to clone past the minimum depth in nested structures.
  224. */
  225. public function setMaxItems(int $maxItems): void
  226. {
  227. $this->maxItems = $maxItems;
  228. }
  229. /**
  230. * Sets the maximum cloned length for strings.
  231. */
  232. public function setMaxString(int $maxString): void
  233. {
  234. $this->maxString = $maxString;
  235. }
  236. /**
  237. * Sets the minimum tree depth where we are guaranteed to clone all the items. After this
  238. * depth is reached, only setMaxItems items will be cloned.
  239. */
  240. public function setMinDepth(int $minDepth): void
  241. {
  242. $this->minDepth = $minDepth;
  243. }
  244. /**
  245. * Clones a PHP variable.
  246. *
  247. * @param int $filter A bit field of Caster::EXCLUDE_* constants
  248. */
  249. public function cloneVar(mixed $var, int $filter = 0): Data
  250. {
  251. $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) {
  252. if (\E_RECOVERABLE_ERROR === $type || \E_USER_ERROR === $type) {
  253. // Cloner never dies
  254. throw new \ErrorException($msg, 0, $type, $file, $line);
  255. }
  256. if ($this->prevErrorHandler) {
  257. return ($this->prevErrorHandler)($type, $msg, $file, $line, $context);
  258. }
  259. return false;
  260. });
  261. $this->filter = $filter;
  262. if ($gc = gc_enabled()) {
  263. gc_disable();
  264. }
  265. try {
  266. return new Data($this->doClone($var));
  267. } finally {
  268. if ($gc) {
  269. gc_enable();
  270. }
  271. restore_error_handler();
  272. $this->prevErrorHandler = null;
  273. }
  274. }
  275. /**
  276. * Effectively clones the PHP variable.
  277. */
  278. abstract protected function doClone(mixed $var): array;
  279. /**
  280. * Casts an object to an array representation.
  281. *
  282. * @param bool $isNested True if the object is nested in the dumped structure
  283. */
  284. protected function castObject(Stub $stub, bool $isNested): array
  285. {
  286. $obj = $stub->value;
  287. $class = $stub->class;
  288. if (str_contains($class, "@anonymous\0")) {
  289. $stub->class = get_debug_type($obj);
  290. }
  291. if (isset($this->classInfo[$class])) {
  292. [$i, $parents, $hasDebugInfo, $fileInfo] = $this->classInfo[$class];
  293. } else {
  294. $i = 2;
  295. $parents = [$class];
  296. $hasDebugInfo = method_exists($class, '__debugInfo');
  297. foreach (class_parents($class) as $p) {
  298. $parents[] = $p;
  299. ++$i;
  300. }
  301. foreach (class_implements($class) as $p) {
  302. $parents[] = $p;
  303. ++$i;
  304. }
  305. $parents[] = '*';
  306. $r = new \ReflectionClass($class);
  307. $fileInfo = $r->isInternal() || $r->isSubclassOf(Stub::class) ? [] : [
  308. 'file' => $r->getFileName(),
  309. 'line' => $r->getStartLine(),
  310. ];
  311. $this->classInfo[$class] = [$i, $parents, $hasDebugInfo, $fileInfo];
  312. }
  313. $stub->attr += $fileInfo;
  314. $a = Caster::castObject($obj, $class, $hasDebugInfo, $stub->class);
  315. try {
  316. while ($i--) {
  317. if (!empty($this->casters[$p = $parents[$i]])) {
  318. foreach ($this->casters[$p] as $callback) {
  319. $a = $callback($obj, $a, $stub, $isNested, $this->filter);
  320. }
  321. }
  322. }
  323. } catch (\Exception $e) {
  324. $a = [(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)] + $a;
  325. }
  326. return $a;
  327. }
  328. /**
  329. * Casts a resource to an array representation.
  330. *
  331. * @param bool $isNested True if the object is nested in the dumped structure
  332. */
  333. protected function castResource(Stub $stub, bool $isNested): array
  334. {
  335. $a = [];
  336. $res = $stub->value;
  337. $type = $stub->class;
  338. try {
  339. if (!empty($this->casters[':'.$type])) {
  340. foreach ($this->casters[':'.$type] as $callback) {
  341. $a = $callback($res, $a, $stub, $isNested, $this->filter);
  342. }
  343. }
  344. } catch (\Exception $e) {
  345. $a = [(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)] + $a;
  346. }
  347. return $a;
  348. }
  349. }