core/source/conf.php

131 lines
2.3 KiB
PHP

<?php
namespace davina\conf;
require_once(DIR_LOGIC . '/helpers/list.php');
/**
*/
class _state
{
public static $data = null;
}
/**
*/
function validate_realm_name(
string $realm_name
) : string
{
$matchs = null;
$result = preg_match('/^[0-9a-zA-Z]+$/', $realm_name, $matches);
return ($result === 1);
}
/**
*/
function load(
string $path
) : void
{
$data_raw = \json_decode(
\file_get_contents(
$path
),
true
);
_state::$data = [
/**
* @todo check for name duplicates
*/
'realms' => \davina\helpers\list_\map(
$data_raw['realms'],
function ($realm_raw) {
if (! validate_realm_name($realm_raw['name']))
{
throw (new \Exception(\sprintf('invalid realm name: "%s"', $realm_raw['name'])));
}
else
{
return [
'name' => $realm_raw['name'],
'auth' => (function ($auth_raw) use ($realm_raw) {
switch ($auth_raw['kind'])
{
case 'pass_through':
{
return [
'kind' => 'pass_through',
'data' => null
];
break;
}
case 'static':
{
return [
'kind' => 'static',
'data' => [
'password' => ($auth_raw['data']['password'] ?? $realm_raw['name']),
]
];
break;
}
default:
{
throw (new \Exception(\sprintf('invalid auth kind: %s', $auth_raw['kind'])));
break;
}
}
}) ($realm_raw['auth']),
'source' => (function ($source_raw) {
switch ($source_raw['kind'])
{
case 'ics_feed':
{
return [
'kind' => 'ics_feed',
'data' => [
'url' => $source_raw['data']['url'],
'lifetime' => ($source_raw['data']['lifetime'] ?? (60 * 15)),
'conflate' => ($source_raw['data']['conflate'] ?? true),
]
];
break;
}
default:
{
throw (new \Exception(\sprintf('invalid source kind: %s', $source_raw['kind'])));
}
}
}) ($realm_raw['source']),
];
}
}
),
'settings' => [
'timezone' => (($data_raw['settings'] ?? [])['timezone'] ?? 'UTC'),
]
];
}
/**
*/
function get(
)
{
if (_state::$data === null)
{
throw (new \Exception('not yet loaded'));
}
else
{
return _state::$data;
}
}
?>