| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Services\Template;
- use App\Services\Template\TemplateParser;
- use App\Services\Template\Renderers\RendererFactory;
- use App\Services\Template\TemplateRegistry;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- class TemplateService
- {
- private TemplateParser $parser;
- private bool $cacheEnabled;
- private int $cacheTtl;
- public function __construct(bool $cacheEnabled = true, int $cacheTtl = 3600)
- {
- $this->parser = new TemplateParser();
- $this->cacheEnabled = $cacheEnabled;
- $this->cacheTtl = $cacheTtl;
- }
- /**
- * 解析并渲染内容
- */
- public function parseAndRender(string $content, string $format = 'json'): array
- {
- try {
- // 生成缓存键
- $cacheKey = $this->generateCacheKey($content, $format);
- if ($this->cacheEnabled && Cache::has($cacheKey)) {
- return Cache::get($cacheKey);
- }
- // 解析内容
- $document = $this->parser->parse($content);
- // 渲染内容
- $renderer = RendererFactory::create($format);
- $renderedContent = $renderer->render($document);
- $result = [
- 'data' => $format === 'json' ? json_decode($renderedContent, true) : $renderedContent,
- 'meta' => $document->meta
- ];
- // 缓存结果
- if ($this->cacheEnabled) {
- Cache::put($cacheKey, $result, $this->cacheTtl);
- }
- return $result;
- } catch (\Exception $e) {
- Log::error('Template parsing failed', [
- 'content' => substr($content, 0, 200),
- 'format' => $format,
- 'error' => $e->getMessage()
- ]);
- throw $e;
- }
- }
- /**
- * 仅解析,不渲染
- */
- public function parse(string $content): ParsedDocument
- {
- return $this->parser->parse($content);
- }
- /**
- * 仅渲染已解析的文档
- */
- public function render(ParsedDocument $document, string $format): string
- {
- $renderer = RendererFactory::create($format);
- return $renderer->render($document);
- }
- /**
- * 注册新模板
- */
- public function registerTemplate(string $name, array $config): void
- {
- $registry = $this->parser->getRegistry();
- $registry->registerTemplate($name, $config);
- // 清除相关缓存
- if ($this->cacheEnabled) {
- $this->clearTemplateCache($name);
- }
- }
- /**
- * 获取可用模板列表
- */
- public function getAvailableTemplates(): array
- {
- $registry = $this->parser->getRegistry();
- $templates = [];
- // 这里需要添加一个方法来获取所有模板
- // 为了示例,我们返回一些基本信息
- $basicTemplates = ['note', 'info', 'warning'];
- foreach ($basicTemplates as $templateName) {
- $template = $registry->getTemplate($templateName);
- if ($template) {
- $templates[$templateName] = [
- 'name' => $templateName,
- 'config' => $template,
- 'example' => $this->generateTemplateExample($templateName, $template)
- ];
- }
- }
- return $templates;
- }
- /**
- * 生成模板使用示例
- */
- private function generateTemplateExample(string $name, array $config): string
- {
- $params = [];
- $defaultParams = $config['defaultParams'] ?? [];
- foreach ($defaultParams as $index => $paramName) {
- $params[] = $paramName . '=示例值' . ($index + 1);
- }
- return '{{' . $name . '|' . implode('|', $params) . '}}';
- }
- /**
- * 生成缓存键
- */
- private function generateCacheKey(string $content, string $format): string
- {
- return 'template_' . $format . '_' . md5($content);
- }
- /**
- * 清除模板相关缓存
- */
- private function clearTemplateCache(string $templateName): void
- {
- // 这里可以实现更精确的缓存清除逻辑
- Cache::flush(); // 简单起见,清除所有缓存
- }
- /**
- * 批量处理内容
- */
- public function batchProcess(array $contents, string $format = 'json'): array
- {
- $results = [];
- foreach ($contents as $index => $content) {
- try {
- $results[$index] = $this->parseAndRender($content, $format);
- } catch (\Exception $e) {
- $results[$index] = [
- 'success' => false,
- 'error' => $e->getMessage()
- ];
- }
- }
- return $results;
- }
- /**
- * 验证模板语法
- */
- public function validateSyntax(string $content): array
- {
- $errors = [];
- try {
- $document = $this->parser->parse($content);
- // 检查是否有未知模板
- foreach ($document->content as $node) {
- if ($node instanceof TemplateNode) {
- $registry = $this->parser->getRegistry();
- if (!$registry->hasTemplate($node->name)) {
- $errors[] = [
- 'type' => 'unknown_template',
- 'template' => $node->name,
- 'position' => $node->position,
- 'message' => "Unknown template: {$node->name}"
- ];
- }
- }
- }
- } catch (\Exception $e) {
- $errors[] = [
- 'type' => 'parse_error',
- 'message' => $e->getMessage()
- ];
- }
- return $errors;
- }
- }
|