Files
dafuweng-saiadmin6.x/server/plugin/saiadmin/app/logic/system/SystemAdminGuideLogic.php

73 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
namespace plugin\saiadmin\app\logic\system;
use plugin\saiadmin\exception\ApiException;
/**
* 后台操作指南逻辑(读写 server/docs/ADMIN_GUIDE.md
*/
class SystemAdminGuideLogic
{
private const GUIDE_FILENAME = 'ADMIN_GUIDE.md';
/**
* 获取指南 Markdown 文件绝对路径
*/
public function getFilePath(): string
{
return base_path() . DIRECTORY_SEPARATOR . 'docs' . DIRECTORY_SEPARATOR . self::GUIDE_FILENAME;
}
/**
* 读取指南内容
* @return array{content: string, file_path: string, update_time: string|null}
*/
public function read(): array
{
$filePath = $this->getFilePath();
if (! is_file($filePath)) {
throw new ApiException('admin guide file not found');
}
$content = file_get_contents($filePath);
if ($content === false) {
throw new ApiException('failed to read admin guide file');
}
return [
'content' => $content,
'file_path' => 'docs/' . self::GUIDE_FILENAME,
'update_time' => date('Y-m-d H:i:s', filemtime($filePath)),
];
}
/**
* 保存指南内容到 Markdown 文件
* @param string $content
* @return array{content: string, file_path: string, update_time: string}
*/
public function save(string $content): array
{
$filePath = $this->getFilePath();
$dir = dirname($filePath);
if (! is_dir($dir) && ! mkdir($dir, 0755, true) && ! is_dir($dir)) {
throw new ApiException('failed to create docs directory');
}
$result = file_put_contents($filePath, $content, LOCK_EX);
if ($result === false) {
throw new ApiException('failed to save admin guide file');
}
clearstatcache(true, $filePath);
return [
'content' => $content,
'file_path' => 'docs/' . self::GUIDE_FILENAME,
'update_time' => date('Y-m-d H:i:s', filemtime($filePath)),
];
}
}