forked from laysense/laysite
Launch LaySite visual editor and MCP workspace
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use PDO;
|
||||
use RuntimeException;
|
||||
|
||||
final class SiteDesign
|
||||
{
|
||||
public static function parseLinks(string $value): array
|
||||
{
|
||||
$links = [];
|
||||
foreach (preg_split('/\R/u', $value, -1, PREG_SPLIT_NO_EMPTY) as $line) {
|
||||
if (trim($line) === '') {
|
||||
continue;
|
||||
}
|
||||
[$label, $url] = array_map('trim', array_pad(explode('|', $line, 2), 2, ''));
|
||||
if ($label === '' || mb_strlen($label) > 70 || strlen($url) > 1000 || !Html::url($url) || count($links) >= 20) {
|
||||
throw new RuntimeException('每行填写「文案 | 链接」,最多 20 条。');
|
||||
}
|
||||
$links[] = [$url, $label];
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
public static function linkText(array $links): string
|
||||
{
|
||||
return implode("\n", array_map(static fn (array $link): string => $link[1] . ' | ' . $link[0], $links));
|
||||
}
|
||||
|
||||
public static function saveGeneral(string $name, string $origin, int $version): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($name, $origin, $version): void {
|
||||
$site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
if (($site['revisions']['general'] ?? 0) !== $version) {
|
||||
throw new RuntimeException('站点设置已更新,请重新打开后再保存。');
|
||||
}
|
||||
$site['name'] = $name;
|
||||
$site['cdn'] = ['origin' => $origin];
|
||||
$site['revisions']['general'] = $version + 1;
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user