240 lines
4.1 KiB
PHP
240 lines
4.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 database_map_type(string $type)
|
|
{
|
|
switch ($type)
|
|
{
|
|
case 'integer': return \SQLITE3_INTEGER;
|
|
case 'string': return \SQLITE3_TEXT;
|
|
default: throw (new \Exception(\sprintf('unhandled type: %s', $type)));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function database_get(string $query_template, array $arguments) : array
|
|
{
|
|
$connection = new \SQLite3('data.sqlite');
|
|
$rows = $connection->query($query_template);
|
|
/*
|
|
$query = "SELECT * FROM books";
|
|
$result = $db->query($query);
|
|
|
|
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
|
*/
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function database_put(string $query_template, array $arguments) : int
|
|
{
|
|
$connection = new \SQLite3('data.sqlite');
|
|
$statement = $connection->prepare($query);
|
|
foreach ($arguments as $key => $value)
|
|
{
|
|
$statement->bindValue(
|
|
\sprintf(':%s', $key),
|
|
$value['value'],
|
|
database_map_type($value['type'])
|
|
);
|
|
}
|
|
if ($statement->execute()) {
|
|
// SELECT last_insert_rowid()
|
|
echo "Buch erfolgreich hinzugefügt!";
|
|
}
|
|
else
|
|
{
|
|
echo "Fehler beim Hinzufügen des Buches: " . $db->lastErrorMsg();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
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;
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
class class_crud_jsonfile
|
|
{
|
|
private string $path;
|
|
|
|
private \Closure $id_encode;
|
|
|
|
private \Closure $id_decode;
|
|
|
|
public function __construct(
|
|
string $path,
|
|
\Closure $id_encode,
|
|
\Closure $id_decode
|
|
)
|
|
{
|
|
$this->path = $path;
|
|
$this->id_encode = $id_encode;
|
|
$this->id_decode = $id_decode;
|
|
}
|
|
|
|
private function get() : array
|
|
{
|
|
$content = (\file_exists($this->path) ? \file_get_contents($this->path) : null);
|
|
return (
|
|
($content === null)
|
|
?
|
|
['last_id' => 0, 'entries' => []]
|
|
:
|
|
\json_decode($content, true)
|
|
);
|
|
}
|
|
|
|
private function put(array $data) : void
|
|
{
|
|
$content = \json_encode($data, \JSON_PRETTY_PRINT);
|
|
\file_put_contents($this->path, $content);
|
|
}
|
|
|
|
public function list_() : array
|
|
{
|
|
$data = $this->get();
|
|
return \array_map(
|
|
fn ($id_encoded) => [
|
|
'id' => ($this->id_decode)($id_encoded),
|
|
'value' => $data['entries'][$id_encoded],
|
|
],
|
|
\array_keys($data['entries'])
|
|
);
|
|
}
|
|
|
|
public function read(int $id)
|
|
{
|
|
$data = $this->get();
|
|
$id_encoded = ($this->id_encode)($id);
|
|
if (! \array_key_exists($id_encoded, $data['entries']))
|
|
{
|
|
throw (new \Exception('not found'));
|
|
}
|
|
else
|
|
{
|
|
return $data['entries'][$id_encoded];
|
|
}
|
|
}
|
|
|
|
public function create($value) : int
|
|
{
|
|
$data = $this->get();
|
|
$id = ($data['last_id'] + 1);
|
|
$id_encoded = ($this->id_encode)($id);
|
|
$data['last_id'] = $id;
|
|
$data['entries'][$id_encoded] = $value;
|
|
$this->put($data);
|
|
return $id;
|
|
}
|
|
|
|
public function update(int $id, $value) : void
|
|
{
|
|
$data = $this->get();
|
|
$id_encoded = ($this->id_encode)($id);
|
|
if (! \array_key_exists($id_encoded, $data['entries']))
|
|
{
|
|
throw (new \Exception('not found'));
|
|
}
|
|
else
|
|
{
|
|
$data['entries'][$id_encoded] = $value;
|
|
$this->put($data);
|
|
}
|
|
}
|
|
|
|
public function delete(int $id) : void
|
|
{
|
|
$data = $this->get();
|
|
$id_encoded = ($this->id_encode)($id);
|
|
if (! \array_key_exists($id_encoded, $data['entries']))
|
|
{
|
|
throw (new \Exception('not found'));
|
|
}
|
|
else
|
|
{
|
|
unset($data['entries'][$id_encoded]);
|
|
$this->put($data);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
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);
|
|
}
|
|
|
|
|
|
?>
|