Няма описание

Restorer.php 4.3KB

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