rosavox/source/services/doc.php

267 lines
4.7 KiB
PHP
Raw Normal View History

2025-05-22 06:26:16 +00:00
<?php
namespace rosavox\services\doc;
require_once('helpers/string.php');
require_once('helpers/misc.php');
require_once('entities/doc.php');
require_once('repositories/doc.php');
2025-05-22 06:26:16 +00:00
/**
*/
function name(int $id) : string
{
return \sprintf('doc-%04u', $id);
}
/**
*/
2025-05-22 19:56:16 +00:00
function readable_path(int $id) : string
2025-05-22 06:26:16 +00:00
{
2025-05-22 19:56:16 +00:00
return \sprintf('docs/%s.md', name($id));
2025-05-22 06:26:16 +00:00
}
/**
*/
2025-05-22 19:56:16 +00:00
function audible_path(int $id) : string
2025-05-22 06:26:16 +00:00
{
2025-05-22 19:56:16 +00:00
return \sprintf('docs/%s.oga', name($id));
2025-05-22 06:26:16 +00:00
}
/**
*/
function generate_readable(int $id, \rosavox\entities\doc\entity $doc) : void
2025-05-22 06:26:16 +00:00
{
$markdown = \rosavox\helpers\string_\coin(
"# {{value_title}}
## {{label_authors}}
{{value_authors}}
## {{label_content}}
{{value_content}}
{{macro_reasoning}}",
[
'label_title' => \rosavox\helpers\misc\translate('domain.doc.title'),
'value_title' => $doc->title,
2025-05-22 06:26:16 +00:00
'label_authors' => \rosavox\helpers\misc\translate('domain.doc.authors'),
'value_authors' => \implode(
"\n",
\array_map(
fn ($author) => \rosavox\helpers\string_\coin(
'- {{author}}',
[
'author' => $author,
]
),
$doc->authors
2025-05-22 06:26:16 +00:00
)
),
'label_content' => \rosavox\helpers\misc\translate('domain.doc.content'),
'value_content' => $doc->content,
2025-05-22 06:26:16 +00:00
'macro_reasoning' => (
($doc->reasoning === null)
2025-05-22 06:26:16 +00:00
?
''
:
\rosavox\helpers\string_\coin(
"
## {{label_reasoning}}
{{value_reasoning}}",
[
'label_reasoning' => \rosavox\helpers\misc\translate('domain.doc.reasoning'),
'value_reasoning' => $doc->reasoning,
2025-05-22 06:26:16 +00:00
]
)
)
]
);
\file_put_contents(
readable_path($id),
$markdown
);
}
/**
*/
function remove_readable(int $id) : void
2025-05-22 19:56:16 +00:00
{
$path = readable_path($id);
if (! \file_exists($path))
{
// do nothing
}
else
{
\unlink($path);
}
}
/**
*/
function generate_audible(int $id, \rosavox\entities\doc\entity $doc) : void
2025-05-22 06:26:16 +00:00
{
$pause = " .\n";
$text = \rosavox\helpers\string_\coin(
"{{value_title}}{{pause}}{{label_authors}}: {{value_authors}}{{pause}}{{label_content}}: {{value_content}}{{macro_reasoning}}",
[
'pause' => $pause,
'label_title' => \rosavox\helpers\misc\translate('domain.doc.title'),
'value_title' => $doc->title,
2025-05-22 06:26:16 +00:00
'label_authors' => \rosavox\helpers\misc\translate('domain.doc.authors'),
'value_authors' => \implode($pause, $doc->authors),
2025-05-22 06:26:16 +00:00
'label_content' => \rosavox\helpers\misc\translate('domain.doc.content'),
'value_content' => $doc->content,
2025-05-22 06:26:16 +00:00
'macro_reasoning' => (
($doc->reasoning === null)
2025-05-22 06:26:16 +00:00
?
''
:
\rosavox\helpers\string_\coin(
"{{pause}}{{label_reasoning}}: {{value_reasoning}}",
[
'pause' => $pause,
'label_reasoning' => \rosavox\helpers\misc\translate('domain.doc.reasoning'),
'value_reasoning' => $doc->reasoning,
2025-05-22 06:26:16 +00:00
]
)
)
]
);
\rosavox\helpers\misc\generate_audio(
$text,
2025-05-22 19:56:16 +00:00
audible_path($id),
[
'blocking' => false,
]
2025-05-22 06:26:16 +00:00
);
}
2025-05-22 19:56:16 +00:00
/**
*/
function remove_audible(int $id) : void
2025-05-22 19:56:16 +00:00
{
$path = audible_path($id);
if (! \file_exists($path))
{
// do nothing
}
else
{
\unlink($path);
}
}
2025-05-22 06:26:16 +00:00
/**
*/
function dump() : array
2025-05-22 06:26:16 +00:00
{
return \rosavox\repositories\doc\repo::get_instance()->list_();
2025-05-22 06:26:16 +00:00
}
/**
*/
function get(int $id) : \rosavox\entities\doc\entity
2025-05-22 06:26:16 +00:00
{
return \rosavox\repositories\doc\repo::get_instance()->read($id);
2025-05-22 06:26:16 +00:00
}
/**
*/
function add(\rosavox\entities\doc\entity $doc) : int
2025-05-22 06:26:16 +00:00
{
$id = \rosavox\repositories\doc\repo::get_instance()->create($doc);
2025-05-22 06:26:16 +00:00
generate_readable($id, $doc);
2025-05-22 19:56:16 +00:00
generate_audible($id, $doc);
2025-05-22 06:26:16 +00:00
return $id;
}
/**
*/
function add_examples() : void
2025-05-22 06:26:16 +00:00
{
$entities = [
new \rosavox\entities\doc\entity(
'Freibier bei Parteitagen',
[
'Björn Biernot',
'Doreen Dauerdurst',
],
'Der Landesverband möge beschließen, dass zu Beginn eines jeden Parteitags für jeden Deligierten mindestens zwei Flaschen Bier auf den zugehörigen Platz zu stellen sind.',
'Wir haben Durst!',
),
new \rosavox\entities\doc\entity(
'Götterdämmerung',
[
'Fenriswolf',
],
'Der Allvater hat mich betrogen. Ich werde ihn und seine elende Asenbrut verschlingen. Diese Welt wird enden.',
null,
),
];
foreach ($entities as $entity)
{
add($entity);
}
2025-05-22 06:26:16 +00:00
}
2025-05-22 06:26:16 +00:00
/**
*/
function change(int $id, \rosavox\entities\doc\entity $doc) : void
2025-05-22 06:26:16 +00:00
{
\rosavox\repositories\doc\repo::get_instance()->update($id, $doc);
2025-05-22 19:56:16 +00:00
remove_readable($id);
remove_audible($id);
generate_readable($id, $doc);
generate_audible($id, $doc);
2025-05-22 06:26:16 +00:00
}
/**
*/
function remove(int $id) : void
2025-05-22 06:26:16 +00:00
{
\rosavox\repositories\doc\repo::get_instance()->delete($id);
remove_readable($id);
remove_audible($id);
2025-05-22 06:26:16 +00:00
}
/**
*/
function init() : void
{
\rosavox\repositories\doc\repo::get_instance()->setup();
if (empty(dump()))
2025-05-22 06:26:16 +00:00
{
add_examples();
}
else
{
// do nothing
}
}
?>