Introducing settings file, some refactorings and stabilization
This commit is contained in:
@@ -4,23 +4,138 @@ declare(strict_types=1);
|
||||
|
||||
namespace PmConverter;
|
||||
|
||||
class Environment implements \ArrayAccess
|
||||
use ArrayAccess;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Environment implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var string Path to env file
|
||||
*/
|
||||
protected static string $filepath = '';
|
||||
|
||||
/**
|
||||
* @var Environment
|
||||
*/
|
||||
protected static Environment $instance;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $vars = [];
|
||||
protected array $ownVars = [];
|
||||
|
||||
/**
|
||||
* @param object|null $env
|
||||
* @var array
|
||||
*/
|
||||
public function __construct(protected ?object $env)
|
||||
protected array $customVars = [];
|
||||
|
||||
public static function instance(): static
|
||||
{
|
||||
if (!empty($env->values)) {
|
||||
foreach ($env->values as $var) {
|
||||
$this->vars[static::formatKey($var->key)] = $var->value;
|
||||
}
|
||||
return static::$instance ??= new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filepath
|
||||
* @return $this
|
||||
* @throws JsonException
|
||||
*/
|
||||
public function readFromFile(string $filepath): static
|
||||
{
|
||||
$content = file_get_contents(static::$filepath = $filepath);
|
||||
$content = json_decode($content, flags: JSON_THROW_ON_ERROR); //TODO try-catch
|
||||
$content || throw new JsonException("not a valid environment: $filepath");
|
||||
property_exists($content, 'environment') && $content = $content->environment;
|
||||
if (!property_exists($content, 'id') && !property_exists($content, 'name')) {
|
||||
throw new JsonException("not a valid environment: $filepath");
|
||||
}
|
||||
return $this->setOwnVars($content->values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $vars
|
||||
* @return $this
|
||||
*/
|
||||
protected function setOwnVars(array $vars): static
|
||||
{
|
||||
foreach ($vars as $key => $value) {
|
||||
is_object($value) && [$key, $value] = [$value->key, $value->value];
|
||||
$this->setOwnVar($key, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets value to some environment own variable
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return $this
|
||||
*/
|
||||
protected function setOwnVar(string $name, string $value): static
|
||||
{
|
||||
$this->ownVars[static::formatKey($name)] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $vars
|
||||
* @return $this
|
||||
*/
|
||||
public function setCustomVars(array $vars): static
|
||||
{
|
||||
foreach ($vars as $key => $value) {
|
||||
is_object($value) && [$key, $value] = [$value->key, $value->value];
|
||||
$this->setCustomVar($key, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets value to some environment own variable
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setCustomVar(string $name, string $value): static
|
||||
{
|
||||
$this->customVars[static::formatKey($name)] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value of specific variable
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function var(string $name): mixed
|
||||
{
|
||||
$format_key = static::formatKey($name);
|
||||
return $this->ownVars[$format_key] ?? $this->customVars[$format_key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of own and custom variables
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function vars(): array
|
||||
{
|
||||
return array_merge($this->ownVars, $this->customVars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of custom variables
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function customVars(): array
|
||||
{
|
||||
return $this->customVars;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,15 +145,35 @@ class Environment implements \ArrayAccess
|
||||
*/
|
||||
public function hasVars(): bool
|
||||
{
|
||||
return !empty($this->vars);
|
||||
return !empty($this->ownVars) && !empty($this->customVars);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Closed constructor
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return array_key_exists(static::formatKey($offset), $this->vars);
|
||||
return array_key_exists(static::formatKey($offset), $this->vars());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +181,7 @@ class Environment implements \ArrayAccess
|
||||
*/
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
return $this->vars[static::formatKey($offset)] ?? null;
|
||||
return $this->var($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +189,7 @@ class Environment implements \ArrayAccess
|
||||
*/
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->vars[static::formatKey($offset)] = $value;
|
||||
$this->customVars[static::formatKey($offset)] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,7 +197,7 @@ class Environment implements \ArrayAccess
|
||||
*/
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
unset($this->vars[static::formatKey($offset)]);
|
||||
unset($this->customVars[static::formatKey($offset)]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user