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