2023-08-13 02:26:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace PmConverter;
|
|
|
|
|
2024-07-07 15:43:15 +00:00
|
|
|
use ArrayAccess;
|
|
|
|
use JsonException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Environment implements ArrayAccess
|
2023-08-13 02:26:22 +00:00
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
/**
|
|
|
|
* @var string Path to env file
|
|
|
|
*/
|
|
|
|
protected static string $filepath = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Environment
|
|
|
|
*/
|
|
|
|
protected static Environment $instance;
|
|
|
|
|
2023-08-13 02:26:22 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2024-07-07 15:43:15 +00:00
|
|
|
protected array $ownVars = [];
|
2023-08-13 02:26:22 +00:00
|
|
|
|
|
|
|
/**
|
2024-07-07 15:43:15 +00:00
|
|
|
* @var array
|
2023-08-13 02:26:22 +00:00
|
|
|
*/
|
2024-07-07 15:43:15 +00:00
|
|
|
protected array $customVars = [];
|
|
|
|
|
|
|
|
public static function instance(): static
|
2023-08-13 02:26:22 +00:00
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
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");
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
2024-07-07 15:43:15 +00:00
|
|
|
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;
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells if there are some vars or not
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVars(): bool
|
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
return !empty($this->ownVars) && !empty($this->customVars);
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 15:43:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closed constructor
|
|
|
|
*/
|
|
|
|
protected function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-13 02:26:22 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetExists(mixed $offset): bool
|
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
return array_key_exists(static::formatKey($offset), $this->vars());
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetGet(mixed $offset): mixed
|
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
return $this->var($offset);
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetSet(mixed $offset, mixed $value): void
|
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
$this->customVars[static::formatKey($offset)] = $value;
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetUnset(mixed $offset): void
|
|
|
|
{
|
2024-07-07 15:43:15 +00:00
|
|
|
unset($this->customVars[static::formatKey($offset)]);
|
2023-09-10 07:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns correct variable {{name}}
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function formatKey(string $key): string
|
|
|
|
{
|
|
|
|
return sprintf('{{%s}}', str_replace(['{', '}'], '', $key));
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
}
|