LiquidRenderer.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Services;
  3. use App\Services\LiquidTags\LiquidTagCollection;
  4. use Illuminate\Support\Facades\Cache;
  5. use App\Http\Controllers\ProductController;
  6. use App\Services\LiquidTags\LiquidTagProduct;
  7. use App\Services\LiquidTags\LiquidTagVideo;
  8. use App\Services\LiquidTags\LiquidTagBanner;
  9. use App\Services\LiquidTags\LiquidTagContactUs;
  10. use Liquid\Liquid;
  11. use Liquid\Template;
  12. use Liquid\Cache\File as FileCache;
  13. use App\Services\LiquidTags\LiquidTagPage; // 添加对自定义标签的引用
  14. use App\Services\LiquidFilters\Filters;
  15. use App\Helpers\SiteCache;
  16. class LiquidRenderer
  17. {
  18. protected static ?string $baseTemplatePath = null;
  19. // 渲染 Liquid 模板
  20. public static function render(string $templateName, array $data = [],?string $cacheKey = null): string
  21. {
  22. self::initializeLiquidSettings();
  23. self::initializeBaseTemplatePath();
  24. // 如果提供了缓存键,则检查和设置缓存
  25. if (config('liquid.cache_enabled')&&$cacheKey) {
  26. $cacheDuration = config('liquid.cache_duration', 300); // 默认缓存时间为 300 秒
  27. // 检查缓存是否存在
  28. $domain=app('dist')->domain;
  29. if(!$domain)
  30. {
  31. abort('403');
  32. }
  33. return Cache::tags([$domain, 'dist'])->remember("liquid_{$cacheKey}", $cacheDuration, function () use ($templateName, $data) {
  34. return self::processTemplate($templateName, $data);
  35. });
  36. }
  37. // 如果没有提供缓存键,直接渲染模板
  38. return self::processTemplate($templateName, $data);
  39. }
  40. // 处理模板渲染逻辑
  41. private static function processTemplate(string $templateName, array $data): string
  42. {
  43. $template = self::createTemplateInstance();
  44. $template->registerTag('page', LiquidTagPage::class);
  45. $template->registerTag('product', LiquidTagProduct::class);
  46. $template->registerTag('video', LiquidTagVideo::class);
  47. $template->registerTag('contact', LiquidTagContactUs::class);
  48. $template->registerTag('banner', LiquidTagBanner::class);
  49. $template->registerTag('contactus', LiquidTagContactUs::class);
  50. $template->registerTag('collection', LiquidTagCollection::class);
  51. $template->registerFilter(Filters::class);
  52. // 获取全局配置,合并到当前配置
  53. $config = self::getGlobalConfig();
  54. $data['site'] = array_merge($data['site'] ?? [], $config);
  55. try {
  56. $parsedTemplate = $template->parseFile($templateName);
  57. } catch (\Exception $e) {
  58. throw new \RuntimeException("Template not found: {$templateName}", 0, $e);
  59. }
  60. $now_string=date('Y-m-d H:i:s');
  61. return $parsedTemplate->render($data);//."<!-- CACHE {$now_string}-->";
  62. }
  63. // 初始化基础模板路径
  64. private static function initializeBaseTemplatePath(): void
  65. {
  66. // 获取分销
  67. $dist=app('dist');
  68. // 使用默认值的函数封装,避免重复逻辑
  69. $template_dist_id = $dist->id ?? trim(config('liquid.template_dist_id'));
  70. $template_name = $dist->template_name ?? trim(config('liquid.template_name'));
  71. if (self::$baseTemplatePath === null) {
  72. $preview = isset($_GET['mtb-preview']) ? $_GET['mtb-preview'] : false;
  73. if ($preview) {
  74. self::$baseTemplatePath = rtrim(config('liquid.preview_template_path'), '/') . '/' .
  75. trim($template_dist_id). '/' .
  76. ltrim($template_name, '/');
  77. } else {
  78. self::$baseTemplatePath = rtrim(config('liquid.template_path'), '/') . '/' .
  79. trim($template_dist_id). '/' .
  80. ltrim($template_name, '/');
  81. }
  82. }
  83. }
  84. // 创建模板实例
  85. private static function createTemplateInstance(): Template
  86. {
  87. $template = new Template(self::$baseTemplatePath);
  88. // 检查配置文件是否启用缓存
  89. $enableCache = config('liquid.cache_enabled', false); // 默认不开启缓存
  90. if ($enableCache) {
  91. $template->setCache(new FileCache(['cache_dir' => storage_path('framework/cache/data')]));
  92. }
  93. return $template;
  94. }
  95. public static function getBaseTemplatePath(): string
  96. {
  97. // 确保路径已经初始化
  98. if (self::$baseTemplatePath === null) {
  99. self::initializeBaseTemplatePath();
  100. }
  101. return self::$baseTemplatePath;
  102. }
  103. // 设置 Liquid 渲染的全局配置
  104. private static function initializeLiquidSettings(): void
  105. {
  106. Liquid::set('INCLUDE_SUFFIX', '');
  107. Liquid::set('INCLUDE_PREFIX', '');
  108. Liquid::set('INCLUDE_ALLOW_EXT', true);
  109. Liquid::set('ESCAPE_BY_DEFAULT', true);
  110. }
  111. /**
  112. * 获取全局配置
  113. */
  114. private static function getGlobalConfig(): array
  115. {
  116. return [
  117. 'site_title' => config('app.name'),
  118. 'image_base_url' => config('liquid.image_base_url'),
  119. 'asset_base_url' =>config('liquid.asset_base_url'),
  120. 'font_base_url' => config('liquid.font_base_url'),
  121. 'dist' => app('dist'),
  122. 'menus_header'=>app('menus_header'),
  123. 'menus_footer'=>app('menus_footer'),
  124. 'current_url'=>app('current_url'),
  125. ];
  126. }
  127. }