misuzu/src/Config.php

98 lines
2.7 KiB
PHP

<?php
namespace Misuzu;
use PDO;
use PDOException;
final class Config {
public const TYPE_ANY = '';
public const TYPE_STR = 'string';
public const TYPE_INT = 'integer';
public const TYPE_BOOL = 'boolean';
public const TYPE_ARR = 'array';
public const DEFAULTS = [
self::TYPE_ANY => null,
self::TYPE_STR => '',
self::TYPE_INT => 0,
self::TYPE_BOOL => false,
self::TYPE_ARR => [],
];
private static $config = [];
public static function init(): void {
try {
$config = DB::prepare('SELECT * FROM `msz_config`')->fetchAll();
} catch(PDOException $ex) {
return;
}
foreach($config as $record) {
self::$config[$record['config_name']] = unserialize($record['config_value']);
}
}
public static function keys(): array {
return array_keys(self::$config);
}
public static function type($value): string {
return gettype($value);
}
public static function isValidType(string $type): bool {
return $type === self::TYPE_ARR
|| $type === self::TYPE_BOOL
|| $type === self::TYPE_INT
|| $type === self::TYPE_STR;
}
public static function validateName(string $name): bool {
// this should better validate the format, this allows for a lot of shittery
return preg_match('#^([a-z][a-zA-Z0-9._]+)$#', $name) === 1;
}
public static function default(string $type) {
return self::DEFAULTS[$type] ?? null;
}
public static function get(string $key, string $type = self::TYPE_ANY, $default = null) {
$value = self::$config[$key] ?? null;
if($type !== self::TYPE_ANY && gettype($value) !== $type)
$value = null;
return $value ?? $default ?? self::DEFAULTS[$type];
}
public static function has(string $key): bool {
return array_key_exists($key, self::$config);
}
public static function set(string $key, $value, bool $soft = false): void {
self::$config[$key] = $value;
if(!$soft) {
$value = serialize($value);
DB::prepare('
REPLACE INTO `msz_config`
(`config_name`, `config_value`)
VALUES
(:name, :value)
')->bind('name', $key)
->bind('value', $value)
->execute();
}
}
public static function remove(string $key, bool $soft = false): void {
unset(self::$config[$key]);
if(!$soft)
DB::prepare('DELETE FROM `msz_config` WHERE `config_name` = :name')->bind('name', $key)->execute();
}
}