AbstractCloner.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. 'MongoCursorInterface' => array('Symfony\Component\VarDumper\Caster\MongoCaster', 'castCursor'),
  94. 'Redis' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'),
  95. 'RedisArray' => array('Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedisArray'),
  96. 'DateTimeInterface' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castDateTime'),
  97. 'DateInterval' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castInterval'),
  98. 'DateTimeZone' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castTimeZone'),
  99. 'DatePeriod' => array('Symfony\Component\VarDumper\Caster\DateCaster', 'castPeriod'),
  100. ':curl' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'),
  101. ':dba' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
  102. ':dba persistent' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'),
  103. ':gd' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'),
  104. ':mysql link' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castMysqlLink'),
  105. ':pgsql large object' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'),
  106. ':pgsql link' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'),
  107. ':pgsql link persistent' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'),
  108. ':pgsql result' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'),
  109. ':process' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'),
  110. ':stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
  111. ':persistent stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'),
  112. ':stream-context' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'),
  113. ':xml' => array('Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'),
  114. );
  115. protected $maxItems = 2500;
  116. protected $maxString = -1;
  117. protected $minDepth = 1;
  118. protected $useExt;
  119. private $casters = array();
  120. private $prevErrorHandler;
  121. private $classInfo = array();
  122. private $filter = 0;
  123. /**
  124. * @param callable[]|null $casters A map of casters
  125. *
  126. * @see addCasters
  127. */
  128. public function __construct(array $casters = null)
  129. {
  130. if (null === $casters) {
  131. $casters = static::$defaultCasters;
  132. }
  133. $this->addCasters($casters);
  134. $this->useExt = \extension_loaded('symfony_debug');
  135. }
  136. /**
  137. * Adds casters for resources and objects.
  138. *
  139. * Maps resources or objects types to a callback.
  140. * Types are in the key, with a callable caster for value.
  141. * Resource types are to be prefixed with a `:`,
  142. * see e.g. static::$defaultCasters.
  143. *
  144. * @param callable[] $casters A map of casters
  145. */
  146. public function addCasters(array $casters)
  147. {
  148. foreach ($casters as $type => $callback) {
  149. $this->casters[strtolower($type)][] = \is_string($callback) && false !== strpos($callback, '::') ? explode('::', $callback, 2) : $callback;
  150. }
  151. }
  152. /**
  153. * Sets the maximum number of items to clone past the minimum depth in nested structures.
  154. *
  155. * @param int $maxItems
  156. */
  157. public function setMaxItems($maxItems)
  158. {
  159. $this->maxItems = (int) $maxItems;
  160. }
  161. /**
  162. * Sets the maximum cloned length for strings.
  163. *
  164. * @param int $maxString
  165. */
  166. public function setMaxString($maxString)
  167. {
  168. $this->maxString = (int) $maxString;
  169. }
  170. /**
  171. * Sets the minimum tree depth where we are guaranteed to clone all the items. After this
  172. * depth is reached, only setMaxItems items will be cloned.
  173. *
  174. * @param int $minDepth
  175. */
  176. public function setMinDepth($minDepth)
  177. {
  178. $this->minDepth = (int) $minDepth;
  179. }
  180. /**
  181. * Clones a PHP variable.
  182. *
  183. * @param mixed $var Any PHP variable
  184. * @param int $filter A bit field of Caster::EXCLUDE_* constants
  185. *
  186. * @return Data The cloned variable represented by a Data object
  187. */
  188. public function cloneVar($var, $filter = 0)
  189. {
  190. $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) {
  191. if (E_RECOVERABLE_ERROR === $type || E_USER_ERROR === $type) {
  192. // Cloner never dies
  193. throw new \ErrorException($msg, 0, $type, $file, $line);
  194. }
  195. if ($this->prevErrorHandler) {
  196. return \call_user_func($this->prevErrorHandler, $type, $msg, $file, $line, $context);
  197. }
  198. return false;
  199. });
  200. $this->filter = $filter;
  201. if ($gc = gc_enabled()) {
  202. gc_disable();
  203. }
  204. try {
  205. return new Data($this->doClone($var));
  206. } finally {
  207. if ($gc) {
  208. gc_enable();
  209. }
  210. restore_error_handler();
  211. $this->prevErrorHandler = null;
  212. }
  213. }
  214. /**
  215. * Effectively clones the PHP variable.
  216. *
  217. * @param mixed $var Any PHP variable
  218. *
  219. * @return array The cloned variable represented in an array
  220. */
  221. abstract protected function doClone($var);
  222. /**
  223. * Casts an object to an array representation.
  224. *
  225. * @param Stub $stub The Stub for the casted object
  226. * @param bool $isNested True if the object is nested in the dumped structure
  227. *
  228. * @return array The object casted as array
  229. */
  230. protected function castObject(Stub $stub, $isNested)
  231. {
  232. $obj = $stub->value;
  233. $class = $stub->class;
  234. if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
  235. $stub->class = get_parent_class($class).'@anonymous';
  236. }
  237. if (isset($this->classInfo[$class])) {
  238. list($i, $parents, $hasDebugInfo) = $this->classInfo[$class];
  239. } else {
  240. $i = 2;
  241. $parents = array(strtolower($class));
  242. $hasDebugInfo = method_exists($class, '__debugInfo');
  243. foreach (class_parents($class) as $p) {
  244. $parents[] = strtolower($p);
  245. ++$i;
  246. }
  247. foreach (class_implements($class) as $p) {
  248. $parents[] = strtolower($p);
  249. ++$i;
  250. }
  251. $parents[] = '*';
  252. $this->classInfo[$class] = array($i, $parents, $hasDebugInfo);
  253. }
  254. $a = Caster::castObject($obj, $class, $hasDebugInfo);
  255. try {
  256. while ($i--) {
  257. if (!empty($this->casters[$p = $parents[$i]])) {
  258. foreach ($this->casters[$p] as $callback) {
  259. $a = $callback($obj, $a, $stub, $isNested, $this->filter);
  260. }
  261. }
  262. }
  263. } catch (\Exception $e) {
  264. $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;
  265. }
  266. return $a;
  267. }
  268. /**
  269. * Casts a resource to an array representation.
  270. *
  271. * @param Stub $stub The Stub for the casted resource
  272. * @param bool $isNested True if the object is nested in the dumped structure
  273. *
  274. * @return array The resource casted as array
  275. */
  276. protected function castResource(Stub $stub, $isNested)
  277. {
  278. $a = array();
  279. $res = $stub->value;
  280. $type = $stub->class;
  281. try {
  282. if (!empty($this->casters[':'.$type])) {
  283. foreach ($this->casters[':'.$type] as $callback) {
  284. $a = $callback($res, $a, $stub, $isNested, $this->filter);
  285. }
  286. }
  287. } catch (\Exception $e) {
  288. $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;
  289. }
  290. return $a;
  291. }
  292. }