AbstractCloner.php 22 KB

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