Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iamwizzdom committed Nov 9, 2021
1 parent 8a7bdf8 commit 1ba7038
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions system/que/support/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace que\support;

use InvalidArgumentException;
use que\config\Repository;
use RuntimeException;

class Env
{
Expand All @@ -21,7 +23,7 @@ class Env
public function __construct(string $path)
{
if(!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
throw new InvalidArgumentException(sprintf('%s does not exist', $path));
}
$this->path = $path;
$this->repository = Repository::getInstance();
Expand All @@ -30,7 +32,7 @@ public function __construct(string $path)
public function load() :void
{
if (!is_readable($this->path)) {
throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
throw new RuntimeException(sprintf('%s file is not readable', $this->path));
}

$env = [];
Expand All @@ -56,11 +58,15 @@ public function load() :void
$value = $matches[2];
}

if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
if (!array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
$_ENV[$name] = $value;
}

if (!array_key_exists($name, $_SERVER)) {
$_SERVER[$name] = $value;
}

$env[$name] = $value;
}
$this->repository->set('env', $env);
Expand All @@ -85,7 +91,7 @@ public static function all(): array
*/
public static function get($key, $default = null): mixed
{
return Repository::getInstance()->get("env.{$key}", $default);
return Repository::getInstance()->get("env.$key", $default);
}

/**
Expand All @@ -97,7 +103,7 @@ public static function get($key, $default = null): mixed
*/
public static function set($key, $value): Repository
{
return Repository::getInstance()->set("env.{$key}", $value);
return Repository::getInstance()->set("env.$key", $value);
}

/**
Expand All @@ -107,6 +113,6 @@ public static function set($key, $value): Repository
*/
public static function unset($key)
{
Repository::getInstance()->remove("env.{$key}");
Repository::getInstance()->remove("env.$key");
}
}

0 comments on commit 1ba7038

Please sign in to comment.