From b835243e57b0d4679dfee49ec907f3afdaf6da83 Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 28 Feb 2024 19:24:49 +0100 Subject: [PATCH] Add .env to gitignore --- src/RaspAP/DotEnv/DotEnv.php | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/RaspAP/DotEnv/DotEnv.php diff --git a/src/RaspAP/DotEnv/DotEnv.php b/src/RaspAP/DotEnv/DotEnv.php new file mode 100644 index 00000000..edf3d906 --- /dev/null +++ b/src/RaspAP/DotEnv/DotEnv.php @@ -0,0 +1,66 @@ + + * @license https://github.com/raspap/raspap-webgui/blob/master/LICENSE + */ + +declare(strict_types=1); + +namespace RaspAP\DotEnv; + +class DotEnv { + protected $envFile; + protected $data = []; + + public function __construct($envFile = '.env') { + $this->envFile = $envFile; + } + + public function load() { + if (file_exists($this->envFile)) { + $this->data = parse_ini_file($this->envFile); + foreach ($this->data as $key => $value) { + if (!getenv($key)) { + putenv("$key=$value"); + $_ENV[$key] = $value; + } + } + } else { + throw new Exception(".env file '{$this->envFile}' not found."); + } + } + + public function set($key, $value) { + $this->data[$key] = $value; + putenv("$key=$value"); + $this->store($key, $value); + } + + public function get($key) { + return getenv($key); + } + + public function getAll() { + return $this->data; + } + + public function unset($key) { + unset($_ENV[$key]); + return $this; + } + + private function store($key, $value) { + $content = file_get_contents($this->envFile); + $content = preg_replace("/^$key=.*/m", "$key=$value", $content, 1, $count); + if ($count === 0) { + // if key doesn't exist, append it + $content .= "$key=$value\n"; + } + file_put_contents($this->envFile, $content); + } +} +