Snapshot.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  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. declare(strict_types=1);
  11. namespace SebastianBergmann\GlobalState;
  12. use ReflectionClass;
  13. use Serializable;
  14. /**
  15. * A snapshot of global state.
  16. */
  17. class Snapshot
  18. {
  19. /**
  20. * @var Blacklist
  21. */
  22. private $blacklist;
  23. /**
  24. * @var array
  25. */
  26. private $globalVariables = [];
  27. /**
  28. * @var array
  29. */
  30. private $superGlobalArrays = [];
  31. /**
  32. * @var array
  33. */
  34. private $superGlobalVariables = [];
  35. /**
  36. * @var array
  37. */
  38. private $staticAttributes = [];
  39. /**
  40. * @var array
  41. */
  42. private $iniSettings = [];
  43. /**
  44. * @var array
  45. */
  46. private $includedFiles = [];
  47. /**
  48. * @var array
  49. */
  50. private $constants = [];
  51. /**
  52. * @var array
  53. */
  54. private $functions = [];
  55. /**
  56. * @var array
  57. */
  58. private $interfaces = [];
  59. /**
  60. * @var array
  61. */
  62. private $classes = [];
  63. /**
  64. * @var array
  65. */
  66. private $traits = [];
  67. /**
  68. * Creates a snapshot of the current global state.
  69. */
  70. public function __construct(Blacklist $blacklist = 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)
  71. {
  72. if ($blacklist === null) {
  73. $blacklist = new Blacklist;
  74. }
  75. $this->blacklist = $blacklist;
  76. if ($includeConstants) {
  77. $this->snapshotConstants();
  78. }
  79. if ($includeFunctions) {
  80. $this->snapshotFunctions();
  81. }
  82. if ($includeClasses || $includeStaticAttributes) {
  83. $this->snapshotClasses();
  84. }
  85. if ($includeInterfaces) {
  86. $this->snapshotInterfaces();
  87. }
  88. if ($includeGlobalVariables) {
  89. $this->setupSuperGlobalArrays();
  90. $this->snapshotGlobals();
  91. }
  92. if ($includeStaticAttributes) {
  93. $this->snapshotStaticAttributes();
  94. }
  95. if ($includeIniSettings) {
  96. $this->iniSettings = \ini_get_all(null, false);
  97. }
  98. if ($includeIncludedFiles) {
  99. $this->includedFiles = \get_included_files();
  100. }
  101. $this->traits = \get_declared_traits();
  102. }
  103. public function blacklist(): Blacklist
  104. {
  105. return $this->blacklist;
  106. }
  107. public function globalVariables(): array
  108. {
  109. return $this->globalVariables;
  110. }
  111. public function superGlobalVariables(): array
  112. {
  113. return $this->superGlobalVariables;
  114. }
  115. public function superGlobalArrays(): array
  116. {
  117. return $this->superGlobalArrays;
  118. }
  119. public function staticAttributes(): array
  120. {
  121. return $this->staticAttributes;
  122. }
  123. public function iniSettings(): array
  124. {
  125. return $this->iniSettings;
  126. }
  127. public function includedFiles(): array
  128. {
  129. return $this->includedFiles;
  130. }
  131. public function constants(): array
  132. {
  133. return $this->constants;
  134. }
  135. public function functions(): array
  136. {
  137. return $this->functions;
  138. }
  139. public function interfaces(): array
  140. {
  141. return $this->interfaces;
  142. }
  143. public function classes(): array
  144. {
  145. return $this->classes;
  146. }
  147. public function traits(): array
  148. {
  149. return $this->traits;
  150. }
  151. /**
  152. * Creates a snapshot user-defined constants.
  153. */
  154. private function snapshotConstants()
  155. {
  156. $constants = \get_defined_constants(true);
  157. if (isset($constants['user'])) {
  158. $this->constants = $constants['user'];
  159. }
  160. }
  161. /**
  162. * Creates a snapshot user-defined functions.
  163. */
  164. private function snapshotFunctions()
  165. {
  166. $functions = \get_defined_functions();
  167. $this->functions = $functions['user'];
  168. }
  169. /**
  170. * Creates a snapshot user-defined classes.
  171. */
  172. private function snapshotClasses()
  173. {
  174. foreach (\array_reverse(\get_declared_classes()) as $className) {
  175. $class = new ReflectionClass($className);
  176. if (!$class->isUserDefined()) {
  177. break;
  178. }
  179. $this->classes[] = $className;
  180. }
  181. $this->classes = \array_reverse($this->classes);
  182. }
  183. /**
  184. * Creates a snapshot user-defined interfaces.
  185. */
  186. private function snapshotInterfaces()
  187. {
  188. foreach (\array_reverse(\get_declared_interfaces()) as $interfaceName) {
  189. $class = new ReflectionClass($interfaceName);
  190. if (!$class->isUserDefined()) {
  191. break;
  192. }
  193. $this->interfaces[] = $interfaceName;
  194. }
  195. $this->interfaces = \array_reverse($this->interfaces);
  196. }
  197. /**
  198. * Creates a snapshot of all global and super-global variables.
  199. */
  200. private function snapshotGlobals()
  201. {
  202. $superGlobalArrays = $this->superGlobalArrays();
  203. foreach ($superGlobalArrays as $superGlobalArray) {
  204. $this->snapshotSuperGlobalArray($superGlobalArray);
  205. }
  206. foreach (\array_keys($GLOBALS) as $key) {
  207. if ($key != 'GLOBALS' &&
  208. !\in_array($key, $superGlobalArrays) &&
  209. $this->canBeSerialized($GLOBALS[$key]) &&
  210. !$this->blacklist->isGlobalVariableBlacklisted($key)) {
  211. $this->globalVariables[$key] = \unserialize(\serialize($GLOBALS[$key]));
  212. }
  213. }
  214. }
  215. /**
  216. * Creates a snapshot a super-global variable array.
  217. */
  218. private function snapshotSuperGlobalArray(string $superGlobalArray)
  219. {
  220. $this->superGlobalVariables[$superGlobalArray] = [];
  221. if (isset($GLOBALS[$superGlobalArray]) && \is_array($GLOBALS[$superGlobalArray])) {
  222. foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
  223. $this->superGlobalVariables[$superGlobalArray][$key] = \unserialize(\serialize($value));
  224. }
  225. }
  226. }
  227. /**
  228. * Creates a snapshot of all static attributes in user-defined classes.
  229. */
  230. private function snapshotStaticAttributes()
  231. {
  232. foreach ($this->classes as $className) {
  233. $class = new ReflectionClass($className);
  234. $snapshot = [];
  235. foreach ($class->getProperties() as $attribute) {
  236. if ($attribute->isStatic()) {
  237. $name = $attribute->getName();
  238. if ($this->blacklist->isStaticAttributeBlacklisted($className, $name)) {
  239. continue;
  240. }
  241. $attribute->setAccessible(true);
  242. $value = $attribute->getValue();
  243. if ($this->canBeSerialized($value)) {
  244. $snapshot[$name] = \unserialize(\serialize($value));
  245. }
  246. }
  247. }
  248. if (!empty($snapshot)) {
  249. $this->staticAttributes[$className] = $snapshot;
  250. }
  251. }
  252. }
  253. /**
  254. * Returns a list of all super-global variable arrays.
  255. */
  256. private function setupSuperGlobalArrays()
  257. {
  258. $this->superGlobalArrays = [
  259. '_ENV',
  260. '_POST',
  261. '_GET',
  262. '_COOKIE',
  263. '_SERVER',
  264. '_FILES',
  265. '_REQUEST'
  266. ];
  267. if (\ini_get('register_long_arrays') == '1') {
  268. $this->superGlobalArrays = \array_merge(
  269. $this->superGlobalArrays,
  270. [
  271. 'HTTP_ENV_VARS',
  272. 'HTTP_POST_VARS',
  273. 'HTTP_GET_VARS',
  274. 'HTTP_COOKIE_VARS',
  275. 'HTTP_SERVER_VARS',
  276. 'HTTP_POST_FILES'
  277. ]
  278. );
  279. }
  280. }
  281. /**
  282. * @todo Implement this properly
  283. */
  284. private function canBeSerialized($variable): bool
  285. {
  286. if (!\is_object($variable)) {
  287. return !\is_resource($variable);
  288. }
  289. if ($variable instanceof \stdClass) {
  290. return true;
  291. }
  292. $class = new ReflectionClass($variable);
  293. do {
  294. if ($class->isInternal()) {
  295. return $variable instanceof Serializable;
  296. }
  297. } while ($class = $class->getParentClass());
  298. return true;
  299. }
  300. }