Initial code commit

This commit is contained in:
Victor Westerlund 2024-04-18 13:23:30 +02:00
parent 3c89b96bf7
commit 086d6375c2
3 changed files with 83 additions and 0 deletions

16
.gitignore vendored Executable file
View file

@ -0,0 +1,16 @@
# Bootstrapping #
#################
vendor
.env.ini
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
.directory

18
composer.json Normal file
View file

@ -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/"
}
}
}

49
src/GlobalSnapshot.php Normal file
View file

@ -0,0 +1,49 @@
<?php
namespace victorwesterlund;
// Capture the current state of all superglobals.
// This will save a copy of all keys and values and any changes made to the superglobals
// can be restored to this point in time by calling $this->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];
}
}
}