core/source/conf.php
2025-09-21 13:27:37 +02:00

150 lines
2.1 KiB
PHP

<?php
namespace davina\conf;
require_once(DIR_LOGIC . '/helpers/list.php');
/**
*/
class struct_auth
{
public string $kind;
public $data;
}
/**
*/
class struct_source_data_ics_feed
{
public string $url;
public bool $combined;
public int $lifetime;
}
/**
*/
class struct_source
{
public string $name;
public string $kind;
public $data;
}
/**
*/
class struct_settings
{
public string $timezone;
}
/**
*/
class struct_root
{
public struct_auth $auth;
/**
* @var array {list<struct_source>}
*/
public array $sources;
public struct_settings $settings;
}
/**
*/
class _state
{
public static ?struct_root $data = null;
}
/**
*/
function validate_source_name(
string $source_name
) : string
{
$matchs = null;
$result = preg_match('/^[0-9a-zA-Z]+$/', $source_name, $matches);
return ($result === 1);
}
/**
*/
function load(
string $path
) : void
{
$data_raw = \json_decode(
\file_get_contents(
$path
),
true
);
{
$data = new struct_root();
// auth
{
$auth = new struct_auth();
$auth->kind = (($data_raw['auth'] ?? [])['kind'] ?? 'none');
$auth->data = (($data_raw['auth'] ?? [])['data'] ?? null);
$data->auth = $auth;
}
// sources
{
$sources = [];
/**
* @todo check for name duplicates
*/
$data->sources = \davina\helpers\list_\map(
$data_raw['sources'],
function ($source_raw) {
if (! validate_source_name($source_raw['name']))
{
throw (new \Exception(\sprintf('invalid source name: "%s"', $source_raw['name'])));
}
else
{
$source = new struct_source();
$source->name = $source_raw['name'];
$source->kind = $source_raw['kind'];
$source->data = ($source_raw['data'] ?? null);
return $source;
}
}
);
}
// settings
{
$settings = new struct_settings();
$settings->timezone = (($data_raw['settings'] ?? [])['timezone'] ?? 'UTC');
$data->settings = $settings;
}
_state::$data = $data;
}
}
/**
*/
function get(
) : struct_root
{
if (_state::$data === null)
{
throw (new \Exception('not yet loaded'));
}
else
{
return _state::$data;
}
}
?>