diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..1a6ac11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Bootstrapping # +################# +vendor +.env.ini + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db +.directory diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6e32887 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "victorwesterlund/globalsnapshot", + "description": "Capture and restore the state of all PHP superglobals.", + "type": "library", + "license": "The-Unlicense", + "authors": [ + { + "name": "Victor Westerlund", + "email": "victor.vesterlund@gmail.com" + } + ], + "minimum-stability": "dev", + "autoload": { + "psr-4": { + "victorwesterlund\\": "src/" + } + } +} \ No newline at end of file diff --git a/src/GlobalSnapshot.php b/src/GlobalSnapshot.php new file mode 100644 index 0000000..4c2a917 --- /dev/null +++ b/src/GlobalSnapshot.php @@ -0,0 +1,49 @@ +restore(); + class GlobalSnapshot { + // Declare all PHP superglobals + private array $_ENV; + private array $_GET; + private array $_POST; + private array $_FILES; + private array $_SERVER; + private array $_COOKIE; + private array $_REQUEST; + private array $_SESSION; + + // Declare additional PHP globals (for PHP 8.0+ compatability) + // These will not be captured, or altered by GlobalSnapshot + private int $argc; + private array $argv; + private ?array $__composer_autoload_files; // Native support for composer + + public function __construct() {} + + // Wipe all superglobals + private function truncate(string $global) { + global $$global; + $$global = []; + } + + // Restore state of superglobals at the time of capture() + public function restore() { + foreach ($this as $global => $values) { + global $$global; + + $this->truncate($global); + $$global = $this->{$global}; + } + } + + // Store current state of superglobals + public function capture() { + foreach (array_keys($GLOBALS) as $global) { + $this->{$global} = $GLOBALS[$global]; + } + } + } \ No newline at end of file