2023-03-05 02:02:46 +01:00
|
|
|
<?php
|
|
|
|
require_once("config.php");
|
2023-03-08 00:09:38 +01:00
|
|
|
require_once("templates.php");
|
|
|
|
require_once("activity.php");
|
2023-03-05 02:02:46 +01:00
|
|
|
|
|
|
|
function storeActivity(Activity $activity): void
|
|
|
|
{
|
2023-03-08 00:09:38 +01:00
|
|
|
global $ACTIVITY_FILE;
|
|
|
|
storeObject($activity, $ACTIVITY_FILE);
|
2023-03-05 02:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadActivity(): Activity
|
|
|
|
{
|
2023-03-08 00:09:38 +01:00
|
|
|
global $ACTIVITY_FILE;
|
|
|
|
return loadObject($ACTIVITY_FILE, Activity::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeTemplates(Templates $templates): void
|
|
|
|
{
|
|
|
|
global $TEMPLATES_FILE;
|
|
|
|
storeObject($templates, $TEMPLATES_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadTemplates(): Templates
|
|
|
|
{
|
|
|
|
global $TEMPLATES_FILE;
|
|
|
|
if (!file_exists($TEMPLATES_FILE)) {
|
|
|
|
// Create initial template file
|
|
|
|
$templates = new Templates();
|
|
|
|
$templates->loadDefaultTemplates();
|
|
|
|
storeTemplates($templates);
|
|
|
|
return $templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadObject($TEMPLATES_FILE, Templates::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeObject(object $object, string $file): void
|
|
|
|
{
|
|
|
|
$file = fopen($file, "w");
|
|
|
|
fwrite($file, json_encode($object, JSON_PRETTY_PRINT));
|
2023-03-05 02:02:46 +01:00
|
|
|
fclose($file);
|
2023-03-08 00:09:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
|
|
|
function loadObject(string $file, string $class): object
|
|
|
|
{
|
|
|
|
$json = file_get_contents($file);
|
|
|
|
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
return parseObject($data, $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ReflectionException
|
|
|
|
*/
|
|
|
|
function parseObject(array $data, string $class): object
|
|
|
|
{
|
|
|
|
$reflection = new ReflectionClass($class);
|
|
|
|
$instance = $reflection->newInstanceWithoutConstructor();
|
|
|
|
foreach ($data as $property => $value) {
|
|
|
|
$propertyReflection = $reflection->getProperty($property);
|
|
|
|
$propertyReflection->setAccessible(true);
|
|
|
|
$propertyReflection->setValue($instance, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$instance->initialLoad();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
2024-01-09 22:07:36 +01:00
|
|
|
}
|