This commit is contained in:
88
src/Config/Config.php
Normal file
88
src/Config/Config.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Mclogs\Config;
|
||||
|
||||
use Aternos\Mclogs\Util\Singleton;
|
||||
use Aternos\Mclogs\Util\URL;
|
||||
|
||||
class Config
|
||||
{
|
||||
use Singleton;
|
||||
|
||||
protected array $jsonData = [];
|
||||
|
||||
protected function __construct()
|
||||
{
|
||||
$configPath = __DIR__ . "/../../config.json";
|
||||
if (file_exists($configPath)) {
|
||||
$jsonContent = file_get_contents($configPath);
|
||||
$data = @json_decode($jsonContent, true);
|
||||
if (is_array($data)) {
|
||||
$this->jsonData = $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config value by checking environment variable, then config file, then default value
|
||||
*
|
||||
* @param ConfigKey $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(ConfigKey $key): mixed
|
||||
{
|
||||
$env = getenv($key->getEnvironmentVariable());
|
||||
if ($env !== false) {
|
||||
if ($env === "true") {
|
||||
return true;
|
||||
} else if ($env === "false") {
|
||||
return false;
|
||||
}
|
||||
return $env;
|
||||
}
|
||||
|
||||
$json = $this->getJsonValue($key->getJSONPath());
|
||||
if ($json !== null) {
|
||||
return $json;
|
||||
}
|
||||
|
||||
return $key->getDefaultValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->get(ConfigKey::FRONTEND_NAME) ?? URL::getBase()->getHost();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively get a value from the json data by path
|
||||
*
|
||||
* @param array $path
|
||||
* @param array|null $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getJsonValue(array $path, ?array $data = null): mixed
|
||||
{
|
||||
if ($data === null) {
|
||||
$data = $this->jsonData;
|
||||
}
|
||||
|
||||
$nextKey = array_shift($path);
|
||||
|
||||
if (!isset($data[$nextKey])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$nextData = $data[$nextKey];
|
||||
if (count($path) === 0) {
|
||||
return $nextData;
|
||||
}
|
||||
if (!is_array($nextData)) {
|
||||
return null;
|
||||
}
|
||||
return $this->getJsonValue($path, $nextData);
|
||||
}
|
||||
}
|
||||
80
src/Config/ConfigKey.php
Normal file
80
src/Config/ConfigKey.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Mclogs\Config;
|
||||
|
||||
enum ConfigKey
|
||||
{
|
||||
case STORAGE_TTL;
|
||||
case STORAGE_LIMIT_BYTES;
|
||||
case STORAGE_LIMIT_LINES;
|
||||
|
||||
case MONGODB_URL;
|
||||
case MONGODB_DATABASE;
|
||||
|
||||
case ID_LENGTH;
|
||||
|
||||
case LEGAL_ABUSE;
|
||||
case LEGAL_IMPRINT;
|
||||
case LEGAL_PRIVACY;
|
||||
|
||||
case FRONTEND_NAME;
|
||||
case FRONTEND_ANALYTICS;
|
||||
case FRONTEND_ASSETS_INTEGRITY;
|
||||
case FRONTEND_COLOR_BACKGROUND;
|
||||
case FRONTEND_COLOR_TEXT;
|
||||
case FRONTEND_COLOR_ACCENT;
|
||||
case FRONTEND_COLOR_ERROR;
|
||||
|
||||
case WORKER_REQUESTS;
|
||||
|
||||
/**
|
||||
* Get the default value for the config key
|
||||
*
|
||||
* @return string|int|null
|
||||
*/
|
||||
public function getDefaultValue(): string|int|null
|
||||
{
|
||||
return match ($this) {
|
||||
ConfigKey::STORAGE_TTL => 90 * 24 * 60 * 60,
|
||||
ConfigKey::STORAGE_LIMIT_BYTES => 10 * 1024 * 1024,
|
||||
ConfigKey::STORAGE_LIMIT_LINES => 25000,
|
||||
|
||||
ConfigKey::MONGODB_URL => 'mongodb://mongo:27017',
|
||||
ConfigKey::MONGODB_DATABASE => 'mclogs',
|
||||
|
||||
ConfigKey::ID_LENGTH => 7,
|
||||
|
||||
ConfigKey::FRONTEND_ANALYTICS => false,
|
||||
|
||||
ConfigKey::FRONTEND_ASSETS_INTEGRITY => true,
|
||||
|
||||
ConfigKey::FRONTEND_COLOR_BACKGROUND => "#1a1a1a",
|
||||
ConfigKey::FRONTEND_COLOR_TEXT => "#e8e8e8",
|
||||
ConfigKey::FRONTEND_COLOR_ACCENT => "#5cb85c",
|
||||
ConfigKey::FRONTEND_COLOR_ERROR => "#f62451",
|
||||
|
||||
ConfigKey::WORKER_REQUESTS => 500,
|
||||
|
||||
default => null
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variable name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironmentVariable(): string
|
||||
{
|
||||
return "MCLOGS_" . $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getJSONPath(): array
|
||||
{
|
||||
$parts = explode("_", $this->name);
|
||||
return array_map(fn($part) => strtolower($part), $parts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user