AbstractCloner.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. ':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],
  144. ':dba persistent' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],
  145. 'GdImage' => ['Symfony\Component\VarDumper\Caster\GdCaster', 'castGd'],
  146. 'SQLite3Result' => ['Symfony\Component\VarDumper\Caster\SqliteCaster', 'castSqlite3Result'],
  147. 'PgSql\Lob' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'],
  148. 'PgSql\Connection' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
  149. 'PgSql\Result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'],
  150. ':process' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'],
  151. ':stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
  152. 'OpenSSLAsymmetricKey' => ['Symfony\Component\VarDumper\Caster\OpenSSLCaster', 'castOpensslAsymmetricKey'],
  153. 'OpenSSLCertificateSigningRequest' => ['Symfony\Component\VarDumper\Caster\OpenSSLCaster', 'castOpensslCsr'],
  154. 'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\OpenSSLCaster', 'castOpensslX509'],
  155. ':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
  156. ':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'],
  157. 'XmlParser' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
  158. 'RdKafka' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castRdKafka'],
  159. 'RdKafka\Conf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castConf'],
  160. 'RdKafka\KafkaConsumer' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castKafkaConsumer'],
  161. 'RdKafka\Metadata\Broker' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castBrokerMetadata'],
  162. 'RdKafka\Metadata\Collection' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castCollectionMetadata'],
  163. 'RdKafka\Metadata\Partition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castPartitionMetadata'],
  164. 'RdKafka\Metadata\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicMetadata'],
  165. 'RdKafka\Message' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castMessage'],
  166. 'RdKafka\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopic'],
  167. 'RdKafka\TopicPartition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicPartition'],
  168. 'RdKafka\TopicConf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicConf'],
  169. 'FFI\CData' => ['Symfony\Component\VarDumper\Caster\FFICaster', 'castCTypeOrCData'],
  170. 'FFI\CType' => ['Symfony\Component\VarDumper\Caster\FFICaster', 'castCTypeOrCData'],
  171. ];
  172. protected int $maxItems = 2500;
  173. protected int $maxString = -1;
  174. protected int $minDepth = 1;
  175. /**
  176. * @var array<string, list<callable>>
  177. */
  178. private array $casters = [];
  179. /**
  180. * @var callable|null
  181. */
  182. private $prevErrorHandler;
  183. private array $classInfo = [];
  184. private int $filter = 0;
  185. /**
  186. * @param callable[]|null $casters A map of casters
  187. *
  188. * @see addCasters
  189. */
  190. public function __construct(?array $casters = null)
  191. {
  192. $this->addCasters($casters ?? static::$defaultCasters);
  193. }
  194. /**
  195. * Adds casters for resources and objects.
  196. *
  197. * Maps resources or object types to a callback.
  198. * Use types as keys and callable casters as values.
  199. * Prefix types with `::`,
  200. * see e.g. self::$defaultCasters.
  201. *
  202. * @param array<string, callable> $casters A map of casters
  203. */
  204. public function addCasters(array $casters): void
  205. {
  206. foreach ($casters as $type => $callback) {
  207. $this->casters[$type][] = $callback;
  208. }
  209. }
  210. /**
  211. * Adds default casters for resources and objects.
  212. *
  213. * Maps resources or object types to a callback.
  214. * Use types as keys and callable casters as values.
  215. * Prefix types with `::`,
  216. * see e.g. self::$defaultCasters.
  217. *
  218. * @param array<string, callable> $casters A map of casters
  219. */
  220. public static function addDefaultCasters(array $casters): void
  221. {
  222. self::$defaultCasters = [...self::$defaultCasters, ...$casters];
  223. }
  224. /**
  225. * Sets the maximum number of items to clone past the minimum depth in nested structures.
  226. */
  227. public function setMaxItems(int $maxItems): void
  228. {
  229. $this->maxItems = $maxItems;
  230. }
  231. /**
  232. * Sets the maximum cloned length for strings.
  233. */
  234. public function setMaxString(int $maxString): void
  235. {
  236. $this->maxString = $maxString;
  237. }
  238. /**
  239. * Sets the minimum tree depth where we are guaranteed to clone all the items. After this
  240. * depth is reached, only setMaxItems items will be cloned.
  241. */
  242. public function setMinDepth(int $minDepth): void
  243. {
  244. $this->minDepth = $minDepth;
  245. }
  246. /**
  247. * Clones a PHP variable.
  248. *
  249. * @param int $filter A bit field of Caster::EXCLUDE_* constants
  250. */
  251. public function cloneVar(mixed $var, int $filter = 0): Data
  252. {
  253. $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) {
  254. if (\E_RECOVERABLE_ERROR === $type || \E_USER_ERROR === $type) {
  255. // Cloner never dies
  256. throw new \ErrorException($msg, 0, $type, $file, $line);
  257. }
  258. if ($this->prevErrorHandler) {
  259. return ($this->prevErrorHandler)($type, $msg, $file, $line, $context);
  260. }
  261. return false;
  262. });
  263. $this->filter = $filter;
  264. if ($gc = gc_enabled()) {
  265. gc_disable();
  266. }
  267. try {
  268. return new Data($this->doClone($var));
  269. } finally {
  270. if ($gc) {
  271. gc_enable();
  272. }
  273. restore_error_handler();
  274. $this->prevErrorHandler = null;
  275. }
  276. }
  277. /**
  278. * Effectively clones the PHP variable.
  279. */
  280. abstract protected function doClone(mixed $var): array;
  281. /**
  282. * Casts an object to an array representation.
  283. *
  284. * @param bool $isNested True if the object is nested in the dumped structure
  285. */
  286. protected function castObject(Stub $stub, bool $isNested): array
  287. {
  288. $obj = $stub->value;
  289. $class = $stub->class;
  290. if (str_contains($class, "@anonymous\0")) {
  291. $stub->class = get_debug_type($obj);
  292. }
  293. if (isset($this->classInfo[$class])) {
  294. [$i, $parents, $hasDebugInfo, $fileInfo] = $this->classInfo[$class];
  295. } else {
  296. $i = 2;
  297. $parents = [$class];
  298. $hasDebugInfo = method_exists($class, '__debugInfo');
  299. foreach (class_parents($class) as $p) {
  300. $parents[] = $p;
  301. ++$i;
  302. }
  303. foreach (class_implements($class) as $p) {
  304. $parents[] = $p;
  305. ++$i;
  306. }
  307. $parents[] = '*';
  308. $r = new \ReflectionClass($class);
  309. $fileInfo = $r->isInternal() || $r->isSubclassOf(Stub::class) ? [] : [
  310. 'file' => $r->getFileName(),
  311. 'line' => $r->getStartLine(),
  312. ];
  313. $this->classInfo[$class] = [$i, $parents, $hasDebugInfo, $fileInfo];
  314. }
  315. $stub->attr += $fileInfo;
  316. $a = Caster::castObject($obj, $class, $hasDebugInfo, $stub->class);
  317. try {
  318. while ($i--) {
  319. if (!empty($this->casters[$p = $parents[$i]])) {
  320. foreach ($this->casters[$p] as $callback) {
  321. $a = $callback($obj, $a, $stub, $isNested, $this->filter);
  322. }
  323. }
  324. }
  325. } catch (\Exception $e) {
  326. $a = [(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)] + $a;
  327. }
  328. return $a;
  329. }
  330. /**
  331. * Casts a resource to an array representation.
  332. *
  333. * @param bool $isNested True if the object is nested in the dumped structure
  334. */
  335. protected function castResource(Stub $stub, bool $isNested): array
  336. {
  337. $a = [];
  338. $res = $stub->value;
  339. $type = $stub->class;
  340. try {
  341. if (!empty($this->casters[':'.$type])) {
  342. foreach ($this->casters[':'.$type] as $callback) {
  343. $a = $callback($res, $a, $stub, $isNested, $this->filter);
  344. }
  345. }
  346. } catch (\Exception $e) {
  347. $a = [(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)] + $a;
  348. }
  349. return $a;
  350. }
  351. }