forked from laysense/laysense.cn
Rebuild Laysense site with Webman and SQLite CMS
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use PDO;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
final class Database
|
||||
{
|
||||
private static ?PDO $connection = null;
|
||||
|
||||
public static function connect(): PDO
|
||||
{
|
||||
if (self::$connection !== null) {
|
||||
return self::$connection;
|
||||
}
|
||||
|
||||
$path = getenv('LAYSENSE_DB') ?: dirname(__DIR__, 2) . '/storage/laysense.sqlite';
|
||||
if (!is_dir(dirname($path)) && !mkdir(dirname($path), 0750, true) && !is_dir(dirname($path))) {
|
||||
throw new RuntimeException('无法创建 SQLite 存放目录。');
|
||||
}
|
||||
|
||||
$pdo = new PDO('sqlite:' . $path, options: [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
|
||||
$pdo->exec('PRAGMA busy_timeout = 5000');
|
||||
$pdo->exec('PRAGMA foreign_keys = ON');
|
||||
self::$connection = $pdo;
|
||||
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
public static function initialize(): void
|
||||
{
|
||||
$root = dirname(__DIR__, 2);
|
||||
$seed = json_decode(file_get_contents($root . '/resources/seed.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$pdo = self::connect();
|
||||
$pdo->exec('PRAGMA journal_mode = WAL');
|
||||
|
||||
self::write(function (PDO $pdo) use ($root, $seed): void {
|
||||
$pdo->exec(file_get_contents($root . '/resources/schema.sql'));
|
||||
$site = $pdo->prepare('INSERT OR IGNORE INTO site_settings (id, payload) VALUES (1, ?)');
|
||||
$site->execute([self::json($seed['site'])]);
|
||||
|
||||
$pageQuery = $pdo->prepare('INSERT OR IGNORE INTO pages
|
||||
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks, partners)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
$sectionQuery = $pdo->prepare('INSERT OR IGNORE INTO sections
|
||||
(page_slug, key, title, lead, style, body, position) VALUES (?, ?, ?, ?, ?, ?, ?)');
|
||||
foreach ($seed['pages'] as $page) {
|
||||
$pageQuery->execute([
|
||||
$page['slug'], $page['name'], $page['title'], $page['description'], $page['heading'],
|
||||
$page['accent'], $page['lead'], $page['image'], $page['alt'],
|
||||
self::json($page['actions']), self::json($page['quicklinks']), self::json($page['partners'] ?? []),
|
||||
]);
|
||||
if ($pageQuery->rowCount() !== 1) {
|
||||
continue;
|
||||
}
|
||||
foreach ($page['sections'] as $index => $section) {
|
||||
$sectionQuery->execute([
|
||||
$page['slug'], $section['key'], $section['title'], $section['lead'],
|
||||
$section['style'], Html::sanitize($section['body']), $index,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$article = $pdo->prepare('INSERT OR IGNORE INTO articles
|
||||
(slug, title, category, summary, body, tags, status, published_at, updated_at, legacy_path)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
foreach ($seed['articles'] as $item) {
|
||||
$exists = $pdo->prepare('SELECT 1 FROM article_redirects WHERE slug = ?');
|
||||
$exists->execute([$item['slug']]);
|
||||
if ($exists->fetchColumn()) {
|
||||
continue;
|
||||
}
|
||||
$article->execute([
|
||||
$item['slug'], $item['title'], $item['category'], $item['summary'], $item['body'],
|
||||
self::json($item['tags']), $item['status'], $item['date'], $item['date'], $item['legacy'],
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function write(callable $operation): mixed
|
||||
{
|
||||
$pdo = self::connect();
|
||||
$pdo->exec('BEGIN IMMEDIATE');
|
||||
try {
|
||||
$result = $operation($pdo);
|
||||
$pdo->exec('COMMIT');
|
||||
|
||||
return $result;
|
||||
} catch (Throwable $error) {
|
||||
$pdo->exec('ROLLBACK');
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
||||
public static function json(array $data): string
|
||||
{
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user