core/source/conf.php

150 lines
2.1 KiB
PHP
Raw Normal View History

2025-09-09 12:07:53 +02:00
<?php
2025-09-16 12:05:35 +02:00
namespace davina\conf;
2025-09-09 12:07:53 +02:00
2025-09-21 13:27:37 +02:00
require_once(DIR_LOGIC . '/helpers/list.php');
2025-09-09 12:07:53 +02:00
2025-09-09 19:43:23 +02:00
/**
*/
class struct_auth
{
public string $kind;
public $data;
}
2025-09-09 23:17:19 +02:00
/**
*/
class struct_source_data_ics_feed
{
public string $url;
public bool $combined;
public int $lifetime;
}
2025-09-09 19:43:23 +02:00
/**
*/
class struct_source
{
2025-09-21 13:27:37 +02:00
public string $name;
2025-09-09 19:43:23 +02:00
public string $kind;
public $data;
}
/**
*/
class struct_settings
{
public string $timezone;
}
/**
*/
class struct_root
{
public struct_auth $auth;
2025-09-21 13:27:37 +02:00
/**
* @var array {list<struct_source>}
*/
public array $sources;
2025-09-09 19:43:23 +02:00
public struct_settings $settings;
}
2025-09-09 12:07:53 +02:00
/**
*/
class _state
{
2025-09-09 19:43:23 +02:00
public static ?struct_root $data = null;
2025-09-09 12:07:53 +02:00
}
2025-09-21 13:27:37 +02:00
/**
*/
function validate_source_name(
string $source_name
) : string
{
$matchs = null;
$result = preg_match('/^[0-9a-zA-Z]+$/', $source_name, $matches);
return ($result === 1);
}
2025-09-09 12:07:53 +02:00
/**
*/
function load(
string $path
) : void
{
2025-09-09 19:43:23 +02:00
$data_raw = \json_decode(
2025-09-09 12:07:53 +02:00
\file_get_contents(
$path
),
true
);
2025-09-09 19:43:23 +02:00
{
$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;
}
2025-09-21 13:27:37 +02:00
// sources
2025-09-09 19:43:23 +02:00
{
2025-09-21 13:27:37 +02:00
$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;
}
}
);
2025-09-09 19:43:23 +02:00
}
// settings
{
$settings = new struct_settings();
$settings->timezone = (($data_raw['settings'] ?? [])['timezone'] ?? 'UTC');
$data->settings = $settings;
}
_state::$data = $data;
}
2025-09-09 12:07:53 +02:00
}
/**
*/
function get(
2025-09-09 19:43:23 +02:00
) : struct_root
2025-09-09 12:07:53 +02:00
{
2025-09-09 23:17:19 +02:00
if (_state::$data === null)
{
throw (new \Exception('not yet loaded'));
}
else
{
return _state::$data;
}
2025-09-09 12:07:53 +02:00
}
?>