AbstractCloner.php 20 KB

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