AbstractCloner.php 16 KB

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