AbstractCloner.php 16 KB

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