Snapshot.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/global-state.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\GlobalState;
  11. use const PHP_VERSION_ID;
  12. use function array_keys;
  13. use function array_merge;
  14. use function array_reverse;
  15. use function func_get_args;
  16. use function get_declared_classes;
  17. use function get_declared_interfaces;
  18. use function get_declared_traits;
  19. use function get_defined_constants;
  20. use function get_defined_functions;
  21. use function get_included_files;
  22. use function in_array;
  23. use function ini_get_all;
  24. use function is_array;
  25. use function is_object;
  26. use function is_resource;
  27. use function is_scalar;
  28. use function serialize;
  29. use function unserialize;
  30. use ReflectionClass;
  31. use SebastianBergmann\ObjectReflector\ObjectReflector;
  32. use SebastianBergmann\RecursionContext\Context;
  33. use Throwable;
  34. /**
  35. * A snapshot of global state.
  36. */
  37. class Snapshot
  38. {
  39. /**
  40. * @var ExcludeList
  41. */
  42. private $excludeList;
  43. /**
  44. * @var array
  45. */
  46. private $globalVariables = [];
  47. /**
  48. * @var array
  49. */
  50. private $superGlobalArrays = [];
  51. /**
  52. * @var array
  53. */
  54. private $superGlobalVariables = [];
  55. /**
  56. * @var array
  57. */
  58. private $staticAttributes = [];
  59. /**
  60. * @var array
  61. */
  62. private $iniSettings = [];
  63. /**
  64. * @var array
  65. */
  66. private $includedFiles = [];
  67. /**
  68. * @var array
  69. */
  70. private $constants = [];
  71. /**
  72. * @var array
  73. */
  74. private $functions = [];
  75. /**
  76. * @var array
  77. */
  78. private $interfaces = [];
  79. /**
  80. * @var array
  81. */
  82. private $classes = [];
  83. /**
  84. * @var array
  85. */
  86. private $traits = [];
  87. /**
  88. * Creates a snapshot of the current global state.
  89. */
  90. public function __construct(ExcludeList $excludeList = null, bool $includeGlobalVariables = true, bool $includeStaticAttributes = true, bool $includeConstants = true, bool $includeFunctions = true, bool $includeClasses = true, bool $includeInterfaces = true, bool $includeTraits = true, bool $includeIniSettings = true, bool $includeIncludedFiles = true)
  91. {
  92. $this->excludeList = $excludeList ?: new ExcludeList;
  93. if ($includeConstants) {
  94. $this->snapshotConstants();
  95. }
  96. if ($includeFunctions) {
  97. $this->snapshotFunctions();
  98. }
  99. if ($includeClasses || $includeStaticAttributes) {
  100. $this->snapshotClasses();
  101. }
  102. if ($includeInterfaces) {
  103. $this->snapshotInterfaces();
  104. }
  105. if ($includeGlobalVariables) {
  106. $this->setupSuperGlobalArrays();
  107. $this->snapshotGlobals();
  108. }
  109. if ($includeStaticAttributes) {
  110. $this->snapshotStaticAttributes();
  111. }
  112. if ($includeIniSettings) {
  113. $this->iniSettings = ini_get_all(null, false);
  114. }
  115. if ($includeIncludedFiles) {
  116. $this->includedFiles = get_included_files();
  117. }
  118. if ($includeTraits) {
  119. $this->traits = get_declared_traits();
  120. }
  121. }
  122. public function excludeList(): ExcludeList
  123. {
  124. return $this->excludeList;
  125. }
  126. public function globalVariables(): array
  127. {
  128. return $this->globalVariables;
  129. }
  130. public function superGlobalVariables(): array
  131. {
  132. return $this->superGlobalVariables;
  133. }
  134. public function superGlobalArrays(): array
  135. {
  136. return $this->superGlobalArrays;
  137. }
  138. public function staticAttributes(): array
  139. {
  140. return $this->staticAttributes;
  141. }
  142. public function iniSettings(): array
  143. {
  144. return $this->iniSettings;
  145. }
  146. public function includedFiles(): array
  147. {
  148. return $this->includedFiles;
  149. }
  150. public function constants(): array
  151. {
  152. return $this->constants;
  153. }
  154. public function functions(): array
  155. {
  156. return $this->functions;
  157. }
  158. public function interfaces(): array
  159. {
  160. return $this->interfaces;
  161. }
  162. public function classes(): array
  163. {
  164. return $this->classes;
  165. }
  166. public function traits(): array
  167. {
  168. return $this->traits;
  169. }
  170. /**
  171. * Creates a snapshot user-defined constants.
  172. */
  173. private function snapshotConstants(): void
  174. {
  175. $constants = get_defined_constants(true);
  176. if (isset($constants['user'])) {
  177. $this->constants = $constants['user'];
  178. }
  179. }
  180. /**
  181. * Creates a snapshot user-defined functions.
  182. */
  183. private function snapshotFunctions(): void
  184. {
  185. $functions = get_defined_functions();
  186. $this->functions = $functions['user'];
  187. }
  188. /**
  189. * Creates a snapshot user-defined classes.
  190. */
  191. private function snapshotClasses(): void
  192. {
  193. foreach (array_reverse(get_declared_classes()) as $className) {
  194. $class = new ReflectionClass($className);
  195. if (!$class->isUserDefined()) {
  196. break;
  197. }
  198. $this->classes[] = $className;
  199. }
  200. $this->classes = array_reverse($this->classes);
  201. }
  202. /**
  203. * Creates a snapshot user-defined interfaces.
  204. */
  205. private function snapshotInterfaces(): void
  206. {
  207. foreach (array_reverse(get_declared_interfaces()) as $interfaceName) {
  208. $class = new ReflectionClass($interfaceName);
  209. if (!$class->isUserDefined()) {
  210. break;
  211. }
  212. $this->interfaces[] = $interfaceName;
  213. }
  214. $this->interfaces = array_reverse($this->interfaces);
  215. }
  216. /**
  217. * Creates a snapshot of all global and super-global variables.
  218. */
  219. private function snapshotGlobals(): void
  220. {
  221. $superGlobalArrays = $this->superGlobalArrays();
  222. foreach ($superGlobalArrays as $superGlobalArray) {
  223. $this->snapshotSuperGlobalArray($superGlobalArray);
  224. }
  225. foreach (array_keys($GLOBALS) as $key) {
  226. if ($key !== 'GLOBALS' &&
  227. !in_array($key, $superGlobalArrays, true) &&
  228. $this->canBeSerialized($GLOBALS[$key]) &&
  229. !$this->excludeList->isGlobalVariableExcluded($key)) {
  230. /* @noinspection UnserializeExploitsInspection */
  231. $this->globalVariables[$key] = unserialize(serialize($GLOBALS[$key]));
  232. }
  233. }
  234. }
  235. /**
  236. * Creates a snapshot a super-global variable array.
  237. */
  238. private function snapshotSuperGlobalArray(string $superGlobalArray): void
  239. {
  240. $this->superGlobalVariables[$superGlobalArray] = [];
  241. if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
  242. foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
  243. /* @noinspection UnserializeExploitsInspection */
  244. $this->superGlobalVariables[$superGlobalArray][$key] = unserialize(serialize($value));
  245. }
  246. }
  247. }
  248. /**
  249. * Creates a snapshot of all static attributes in user-defined classes.
  250. */
  251. private function snapshotStaticAttributes(): void
  252. {
  253. foreach ($this->classes as $className) {
  254. $class = new ReflectionClass($className);
  255. $snapshot = [];
  256. foreach ($class->getProperties() as $attribute) {
  257. if ($attribute->isStatic()) {
  258. $name = $attribute->getName();
  259. if ($this->excludeList->isStaticAttributeExcluded($className, $name)) {
  260. continue;
  261. }
  262. $attribute->setAccessible(true);
  263. if (PHP_VERSION_ID >= 70400 && !$attribute->isInitialized()) {
  264. continue;
  265. }
  266. $value = $attribute->getValue();
  267. if ($this->canBeSerialized($value)) {
  268. /* @noinspection UnserializeExploitsInspection */
  269. $snapshot[$name] = unserialize(serialize($value));
  270. }
  271. }
  272. }
  273. if (!empty($snapshot)) {
  274. $this->staticAttributes[$className] = $snapshot;
  275. }
  276. }
  277. }
  278. /**
  279. * Returns a list of all super-global variable arrays.
  280. */
  281. private function setupSuperGlobalArrays(): void
  282. {
  283. $this->superGlobalArrays = [
  284. '_ENV',
  285. '_POST',
  286. '_GET',
  287. '_COOKIE',
  288. '_SERVER',
  289. '_FILES',
  290. '_REQUEST',
  291. ];
  292. }
  293. private function canBeSerialized($variable): bool
  294. {
  295. if (is_scalar($variable) || $variable === null) {
  296. return true;
  297. }
  298. if (is_resource($variable)) {
  299. return false;
  300. }
  301. foreach ($this->enumerateObjectsAndResources($variable) as $value) {
  302. if (is_resource($value)) {
  303. return false;
  304. }
  305. if (is_object($value)) {
  306. $class = new ReflectionClass($value);
  307. if ($class->isAnonymous()) {
  308. return false;
  309. }
  310. try {
  311. @serialize($value);
  312. } catch (Throwable $t) {
  313. return false;
  314. }
  315. }
  316. }
  317. return true;
  318. }
  319. private function enumerateObjectsAndResources($variable): array
  320. {
  321. if (isset(func_get_args()[1])) {
  322. $processed = func_get_args()[1];
  323. } else {
  324. $processed = new Context;
  325. }
  326. $result = [];
  327. if ($processed->contains($variable)) {
  328. return $result;
  329. }
  330. $array = $variable;
  331. $processed->add($variable);
  332. if (is_array($variable)) {
  333. foreach ($array as $element) {
  334. if (!is_array($element) && !is_object($element) && !is_resource($element)) {
  335. continue;
  336. }
  337. if (!is_resource($element)) {
  338. /** @noinspection SlowArrayOperationsInLoopInspection */
  339. $result = array_merge(
  340. $result,
  341. $this->enumerateObjectsAndResources($element, $processed)
  342. );
  343. } else {
  344. $result[] = $element;
  345. }
  346. }
  347. } else {
  348. $result[] = $variable;
  349. foreach ((new ObjectReflector)->getAttributes($variable) as $value) {
  350. if (!is_array($value) && !is_object($value) && !is_resource($value)) {
  351. continue;
  352. }
  353. if (!is_resource($value)) {
  354. /** @noinspection SlowArrayOperationsInLoopInspection */
  355. $result = array_merge(
  356. $result,
  357. $this->enumerateObjectsAndResources($value, $processed)
  358. );
  359. } else {
  360. $result[] = $value;
  361. }
  362. }
  363. }
  364. return $result;
  365. }
  366. }