AbstractCloner.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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' => '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\VarDumper\Exception\ThrowingCasterException' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castThrowingCasterException',
  69. 'Symfony\Component\VarDumper\Caster\TraceStub' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castTraceStub',
  70. 'Symfony\Component\VarDumper\Caster\FrameStub' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castFrameStub',
  71. 'PHPUnit_Framework_MockObject_MockObject' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  72. 'Prophecy\Prophecy\ProphecySubjectInterface' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  73. 'Mockery\MockInterface' => 'Symfony\Component\VarDumper\Caster\StubCaster::cutInternals',
  74. 'PDO' => 'Symfony\Component\VarDumper\Caster\PdoCaster::castPdo',
  75. 'PDOStatement' => 'Symfony\Component\VarDumper\Caster\PdoCaster::castPdoStatement',
  76. 'AMQPConnection' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castConnection',
  77. 'AMQPChannel' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castChannel',
  78. 'AMQPQueue' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castQueue',
  79. 'AMQPExchange' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castExchange',
  80. 'AMQPEnvelope' => 'Symfony\Component\VarDumper\Caster\AmqpCaster::castEnvelope',
  81. 'ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject',
  82. 'SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList',
  83. 'SplFileInfo' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileInfo',
  84. 'SplFileObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileObject',
  85. 'SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray',
  86. 'SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
  87. 'SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage',
  88. 'SplPriorityQueue' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
  89. 'OuterIterator' => 'Symfony\Component\VarDumper\Caster\SplCaster::castOuterIterator',
  90. 'MongoCursorInterface' => 'Symfony\Component\VarDumper\Caster\MongoCaster::castCursor',
  91. 'Redis' => 'Symfony\Component\VarDumper\Caster\RedisCaster::castRedis',
  92. 'RedisArray' => 'Symfony\Component\VarDumper\Caster\RedisCaster::castRedisArray',
  93. ':curl' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castCurl',
  94. ':dba' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
  95. ':dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
  96. ':gd' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castGd',
  97. ':mysql link' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castMysqlLink',
  98. ':pgsql large object' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLargeObject',
  99. ':pgsql link' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLink',
  100. ':pgsql link persistent' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castLink',
  101. ':pgsql result' => 'Symfony\Component\VarDumper\Caster\PgSqlCaster::castResult',
  102. ':process' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castProcess',
  103. ':stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
  104. ':persistent stream' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStream',
  105. ':stream-context' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castStreamContext',
  106. ':xml' => 'Symfony\Component\VarDumper\Caster\XmlResourceCaster::castXml',
  107. );
  108. protected $maxItems = 2500;
  109. protected $maxString = -1;
  110. protected $useExt;
  111. private $casters = array();
  112. private $prevErrorHandler;
  113. private $classInfo = array();
  114. private $filter = 0;
  115. /**
  116. * @param callable[]|null $casters A map of casters
  117. *
  118. * @see addCasters
  119. */
  120. public function __construct(array $casters = null)
  121. {
  122. if (null === $casters) {
  123. $casters = static::$defaultCasters;
  124. }
  125. $this->addCasters($casters);
  126. $this->useExt = extension_loaded('symfony_debug');
  127. }
  128. /**
  129. * Adds casters for resources and objects.
  130. *
  131. * Maps resources or objects types to a callback.
  132. * Types are in the key, with a callable caster for value.
  133. * Resource types are to be prefixed with a `:`,
  134. * see e.g. static::$defaultCasters.
  135. *
  136. * @param callable[] $casters A map of casters
  137. */
  138. public function addCasters(array $casters)
  139. {
  140. foreach ($casters as $type => $callback) {
  141. $this->casters[strtolower($type)][] = $callback;
  142. }
  143. }
  144. /**
  145. * Sets the maximum number of items to clone past the first level in nested structures.
  146. *
  147. * @param int $maxItems
  148. */
  149. public function setMaxItems($maxItems)
  150. {
  151. $this->maxItems = (int) $maxItems;
  152. }
  153. /**
  154. * Sets the maximum cloned length for strings.
  155. *
  156. * @param int $maxString
  157. */
  158. public function setMaxString($maxString)
  159. {
  160. $this->maxString = (int) $maxString;
  161. }
  162. /**
  163. * Clones a PHP variable.
  164. *
  165. * @param mixed $var Any PHP variable
  166. * @param int $filter A bit field of Caster::EXCLUDE_* constants
  167. *
  168. * @return Data The cloned variable represented by a Data object
  169. */
  170. public function cloneVar($var, $filter = 0)
  171. {
  172. $this->prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context) {
  173. if (E_RECOVERABLE_ERROR === $type || E_USER_ERROR === $type) {
  174. // Cloner never dies
  175. throw new \ErrorException($msg, 0, $type, $file, $line);
  176. }
  177. if ($this->prevErrorHandler) {
  178. return call_user_func($this->prevErrorHandler, $type, $msg, $file, $line, $context);
  179. }
  180. return false;
  181. });
  182. $this->filter = $filter;
  183. try {
  184. $data = $this->doClone($var);
  185. } catch (\Exception $e) {
  186. }
  187. restore_error_handler();
  188. $this->prevErrorHandler = null;
  189. if (isset($e)) {
  190. throw $e;
  191. }
  192. return new Data($data);
  193. }
  194. /**
  195. * Effectively clones the PHP variable.
  196. *
  197. * @param mixed $var Any PHP variable
  198. *
  199. * @return array The cloned variable represented in an array
  200. */
  201. abstract protected function doClone($var);
  202. /**
  203. * Casts an object to an array representation.
  204. *
  205. * @param Stub $stub The Stub for the casted object
  206. * @param bool $isNested True if the object is nested in the dumped structure
  207. *
  208. * @return array The object casted as array
  209. */
  210. protected function castObject(Stub $stub, $isNested)
  211. {
  212. $obj = $stub->value;
  213. $class = $stub->class;
  214. if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
  215. $stub->class = get_parent_class($class).'@anonymous';
  216. }
  217. if (isset($this->classInfo[$class])) {
  218. $classInfo = $this->classInfo[$class];
  219. } else {
  220. $classInfo = array(
  221. new \ReflectionClass($class),
  222. array_reverse(array($class => $class) + class_parents($class) + class_implements($class) + array('*' => '*')),
  223. );
  224. $classInfo[1] = array_map('strtolower', $classInfo[1]);
  225. $this->classInfo[$class] = $classInfo;
  226. }
  227. $a = Caster::castObject($obj, $classInfo[0]);
  228. foreach ($classInfo[1] as $p) {
  229. if (!empty($this->casters[$p])) {
  230. foreach ($this->casters[$p] as $p) {
  231. $a = $this->callCaster($p, $obj, $a, $stub, $isNested);
  232. }
  233. }
  234. }
  235. return $a;
  236. }
  237. /**
  238. * Casts a resource to an array representation.
  239. *
  240. * @param Stub $stub The Stub for the casted resource
  241. * @param bool $isNested True if the object is nested in the dumped structure
  242. *
  243. * @return array The resource casted as array
  244. */
  245. protected function castResource(Stub $stub, $isNested)
  246. {
  247. $a = array();
  248. $res = $stub->value;
  249. $type = $stub->class;
  250. if (!empty($this->casters[':'.$type])) {
  251. foreach ($this->casters[':'.$type] as $c) {
  252. $a = $this->callCaster($c, $res, $a, $stub, $isNested);
  253. }
  254. }
  255. return $a;
  256. }
  257. /**
  258. * Calls a custom caster.
  259. *
  260. * @param callable $callback The caster
  261. * @param object|resource $obj The object/resource being casted
  262. * @param array $a The result of the previous cast for chained casters
  263. * @param Stub $stub The Stub for the casted object/resource
  264. * @param bool $isNested True if $obj is nested in the dumped structure
  265. *
  266. * @return array The casted object/resource
  267. */
  268. private function callCaster($callback, $obj, $a, $stub, $isNested)
  269. {
  270. try {
  271. $cast = call_user_func($callback, $obj, $a, $stub, $isNested, $this->filter);
  272. if (is_array($cast)) {
  273. $a = $cast;
  274. }
  275. } catch (\Exception $e) {
  276. $a = array((Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').'⚠' => new ThrowingCasterException($e)) + $a;
  277. }
  278. return $a;
  279. }
  280. }