Snapshot.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 function array_keys;
  12. use function array_merge;
  13. use function array_reverse;
  14. use function assert;
  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. private ExcludeList $excludeList;
  40. private array $globalVariables = [];
  41. private array $superGlobalArrays = [];
  42. private array $superGlobalVariables = [];
  43. private array $staticProperties = [];
  44. private array $iniSettings = [];
  45. private array $includedFiles = [];
  46. private array $constants = [];
  47. private array $functions = [];
  48. private array $interfaces = [];
  49. private array $classes = [];
  50. private array $traits = [];
  51. public function __construct(?ExcludeList $excludeList = null, bool $includeGlobalVariables = true, bool $includeStaticProperties = true, bool $includeConstants = true, bool $includeFunctions = true, bool $includeClasses = true, bool $includeInterfaces = true, bool $includeTraits = true, bool $includeIniSettings = true, bool $includeIncludedFiles = true)
  52. {
  53. $this->excludeList = $excludeList ?: new ExcludeList;
  54. if ($includeConstants) {
  55. $this->snapshotConstants();
  56. }
  57. if ($includeFunctions) {
  58. $this->snapshotFunctions();
  59. }
  60. if ($includeClasses || $includeStaticProperties) {
  61. $this->snapshotClasses();
  62. }
  63. if ($includeInterfaces) {
  64. $this->snapshotInterfaces();
  65. }
  66. if ($includeGlobalVariables) {
  67. $this->setupSuperGlobalArrays();
  68. $this->snapshotGlobals();
  69. }
  70. if ($includeStaticProperties) {
  71. $this->snapshotStaticProperties();
  72. }
  73. if ($includeIniSettings) {
  74. $this->iniSettings = ini_get_all(null, false);
  75. }
  76. if ($includeIncludedFiles) {
  77. $this->includedFiles = get_included_files();
  78. }
  79. if ($includeTraits) {
  80. $this->traits = get_declared_traits();
  81. }
  82. }
  83. public function excludeList(): ExcludeList
  84. {
  85. return $this->excludeList;
  86. }
  87. public function globalVariables(): array
  88. {
  89. return $this->globalVariables;
  90. }
  91. public function superGlobalVariables(): array
  92. {
  93. return $this->superGlobalVariables;
  94. }
  95. public function superGlobalArrays(): array
  96. {
  97. return $this->superGlobalArrays;
  98. }
  99. public function staticProperties(): array
  100. {
  101. return $this->staticProperties;
  102. }
  103. public function iniSettings(): array
  104. {
  105. return $this->iniSettings;
  106. }
  107. public function includedFiles(): array
  108. {
  109. return $this->includedFiles;
  110. }
  111. public function constants(): array
  112. {
  113. return $this->constants;
  114. }
  115. public function functions(): array
  116. {
  117. return $this->functions;
  118. }
  119. public function interfaces(): array
  120. {
  121. return $this->interfaces;
  122. }
  123. public function classes(): array
  124. {
  125. return $this->classes;
  126. }
  127. public function traits(): array
  128. {
  129. return $this->traits;
  130. }
  131. private function snapshotConstants(): void
  132. {
  133. $constants = get_defined_constants(true);
  134. if (isset($constants['user'])) {
  135. $this->constants = $constants['user'];
  136. }
  137. }
  138. private function snapshotFunctions(): void
  139. {
  140. $functions = get_defined_functions();
  141. $this->functions = $functions['user'];
  142. }
  143. private function snapshotClasses(): void
  144. {
  145. foreach (array_reverse(get_declared_classes()) as $className) {
  146. $class = new ReflectionClass($className);
  147. if (!$class->isUserDefined()) {
  148. break;
  149. }
  150. $this->classes[] = $className;
  151. }
  152. $this->classes = array_reverse($this->classes);
  153. }
  154. private function snapshotInterfaces(): void
  155. {
  156. foreach (array_reverse(get_declared_interfaces()) as $interfaceName) {
  157. $class = new ReflectionClass($interfaceName);
  158. if (!$class->isUserDefined()) {
  159. break;
  160. }
  161. $this->interfaces[] = $interfaceName;
  162. }
  163. $this->interfaces = array_reverse($this->interfaces);
  164. }
  165. private function snapshotGlobals(): void
  166. {
  167. $superGlobalArrays = $this->superGlobalArrays();
  168. foreach ($superGlobalArrays as $superGlobalArray) {
  169. $this->snapshotSuperGlobalArray($superGlobalArray);
  170. }
  171. foreach (array_keys($GLOBALS) as $key) {
  172. if ($key !== 'GLOBALS' &&
  173. !in_array($key, $superGlobalArrays, true) &&
  174. $this->canBeSerialized($GLOBALS[$key]) &&
  175. !$this->excludeList->isGlobalVariableExcluded($key)) {
  176. /* @noinspection UnserializeExploitsInspection */
  177. $this->globalVariables[$key] = unserialize(serialize($GLOBALS[$key]));
  178. }
  179. }
  180. }
  181. private function snapshotSuperGlobalArray(string $superGlobalArray): void
  182. {
  183. $this->superGlobalVariables[$superGlobalArray] = [];
  184. if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
  185. foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
  186. /* @noinspection UnserializeExploitsInspection */
  187. $this->superGlobalVariables[$superGlobalArray][$key] = unserialize(serialize($value));
  188. }
  189. }
  190. }
  191. private function snapshotStaticProperties(): void
  192. {
  193. foreach ($this->classes as $className) {
  194. $class = new ReflectionClass($className);
  195. $snapshot = [];
  196. foreach ($class->getProperties() as $property) {
  197. if ($property->isStatic()) {
  198. $name = $property->getName();
  199. if ($this->excludeList->isStaticPropertyExcluded($className, $name)) {
  200. continue;
  201. }
  202. if (!$property->isInitialized()) {
  203. continue;
  204. }
  205. $value = $property->getValue();
  206. if ($this->canBeSerialized($value)) {
  207. /* @noinspection UnserializeExploitsInspection */
  208. $snapshot[$name] = unserialize(serialize($value));
  209. }
  210. }
  211. }
  212. if (!empty($snapshot)) {
  213. $this->staticProperties[$className] = $snapshot;
  214. }
  215. }
  216. }
  217. private function setupSuperGlobalArrays(): void
  218. {
  219. $this->superGlobalArrays = [
  220. '_ENV',
  221. '_POST',
  222. '_GET',
  223. '_COOKIE',
  224. '_SERVER',
  225. '_FILES',
  226. '_REQUEST',
  227. ];
  228. }
  229. private function canBeSerialized(mixed $variable): bool
  230. {
  231. if (is_scalar($variable) || $variable === null) {
  232. return true;
  233. }
  234. if (is_resource($variable)) {
  235. return false;
  236. }
  237. foreach ($this->enumerateObjectsAndResources($variable) as $value) {
  238. if (is_resource($value)) {
  239. return false;
  240. }
  241. if (is_object($value)) {
  242. $class = new ReflectionClass($value);
  243. if ($class->isAnonymous()) {
  244. return false;
  245. }
  246. try {
  247. @serialize($value);
  248. } catch (Throwable $t) {
  249. return false;
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. private function enumerateObjectsAndResources(mixed $variable): array
  256. {
  257. if (isset(func_get_args()[1])) {
  258. $processed = func_get_args()[1];
  259. } else {
  260. $processed = new Context;
  261. }
  262. assert($processed instanceof Context);
  263. $result = [];
  264. if ($processed->contains($variable)) {
  265. return $result;
  266. }
  267. $array = $variable;
  268. /* @noinspection UnusedFunctionResultInspection */
  269. $processed->add($variable);
  270. if (is_array($variable)) {
  271. foreach ($array as $element) {
  272. if (!is_array($element) && !is_object($element) && !is_resource($element)) {
  273. continue;
  274. }
  275. if (!is_resource($element)) {
  276. /** @noinspection SlowArrayOperationsInLoopInspection */
  277. $result = array_merge(
  278. $result,
  279. $this->enumerateObjectsAndResources($element, $processed),
  280. );
  281. } else {
  282. $result[] = $element;
  283. }
  284. }
  285. } else {
  286. $result[] = $variable;
  287. foreach ((new ObjectReflector)->getProperties($variable) as $value) {
  288. if (!is_array($value) && !is_object($value) && !is_resource($value)) {
  289. continue;
  290. }
  291. if (!is_resource($value)) {
  292. /** @noinspection SlowArrayOperationsInLoopInspection */
  293. $result = array_merge(
  294. $result,
  295. $this->enumerateObjectsAndResources($value, $processed),
  296. );
  297. } else {
  298. $result[] = $value;
  299. }
  300. }
  301. }
  302. return $result;
  303. }
  304. }