rosavox/source/helpers.php

79 lines
1.1 KiB
PHP

<?php
namespace rosavox\helpers;
/**
*/
class cache_state
{
public static array $pool = [];
}
/**
*/
function cache_get(string $key, \Closure $retrieve)
{
if (\array_key_exists($key, cache_state::$pool))
{
$value = cache_state::$pool[$key];
}
else
{
$value = ($retrieve)();
cache_state::$pool[$key] = $value;
}
return $value;
}
/**
*/
function string_coin(string $template, array $arguments) : string
{
$result = $template;
foreach ($arguments as $key => $value)
{
$result = \str_replace(\sprintf('{{%s}}', $key), $value, $result);
}
return $result;
}
/**
*/
function render(string $template_name, array $arguments) : string
{
return string_coin(
cache_get(
string_coin(
'template.{{name}}',
[
'name' => $template_name,
]
),
fn() => \file_get_contents(
string_coin(
'{{directory}}/templates/{{name}}.html.tpl',
[
'directory' => __DIR__,
'name' => $template_name,
]
)
)
),
$arguments
);
}
/**
*/
function navigate(string $target) : void
{
\header('Location: ' . $target);
}
?>