AbstractCloner.php 15 KB

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