rosavox/source/helpers/misc.php

106 lines
1.8 KiB
PHP
Raw Normal View History

2025-05-22 06:26:16 +00:00
<?php
namespace rosavox\helpers\misc;
require_once('string.php');
require_once('cache.php');
2025-05-22 19:56:16 +00:00
/**
*/
function path_wrapped(string $path) : ?string
{
return (\file_exists($path) ? $path : null);
}
2025-05-22 06:26:16 +00:00
/**
*/
function render(string $template_name, array $arguments) : string
{
return \rosavox\helpers\string_\coin(
\rosavox\helpers\cache\get(
\rosavox\helpers\string_\coin(
'template.{{name}}',
[
'name' => $template_name,
]
),
fn() => \file_get_contents(
\rosavox\helpers\string_\coin(
'{{directory}}/templates/{{name}}.html.tpl',
[
'directory' => /*__DIR__*/'.',
'name' => $template_name,
]
)
)
),
$arguments
);
}
/**
*/
function navigate(string $target) : void
{
\header('Location: ' . $target);
}
/**
*/
2025-05-22 19:56:16 +00:00
function generate_audio(
string $input_value,
string $output_path,
?array $options = null
) : void
2025-05-22 06:26:16 +00:00
{
2025-05-22 19:56:16 +00:00
$options = \array_merge(
[
'blocking' => false,
],
($options ?? [])
);
$key = \hash('sha256', $input_value);
$input_path = \rosavox\helpers\string_\coin(
'/tmp/{{key}}.txt',
2025-05-22 06:26:16 +00:00
[
'key' => $key,
]
);
2025-05-22 19:56:16 +00:00
\file_put_contents($input_path, $input_value);
2025-05-22 06:26:16 +00:00
$command = \rosavox\helpers\string_\coin(
2025-05-22 19:56:16 +00:00
'scripts/generate-audio {{input_path}} {{output_path}}{{suffix}}',
2025-05-22 06:26:16 +00:00
[
2025-05-22 19:56:16 +00:00
'input_path' => $input_path,
'output_path' => $output_path,
'suffix' => ($options['blocking'] ? '' : ' > /dev/null 2>&1 &'),
2025-05-22 06:26:16 +00:00
]
);
2025-05-22 19:56:16 +00:00
// \unlink($input_path);
// \error_log(\sprintf("-- %s\n", $command));
2025-05-22 06:26:16 +00:00
\exec($command);
}
/**
*/
function translate(string $key, ?array $options = null) : string
{
$strings = \rosavox\helpers\cache\get(
'strings',
fn () => \json_decode(\file_get_contents('strings.json'), true)
);
$options = \array_merge(
[
'language' => 'de',
],
($options ?? [])
);
return ($strings[$options['language']][$key] ?? \sprintf('{%s}', $key));
}
?>