PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/var-dumper/Cloner/VarCloner.php

https://gitlab.com/mvk12/EduManApp
PHP | 324 lines | 279 code | 22 blank | 23 comment | 65 complexity | cdef151b4e21a782d37f1ccfb83057f6 MD5 | raw file
  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 $hashMask = 0;
  17. private static $hashOffset = 0;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function doClone($var)
  22. {
  23. $useExt = $this->useExt;
  24. $i = 0; // Current iteration position in $queue
  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. $arrayRefs = array(); // Map of queue indexes to stub array objects
  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. $gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
  38. $a = null; // Array cast for nested structures
  39. $stub = null; // Stub capturing the main properties of an original item value
  40. // or null if the original value is used directly
  41. $zval = array( // Main properties of the current value
  42. 'type' => null,
  43. 'zval_isref' => null,
  44. 'zval_hash' => null,
  45. 'array_count' => null,
  46. 'object_class' => null,
  47. 'object_handle' => null,
  48. 'resource_type' => null,
  49. );
  50. if (!self::$hashMask) {
  51. self::initHashMask();
  52. }
  53. $hashMask = self::$hashMask;
  54. $hashOffset = self::$hashOffset;
  55. for ($i = 0; $i < $len; ++$i) {
  56. $indexed = true; // Whether the currently iterated array is numerically indexed or not
  57. $j = -1; // Position in the currently iterated array
  58. $fromObjCast = array_keys($queue[$i]);
  59. $fromObjCast = array_keys(array_flip($fromObjCast)) !== $fromObjCast;
  60. $refs = $vals = $fromObjCast ? array_values($queue[$i]) : $queue[$i];
  61. foreach ($queue[$i] as $k => $v) {
  62. // $k is the original key
  63. // $v is the original value or a stub object in case of hard references
  64. if ($k !== ++$j) {
  65. $indexed = false;
  66. }
  67. if ($fromObjCast) {
  68. $k = $j;
  69. }
  70. if ($useExt) {
  71. $zval = symfony_zval_info($k, $refs);
  72. } else {
  73. $refs[$k] = $cookie;
  74. if ($zval['zval_isref'] = $vals[$k] === $cookie) {
  75. $zval['zval_hash'] = $v instanceof Stub ? spl_object_hash($v) : null;
  76. }
  77. $zval['type'] = gettype($v);
  78. }
  79. if ($zval['zval_isref']) {
  80. $vals[$k] = &$stub; // Break hard references to make $queue completely
  81. unset($stub); // independent from the original structure
  82. if (isset($hardRefs[$zval['zval_hash']])) {
  83. $vals[$k] = $useExt ? ($v = $hardRefs[$zval['zval_hash']]) : ($refs[$k] = $v);
  84. if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  85. ++$v->value->refCount;
  86. }
  87. ++$v->refCount;
  88. continue;
  89. }
  90. }
  91. // Create $stub when the original value $v can not be used directly
  92. // If $v is a nested structure, put that structure in array $a
  93. switch ($zval['type']) {
  94. case 'string':
  95. if (isset($v[0]) && !preg_match('//u', $v)) {
  96. $stub = new Stub();
  97. $stub->type = Stub::TYPE_STRING;
  98. $stub->class = Stub::STRING_BINARY;
  99. if (0 <= $maxString && 0 < $cut = strlen($v) - $maxString) {
  100. $stub->cut = $cut;
  101. $stub->value = substr($v, 0, -$cut);
  102. } else {
  103. $stub->value = $v;
  104. }
  105. } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = mb_strlen($v, 'UTF-8') - $maxString) {
  106. $stub = new Stub();
  107. $stub->type = Stub::TYPE_STRING;
  108. $stub->class = Stub::STRING_UTF8;
  109. $stub->cut = $cut;
  110. $stub->value = mb_substr($v, 0, $maxString, 'UTF-8');
  111. }
  112. break;
  113. case 'integer':
  114. break;
  115. case 'array':
  116. if ($v) {
  117. $stub = $arrayRefs[$len] = new Stub();
  118. $stub->type = Stub::TYPE_ARRAY;
  119. $stub->class = Stub::ARRAY_ASSOC;
  120. // Copies of $GLOBALS have very strange behavior,
  121. // let's detect them with some black magic
  122. $a = $v;
  123. $a[$gid] = true;
  124. // Happens with copies of $GLOBALS
  125. if (isset($v[$gid])) {
  126. unset($v[$gid]);
  127. $a = array();
  128. foreach ($v as $gk => &$gv) {
  129. $a[$gk] = &$gv;
  130. }
  131. } else {
  132. $a = $v;
  133. }
  134. $stub->value = $zval['array_count'] ?: count($a);
  135. }
  136. break;
  137. case 'object':
  138. if (empty($objRefs[$h = $zval['object_handle'] ?: ($hashMask ^ hexdec(substr(spl_object_hash($v), $hashOffset, PHP_INT_SIZE)))])) {
  139. $stub = new Stub();
  140. $stub->type = Stub::TYPE_OBJECT;
  141. $stub->class = $zval['object_class'] ?: get_class($v);
  142. $stub->value = $v;
  143. $stub->handle = $h;
  144. $a = $this->castObject($stub, 0 < $i);
  145. if ($v !== $stub->value) {
  146. if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  147. break;
  148. }
  149. if ($useExt) {
  150. $zval['type'] = $stub->value;
  151. $zval = symfony_zval_info('type', $zval);
  152. $h = $zval['object_handle'];
  153. } else {
  154. $h = $hashMask ^ hexdec(substr(spl_object_hash($stub->value), $hashOffset, PHP_INT_SIZE));
  155. }
  156. $stub->handle = $h;
  157. }
  158. $stub->value = null;
  159. if (0 <= $maxItems && $maxItems <= $pos) {
  160. $stub->cut = count($a);
  161. $a = null;
  162. }
  163. }
  164. if (empty($objRefs[$h])) {
  165. $objRefs[$h] = $stub;
  166. } else {
  167. $stub = $objRefs[$h];
  168. ++$stub->refCount;
  169. $a = null;
  170. }
  171. break;
  172. case 'resource':
  173. case 'unknown type':
  174. if (empty($resRefs[$h = (int) $v])) {
  175. $stub = new Stub();
  176. $stub->type = Stub::TYPE_RESOURCE;
  177. $stub->class = $zval['resource_type'] ?: get_resource_type($v);
  178. $stub->value = $v;
  179. $stub->handle = $h;
  180. $a = $this->castResource($stub, 0 < $i);
  181. $stub->value = null;
  182. if (0 <= $maxItems && $maxItems <= $pos) {
  183. $stub->cut = count($a);
  184. $a = null;
  185. }
  186. }
  187. if (empty($resRefs[$h])) {
  188. $resRefs[$h] = $stub;
  189. } else {
  190. $stub = $resRefs[$h];
  191. ++$stub->refCount;
  192. $a = null;
  193. }
  194. break;
  195. }
  196. if (isset($stub)) {
  197. if ($zval['zval_isref']) {
  198. if ($useExt) {
  199. $vals[$k] = $hardRefs[$zval['zval_hash']] = $v = new Stub();
  200. $v->value = $stub;
  201. } else {
  202. $refs[$k] = new Stub();
  203. $refs[$k]->value = $stub;
  204. $h = spl_object_hash($refs[$k]);
  205. $vals[$k] = $hardRefs[$h] = &$refs[$k];
  206. $values[$h] = $v;
  207. }
  208. $vals[$k]->handle = ++$refsCounter;
  209. } else {
  210. $vals[$k] = $stub;
  211. }
  212. if ($a) {
  213. if ($i && 0 <= $maxItems) {
  214. $k = count($a);
  215. if ($pos < $maxItems) {
  216. if ($maxItems < $pos += $k) {
  217. $a = array_slice($a, 0, $maxItems - $pos);
  218. if ($stub->cut >= 0) {
  219. $stub->cut += $pos - $maxItems;
  220. }
  221. }
  222. } else {
  223. if ($stub->cut >= 0) {
  224. $stub->cut += $k;
  225. }
  226. $stub = $a = null;
  227. unset($arrayRefs[$len]);
  228. continue;
  229. }
  230. }
  231. $queue[$len] = $a;
  232. $stub->position = $len++;
  233. }
  234. $stub = $a = null;
  235. } elseif ($zval['zval_isref']) {
  236. if ($useExt) {
  237. $vals[$k] = $hardRefs[$zval['zval_hash']] = new Stub();
  238. $vals[$k]->value = $v;
  239. } else {
  240. $refs[$k] = $vals[$k] = new Stub();
  241. $refs[$k]->value = $v;
  242. $h = spl_object_hash($refs[$k]);
  243. $hardRefs[$h] = &$refs[$k];
  244. $values[$h] = $v;
  245. }
  246. $vals[$k]->handle = ++$refsCounter;
  247. }
  248. }
  249. if ($fromObjCast) {
  250. $refs = $vals;
  251. $vals = array();
  252. $j = -1;
  253. foreach ($queue[$i] as $k => $v) {
  254. foreach (array($k => $v) as $a => $v) {
  255. }
  256. if ($a !== $k) {
  257. $vals = (object) $vals;
  258. $vals->{$k} = $refs[++$j];
  259. $vals = (array) $vals;
  260. } else {
  261. $vals[$k] = $refs[++$j];
  262. }
  263. }
  264. }
  265. $queue[$i] = $vals;
  266. if (isset($arrayRefs[$i])) {
  267. if ($indexed) {
  268. $arrayRefs[$i]->class = Stub::ARRAY_INDEXED;
  269. }
  270. unset($arrayRefs[$i]);
  271. }
  272. }
  273. foreach ($values as $h => $v) {
  274. $hardRefs[$h] = $v;
  275. }
  276. return $queue;
  277. }
  278. private static function initHashMask()
  279. {
  280. $obj = (object) array();
  281. self::$hashOffset = 16 - PHP_INT_SIZE;
  282. self::$hashMask = -1;
  283. if (defined('HHVM_VERSION')) {
  284. self::$hashOffset += 16;
  285. } else {
  286. // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
  287. $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
  288. foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
  289. if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && in_array($frame['function'], $obFuncs)) {
  290. $frame['line'] = 0;
  291. break;
  292. }
  293. }
  294. if (!empty($frame['line'])) {
  295. ob_start();
  296. debug_zval_dump($obj);
  297. self::$hashMask = (int) substr(ob_get_clean(), 17);
  298. }
  299. }
  300. self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));
  301. }
  302. }