Processed w/ phpcbf

This commit is contained in:
billz 2024-02-28 19:49:46 +01:00
parent cfa916632b
commit 0b9cbee210

View File

@ -12,15 +12,18 @@ declare(strict_types=1);
namespace RaspAP\DotEnv; namespace RaspAP\DotEnv;
class DotEnv { class DotEnv
{
protected $envFile; protected $envFile;
protected $data = []; protected $data = [];
public function __construct($envFile = '.env') { public function __construct($envFile = '.env')
{
$this->envFile = $envFile; $this->envFile = $envFile;
} }
public function load() { public function load()
{
if (file_exists($this->envFile)) { if (file_exists($this->envFile)) {
$this->data = parse_ini_file($this->envFile); $this->data = parse_ini_file($this->envFile);
foreach ($this->data as $key => $value) { foreach ($this->data as $key => $value) {
@ -34,26 +37,31 @@ class DotEnv {
} }
} }
public function set($key, $value) { public function set($key, $value)
{
$this->data[$key] = $value; $this->data[$key] = $value;
putenv("$key=$value"); putenv("$key=$value");
$this->store($key, $value); $this->store($key, $value);
} }
public function get($key) { public function get($key)
{
return getenv($key); return getenv($key);
} }
public function getAll() { public function getAll()
{
return $this->data; return $this->data;
} }
public function unset($key) { public function unset($key)
{
unset($_ENV[$key]); unset($_ENV[$key]);
return $this; return $this;
} }
private function store($key, $value) { private function store($key, $value)
{
$content = file_get_contents($this->envFile); $content = file_get_contents($this->envFile);
$content = preg_replace("/^$key=.*/m", "$key=$value", $content, 1, $count); $content = preg_replace("/^$key=.*/m", "$key=$value", $content, 1, $count);
if ($count === 0) { if ($count === 0) {