|
@@ -2,6 +2,8 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
+use App\Http\Controllers\ProductController;
|
|
|
use App\Services\LiquidTags\LiquidTagProduct;
|
|
|
use App\Services\LiquidTags\LiquidTagVideo;
|
|
|
use App\Services\LiquidTags\LiquidTagBanner;
|
|
@@ -22,12 +24,60 @@ class LiquidRenderer
|
|
|
protected static ?string $baseTemplatePath = null;
|
|
|
|
|
|
// 渲染 Liquid 模板
|
|
|
- public static function render(string $templateName, array $data = []): string
|
|
|
+ public static function render(string $templateName, array $data = [],?string $cacheKey = null): string
|
|
|
{
|
|
|
|
|
|
self::initializeLiquidSettings();
|
|
|
self::initializeBaseTemplatePath();
|
|
|
|
|
|
+
|
|
|
+ // 如果提供了缓存键,则检查和设置缓存
|
|
|
+ if ($cacheKey) {
|
|
|
+ $cacheDuration = config('liquid.cache_duration', 300); // 默认缓存时间为 300 秒
|
|
|
+
|
|
|
+ // 检查缓存是否存在
|
|
|
+ $domain=app('dist')->domain;
|
|
|
+ if(!$domain)
|
|
|
+ {
|
|
|
+ abort('403');
|
|
|
+ }
|
|
|
+ return Cache::tags([$domain, 'dist'])->remember("liquid_{$cacheKey}", $cacheDuration, function () use ($templateName, $data) {
|
|
|
+ return self::processTemplate($templateName, $data);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有提供缓存键,直接渲染模板
|
|
|
+ return self::processTemplate($templateName, $data);
|
|
|
+
|
|
|
+// $template = self::createTemplateInstance();
|
|
|
+//
|
|
|
+// $template->registerTag('page', LiquidTagPage::class);
|
|
|
+// $template->registerTag('product', LiquidTagProduct::class);
|
|
|
+// $template->registerTag('video', LiquidTagVideo::class);
|
|
|
+// $template->registerTag('contact', LiquidTagContactUs::class);
|
|
|
+// $template->registerTag('banner', LiquidTagBanner::class);
|
|
|
+// $template->registerTag('contactus', LiquidTagContactUs::class);
|
|
|
+// $template->registerFilter(Filters::class);
|
|
|
+//
|
|
|
+//
|
|
|
+// //获取全局配置,合并到当前配置
|
|
|
+// $config = self::getGlobalConfig();
|
|
|
+// $data['site'] = array_merge($data['site'] ?? [], $config);
|
|
|
+//
|
|
|
+//
|
|
|
+// try {
|
|
|
+// $parsedTemplate = $template->parseFile($templateName);
|
|
|
+// } catch (\Exception $e) {
|
|
|
+// throw new \RuntimeException("Template not found: {$templateName}", 0, $e);
|
|
|
+// }
|
|
|
+//
|
|
|
+// return $parsedTemplate->render($data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 处理模板渲染逻辑
|
|
|
+ private static function processTemplate(string $templateName, array $data): string
|
|
|
+ {
|
|
|
$template = self::createTemplateInstance();
|
|
|
|
|
|
$template->registerTag('page', LiquidTagPage::class);
|
|
@@ -38,22 +88,20 @@ class LiquidRenderer
|
|
|
$template->registerTag('contactus', LiquidTagContactUs::class);
|
|
|
$template->registerFilter(Filters::class);
|
|
|
|
|
|
-
|
|
|
- //获取全局配置,合并到当前配置
|
|
|
+ // 获取全局配置,合并到当前配置
|
|
|
$config = self::getGlobalConfig();
|
|
|
$data['site'] = array_merge($data['site'] ?? [], $config);
|
|
|
|
|
|
-
|
|
|
try {
|
|
|
$parsedTemplate = $template->parseFile($templateName);
|
|
|
} catch (\Exception $e) {
|
|
|
throw new \RuntimeException("Template not found: {$templateName}", 0, $e);
|
|
|
}
|
|
|
|
|
|
- return $parsedTemplate->render($data);
|
|
|
+ $now_string=date('Y-m-d H:i:s');
|
|
|
+ return $parsedTemplate->render($data);//."<!-- CACHE {$now_string}-->";
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// 初始化基础模板路径
|
|
|
private static function initializeBaseTemplatePath(): void
|
|
|
{
|
|
@@ -77,7 +125,11 @@ class LiquidRenderer
|
|
|
private static function createTemplateInstance(): Template
|
|
|
{
|
|
|
$template = new Template(self::$baseTemplatePath);
|
|
|
- //$template->setCache(new FileCache(['cache_dir' => storage_path('framework/cache/data')]));
|
|
|
+ // 检查配置文件是否启用缓存
|
|
|
+ $enableCache = config('liquid.cache_enabled', false); // 默认不开启缓存
|
|
|
+ if ($enableCache) {
|
|
|
+ $template->setCache(new FileCache(['cache_dir' => storage_path('framework/cache/data')]));
|
|
|
+ }
|
|
|
return $template;
|
|
|
}
|
|
|
|