AbstractCloner.php 12 KB

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