No Description

AbstractCloner.php 11KB

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