菜谱项目

VarCloner.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. */
  14. class VarCloner extends AbstractCloner
  15. {
  16. private static $gid;
  17. private static $hashMask = 0;
  18. private static $hashOffset = 0;
  19. private static $arrayCache = array();
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function doClone($var)
  24. {
  25. $len = 1; // Length of $queue
  26. $pos = 0; // Number of cloned items past the first level
  27. $refsCounter = 0; // Hard references counter
  28. $queue = array(array($var)); // This breadth-first queue is the return value
  29. $indexedArrays = array(); // Map of queue indexes that hold numerically indexed arrays
  30. $hardRefs = array(); // Map of original zval hashes to stub objects
  31. $objRefs = array(); // Map of original object handles to their stub object couterpart
  32. $resRefs = array(); // Map of original resource handles to their stub object couterpart
  33. $values = array(); // Map of stub objects' hashes to original values
  34. $maxItems = $this->maxItems;
  35. $maxString = $this->maxString;
  36. $cookie = (object) array(); // Unique object used to detect hard references
  37. $a = null; // Array cast for nested structures
  38. $stub = null; // Stub capturing the main properties of an original item value
  39. // or null if the original value is used directly
  40. if (!self::$hashMask) {
  41. self::$gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
  42. self::initHashMask();
  43. }
  44. $gid = self::$gid;
  45. $hashMask = self::$hashMask;
  46. $hashOffset = self::$hashOffset;
  47. $arrayStub = new Stub();
  48. $arrayStub->type = Stub::TYPE_ARRAY;
  49. $fromObjCast = false;
  50. for ($i = 0; $i < $len; ++$i) {
  51. $refs = $vals = $queue[$i];
  52. if (\PHP_VERSION_ID < 70200 && empty($indexedArrays[$i])) {
  53. // see https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
  54. foreach ($vals as $k => $v) {
  55. if (\is_int($k)) {
  56. continue;
  57. }
  58. foreach (array($k => true) as $gk => $gv) {
  59. }
  60. if ($gk !== $k) {
  61. $fromObjCast = true;
  62. $refs = $vals = \array_values($queue[$i]);
  63. break;
  64. }
  65. }
  66. }
  67. foreach ($vals as $k => $v) {
  68. // $v is the original value or a stub object in case of hard references
  69. $refs[$k] = $cookie;
  70. if ($zvalIsRef = $vals[$k] === $cookie) {
  71. $vals[$k] = &$stub; // Break hard references to make $queue completely
  72. unset($stub); // independent from the original structure
  73. if ($v instanceof Stub && isset($hardRefs[\spl_object_hash($v)])) {
  74. $vals[$k] = $refs[$k] = $v;
  75. if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  76. ++$v->value->refCount;
  77. }
  78. ++$v->refCount;
  79. continue;
  80. }
  81. $refs[$k] = $vals[$k] = new Stub();
  82. $refs[$k]->value = $v;
  83. $h = \spl_object_hash($refs[$k]);
  84. $hardRefs[$h] = &$refs[$k];
  85. $values[$h] = $v;
  86. $vals[$k]->handle = ++$refsCounter;
  87. }
  88. // Create $stub when the original value $v can not be used directly
  89. // If $v is a nested structure, put that structure in array $a
  90. switch (true) {
  91. case empty($v):
  92. case true === $v:
  93. case \is_int($v):
  94. case \is_float($v):
  95. continue 2;
  96. case \is_string($v):
  97. if (!\preg_match('//u', $v)) {
  98. $stub = new Stub();
  99. $stub->type = Stub::TYPE_STRING;
  100. $stub->class = Stub::STRING_BINARY;
  101. if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
  102. $stub->cut = $cut;
  103. $stub->value = \substr($v, 0, -$cut);
  104. } else {
  105. $stub->value = $v;
  106. }
  107. } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = \mb_strlen($v, 'UTF-8') - $maxString) {
  108. $stub = new Stub();
  109. $stub->type = Stub::TYPE_STRING;
  110. $stub->class = Stub::STRING_UTF8;
  111. $stub->cut = $cut;
  112. $stub->value = \mb_substr($v, 0, $maxString, 'UTF-8');
  113. } else {
  114. continue 2;
  115. }
  116. $a = null;
  117. break;
  118. case \is_array($v):
  119. $stub = $arrayStub;
  120. $stub->class = Stub::ARRAY_INDEXED;
  121. $j = -1;
  122. foreach ($v as $gk => $gv) {
  123. if ($gk !== ++$j) {
  124. $stub->class = Stub::ARRAY_ASSOC;
  125. break;
  126. }
  127. }
  128. $a = $v;
  129. if (Stub::ARRAY_ASSOC === $stub->class) {
  130. // Copies of $GLOBALS have very strange behavior,
  131. // let's detect them with some black magic
  132. $a[$gid] = true;
  133. // Happens with copies of $GLOBALS
  134. if (isset($v[$gid])) {
  135. unset($v[$gid]);
  136. $a = array();
  137. foreach ($v as $gk => &$gv) {
  138. $a[$gk] = &$gv;
  139. }
  140. unset($gv);
  141. } else {
  142. $a = $v;
  143. }
  144. } elseif (\PHP_VERSION_ID < 70200) {
  145. $indexedArrays[$len] = true;
  146. }
  147. break;
  148. case \is_object($v):
  149. case $v instanceof \__PHP_Incomplete_Class:
  150. if (empty($objRefs[$h = $hashMask ^ \hexdec(\substr(\spl_object_hash($v), $hashOffset, \PHP_INT_SIZE))])) {
  151. $stub = new Stub();
  152. $stub->type = Stub::TYPE_OBJECT;
  153. $stub->class = \get_class($v);
  154. $stub->value = $v;
  155. $stub->handle = $h;
  156. $a = $this->castObject($stub, 0 < $i);
  157. if ($v !== $stub->value) {
  158. if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  159. break;
  160. }
  161. $h = $hashMask ^ \hexdec(\substr(\spl_object_hash($stub->value), $hashOffset, \PHP_INT_SIZE));
  162. $stub->handle = $h;
  163. }
  164. $stub->value = null;
  165. if (0 <= $maxItems && $maxItems <= $pos) {
  166. $stub->cut = \count($a);
  167. $a = null;
  168. }
  169. }
  170. if (empty($objRefs[$h])) {
  171. $objRefs[$h] = $stub;
  172. } else {
  173. $stub = $objRefs[$h];
  174. ++$stub->refCount;
  175. $a = null;
  176. }
  177. break;
  178. default: // resource
  179. if (empty($resRefs[$h = (int) $v])) {
  180. $stub = new Stub();
  181. $stub->type = Stub::TYPE_RESOURCE;
  182. if ('Unknown' === $stub->class = @\get_resource_type($v)) {
  183. $stub->class = 'Closed';
  184. }
  185. $stub->value = $v;
  186. $stub->handle = $h;
  187. $a = $this->castResource($stub, 0 < $i);
  188. $stub->value = null;
  189. if (0 <= $maxItems && $maxItems <= $pos) {
  190. $stub->cut = \count($a);
  191. $a = null;
  192. }
  193. }
  194. if (empty($resRefs[$h])) {
  195. $resRefs[$h] = $stub;
  196. } else {
  197. $stub = $resRefs[$h];
  198. ++$stub->refCount;
  199. $a = null;
  200. }
  201. break;
  202. }
  203. if ($a) {
  204. if (!$i || 0 > $maxItems) {
  205. $queue[$len] = $a;
  206. $stub->position = $len++;
  207. } elseif ($pos < $maxItems) {
  208. if ($maxItems < $pos += \count($a)) {
  209. $a = \array_slice($a, 0, $maxItems - $pos);
  210. if ($stub->cut >= 0) {
  211. $stub->cut += $pos - $maxItems;
  212. }
  213. }
  214. $queue[$len] = $a;
  215. $stub->position = $len++;
  216. } elseif ($stub->cut >= 0) {
  217. $stub->cut += \count($a);
  218. $stub->position = 0;
  219. }
  220. }
  221. if ($arrayStub === $stub) {
  222. if ($arrayStub->cut) {
  223. $stub = array($arrayStub->cut, $arrayStub->class => $arrayStub->position);
  224. $arrayStub->cut = 0;
  225. } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  226. $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
  227. } else {
  228. self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = array($arrayStub->class => $arrayStub->position);
  229. }
  230. }
  231. if ($zvalIsRef) {
  232. $refs[$k]->value = $stub;
  233. } else {
  234. $vals[$k] = $stub;
  235. }
  236. }
  237. if ($fromObjCast) {
  238. $fromObjCast = false;
  239. $refs = $vals;
  240. $vals = array();
  241. $j = -1;
  242. foreach ($queue[$i] as $k => $v) {
  243. foreach (array($k => true) as $gk => $gv) {
  244. }
  245. if ($gk !== $k) {
  246. $vals = (object) $vals;
  247. $vals->{$k} = $refs[++$j];
  248. $vals = (array) $vals;
  249. } else {
  250. $vals[$k] = $refs[++$j];
  251. }
  252. }
  253. }
  254. $queue[$i] = $vals;
  255. }
  256. foreach ($values as $h => $v) {
  257. $hardRefs[$h] = $v;
  258. }
  259. return $queue;
  260. }
  261. private static function initHashMask()
  262. {
  263. $obj = (object) array();
  264. self::$hashOffset = 16 - PHP_INT_SIZE;
  265. self::$hashMask = -1;
  266. if (defined('HHVM_VERSION')) {
  267. self::$hashOffset += 16;
  268. } else {
  269. // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
  270. $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
  271. foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
  272. if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && in_array($frame['function'], $obFuncs)) {
  273. $frame['line'] = 0;
  274. break;
  275. }
  276. }
  277. if (!empty($frame['line'])) {
  278. ob_start();
  279. debug_zval_dump($obj);
  280. self::$hashMask = (int) substr(ob_get_clean(), 17);
  281. }
  282. }
  283. self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));
  284. }
  285. }