Restorer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the GlobalState package.
  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 ReflectionProperty;
  12. /**
  13. * Restorer of snapshots of global state.
  14. */
  15. class Restorer
  16. {
  17. /**
  18. * Deletes function definitions that are not defined in a snapshot.
  19. *
  20. * @param Snapshot $snapshot
  21. * @throws RuntimeException when the uopz_delete() function is not available
  22. * @see https://github.com/krakjoe/uopz
  23. */
  24. public function restoreFunctions(Snapshot $snapshot)
  25. {
  26. if (!function_exists('uopz_delete')) {
  27. throw new RuntimeException('The uopz_delete() function is required for this operation');
  28. }
  29. $functions = get_defined_functions();
  30. foreach (array_diff($functions['user'], $snapshot->functions()) as $function) {
  31. uopz_delete($function);
  32. }
  33. }
  34. /**
  35. * Restores all global and super-global variables from a snapshot.
  36. *
  37. * @param Snapshot $snapshot
  38. */
  39. public function restoreGlobalVariables(Snapshot $snapshot)
  40. {
  41. $superGlobalArrays = $snapshot->superGlobalArrays();
  42. foreach ($superGlobalArrays as $superGlobalArray) {
  43. $this->restoreSuperGlobalArray($snapshot, $superGlobalArray);
  44. }
  45. $globalVariables = $snapshot->globalVariables();
  46. foreach (array_keys($GLOBALS) as $key) {
  47. if ($key != 'GLOBALS' &&
  48. !in_array($key, $superGlobalArrays) &&
  49. !$snapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
  50. if (isset($globalVariables[$key])) {
  51. $GLOBALS[$key] = $globalVariables[$key];
  52. } else {
  53. unset($GLOBALS[$key]);
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * Restores all static attributes in user-defined classes from this snapshot.
  60. *
  61. * @param Snapshot $snapshot
  62. */
  63. public function restoreStaticAttributes(Snapshot $snapshot)
  64. {
  65. $current = new Snapshot($snapshot->blacklist(), false, false, false, false, true, false, false, false, false);
  66. $newClasses = array_diff($current->classes(), $snapshot->classes());
  67. unset($current);
  68. foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
  69. foreach ($staticAttributes as $name => $value) {
  70. $reflector = new ReflectionProperty($className, $name);
  71. $reflector->setAccessible(true);
  72. $reflector->setValue($value);
  73. }
  74. }
  75. foreach ($newClasses as $className) {
  76. $class = new \ReflectionClass($className);
  77. $defaults = $class->getDefaultProperties();
  78. foreach ($class->getProperties() as $attribute) {
  79. if (!$attribute->isStatic()) {
  80. continue;
  81. }
  82. $name = $attribute->getName();
  83. if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
  84. continue;
  85. }
  86. if (!isset($defaults[$name])) {
  87. continue;
  88. }
  89. $attribute->setAccessible(true);
  90. $attribute->setValue($defaults[$name]);
  91. }
  92. }
  93. }
  94. /**
  95. * Restores a super-global variable array from this snapshot.
  96. *
  97. * @param Snapshot $snapshot
  98. * @param $superGlobalArray
  99. */
  100. private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
  101. {
  102. $superGlobalVariables = $snapshot->superGlobalVariables();
  103. if (isset($GLOBALS[$superGlobalArray]) &&
  104. is_array($GLOBALS[$superGlobalArray]) &&
  105. isset($superGlobalVariables[$superGlobalArray])) {
  106. $keys = array_keys(
  107. array_merge(
  108. $GLOBALS[$superGlobalArray],
  109. $superGlobalVariables[$superGlobalArray]
  110. )
  111. );
  112. foreach ($keys as $key) {
  113. if (isset($superGlobalVariables[$superGlobalArray][$key])) {
  114. $GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
  115. } else {
  116. unset($GLOBALS[$superGlobalArray][$key]);
  117. }
  118. }
  119. }
  120. }
  121. }