AbstractCloner.php 18 KB

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