AbstractCloner.php 14 KB

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