-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathShopifyLiquidThemeEngine.cs
534 lines (465 loc) · 22.9 KB
/
ShopifyLiquidThemeEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DotLiquid.ViewEngine.Exceptions;
using LibSassHost;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Scriban;
using Scriban.Parsing;
using Scriban.Runtime;
using VirtoCommerce.LiquidThemeEngine.Scriban;
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Model.Caching;
using VirtoCommerce.Storefront.Model.Common;
using VirtoCommerce.Storefront.Model.Common.Caching;
using VirtoCommerce.Storefront.Model.Common.Exceptions;
using VirtoCommerce.Storefront.Model.StaticContent;
namespace VirtoCommerce.LiquidThemeEngine
{
/// <summary>
/// Shopify compliant theme folder structure and all methods for rendering
/// assets - storages for css, images and other assets
/// config - contains theme configuration
/// layout - master pages and layouts
/// locales - localization resources
/// snippets - snippets - partial views
/// templates - view templates
/// </summary>
public class ShopifyLiquidThemeEngine : ILiquidThemeEngine, ITemplateLoader
{
private readonly LiquidThemeEngineOptions _options;
private static readonly Regex _isLiquid = new Regex("[{}|]", RegexOptions.Compiled);
private const string _liquidTemplateFormat = "{0}.liquid";
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IStorefrontMemoryCache _memoryCache;
private readonly IContentBlobProvider _themeBlobProvider;
private readonly ISassFileManager _sassFileManager;
public ShopifyLiquidThemeEngine(IStorefrontMemoryCache memoryCache, IWorkContextAccessor workContextAccessor, IHttpContextAccessor httpContextAccessor,
IStorefrontUrlBuilder storeFrontUrlBuilder, IContentBlobProvider contentBlobProvider, ISassFileManager sassFileManager, IOptions<LiquidThemeEngineOptions> options)
{
_workContextAccessor = workContextAccessor;
_httpContextAccessor = httpContextAccessor;
UrlBuilder = storeFrontUrlBuilder;
_options = options.Value;
_memoryCache = memoryCache;
_themeBlobProvider = contentBlobProvider;
_sassFileManager = sassFileManager;
SassCompiler.FileManager = sassFileManager;
}
/// <summary>
/// Main work context
/// </summary>
public WorkContext WorkContext => _workContextAccessor.WorkContext;
/// <summary>
/// Current HttpContext
/// </summary>
public HttpContext HttpContext => _httpContextAccessor.HttpContext;
/// <summary>
/// Store url builder
/// </summary>
public IStorefrontUrlBuilder UrlBuilder { get; }
/// <summary>
/// Default master view name
/// </summary>
public string MasterViewName => _options.DefaultLayout;
/// <summary>
/// Current theme name
/// </summary>
public string CurrentThemeName => !string.IsNullOrEmpty(WorkContext.CurrentStore.ThemeName) ? WorkContext.CurrentStore.ThemeName : "default";
public string CurrentThemeSettingPath => Path.Combine(CurrentThemePath, "config", GetSettingsFilePath());
public string CurrentThemeLocalePath => Path.Combine(CurrentThemePath, "locales");
/// <summary>
/// The path for current theme
/// </summary>
private string CurrentThemePath => Path.Combine("Themes", WorkContext.CurrentStore.Id, CurrentThemeName);
//Relative path to the discovery of theme resources that weren't found by the current path.
private string BaseThemePath =>
!string.IsNullOrEmpty(_options.BaseThemePath) ? Path.Combine("Themes", _options.BaseThemePath) :
#pragma warning disable 618
// We need to use obsolete value here for backward compatibility.
!string.IsNullOrEmpty(_options.BaseThemeName) ? Path.Combine("Themes", _options.BaseThemeName, "default") : null;
#pragma warning restore 618
private string BaseThemeSettingPath => BaseThemePath != null ? Path.Combine(BaseThemePath, "config", "settings_data.json") : null;
public string BaseThemeLocalePath => BaseThemePath != null ? Path.Combine(BaseThemePath, "locales") : null;
#region ITemplateLoader members
public string GetPath(TemplateContext context, SourceSpan callerSpan, string templateName)
{
return ResolveTemplatePath(templateName);
}
public string Load(TemplateContext context, SourceSpan callerSpan, string templatePath)
{
var content = ReadTemplateByPath(templatePath);
return content;
}
public ValueTask<string> LoadAsync(TemplateContext context, SourceSpan callerSpan, string templatePath)
{
return new ValueTask<string>(Load(context, callerSpan, templatePath));
}
#endregion
#region ILiquidThemeEngine Members
public IEnumerable<string> DiscoveryPaths
{
get
{
var retVal = Enumerable.Empty<string>();
if (WorkContext.CurrentStore != null)
{
retVal = _options.TemplatesDiscoveryFolders.Select(x => Path.Combine(CurrentThemePath, x));
if (BaseThemePath != null)
{
retVal = retVal.Concat(_options.TemplatesDiscoveryFolders.Select(x => Path.Combine(BaseThemePath, x)));
}
}
return retVal;
}
}
/// <summary>
/// Return stream for requested asset file (used for search current and base themes assets)
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public async Task<Stream> GetAssetStreamAsync(string filePath)
{
Stream retVal = null;
var filePathWithoutExtension = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
//file.ext => file.ext || file.liquid || file.ext.liquid || file
var searchPatterns = new[] { filePath, string.Format(_liquidTemplateFormat, filePathWithoutExtension), string.Format(_liquidTemplateFormat, filePath), filePathWithoutExtension };
string currentThemeFilePath = null;
//try to search in current store theme
if (_themeBlobProvider.PathExists(Path.Combine(CurrentThemePath, "assets")))
{
currentThemeFilePath = searchPatterns.SelectMany(x => _themeBlobProvider.Search(Path.Combine(CurrentThemePath, "assets"), x, true)).FirstOrDefault();
}
//If not found by current theme path try find them by base path if it is defined
if (currentThemeFilePath == null && BaseThemePath != null)
{
currentThemeFilePath = searchPatterns.SelectMany(x => _themeBlobProvider.Search(Path.Combine(BaseThemePath, "assets"), x, true)).FirstOrDefault();
}
if (currentThemeFilePath != null)
{
retVal = _themeBlobProvider.OpenRead(currentThemeFilePath);
filePath = currentThemeFilePath;
}
if (retVal != null && filePath.EndsWith(".liquid"))
{
var context = WorkContext.Clone() as WorkContext;
context.Settings = GetSettings("''");
var templateContent = retVal.ReadToString();
retVal.Dispose();
var template = await RenderTemplateAsync(templateContent, filePath, context.ToScriptObject());
retVal = new MemoryStream(Encoding.UTF8.GetBytes(template));
}
if (retVal != null && (filePath.Contains(".scss.") && filePath.EndsWith(".liquid") || filePath.EndsWith(".scss")))
{
var content = retVal.ReadToString();
retVal.Dispose();
try
{
//handle scss resources
_sassFileManager.CurrentDirectory = Path.GetDirectoryName(filePath);
var result = SassCompiler.Compile(content);
content = result.CompiledContent;
retVal = new MemoryStream(Encoding.UTF8.GetBytes(content));
}
catch (Exception ex)
{
throw new SaasCompileException(filePath, content, ex);
}
}
return retVal;
}
/// <summary>
/// Return hash of requested asset (used for file versioning)
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string GetAssetHash(string filePath)
{
var cacheKey = CacheKey.With(GetType(), "GetAssetHash", filePath);
return _memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
{
cacheEntry.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(filePath), _themeBlobProvider.Watch(CurrentThemeSettingPath) }));
using (var stream = GetAssetStreamAsync(filePath).GetAwaiter().GetResult())
{
if (stream == null)
{
throw new StorefrontException($"Theme resource for path '{filePath}' not found");
}
var hashAlgorithm = CryptoConfig.AllowOnlyFipsAlgorithms ? (SHA256)new SHA256CryptoServiceProvider() : new SHA256Managed();
return WebEncoders.Base64UrlEncode(hashAlgorithm.ComputeHash(stream));
}
});
}
/// <summary>
/// resolve template path by it name
/// </summary>
/// <param name="templateName"></param>
/// <returns></returns>
public string ResolveTemplatePath(string templateName)
{
if (WorkContext.CurrentStore == null)
{
return null;
}
var liquidTemplateFileName = string.Format(_liquidTemplateFormat, templateName);
//If not found by current theme path try find them by base path if it is defined
var curentThemeDiscoveryPaths = _options.TemplatesDiscoveryFolders.Select(x => Path.Combine(CurrentThemePath, x, liquidTemplateFileName));
if (BaseThemePath != null)
{
curentThemeDiscoveryPaths = curentThemeDiscoveryPaths.Concat(_options.TemplatesDiscoveryFolders.Select(x => Path.Combine(BaseThemePath, x, liquidTemplateFileName)));
}
//Try to find template in current theme folder
return curentThemeDiscoveryPaths.FirstOrDefault(x => _themeBlobProvider.PathExists(x));
}
/// <summary>
/// Render template by name and with passed context (parameters)
/// </summary>
/// <param name="templateName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public async ValueTask<string> RenderTemplateByNameAsync(string templateName, object context)
{
if (string.IsNullOrEmpty(templateName))
{
throw new ArgumentNullException(nameof(templateName));
}
var templatePath = ResolveTemplatePath(templateName);
if (string.IsNullOrEmpty(templatePath))
{
throw new FileNotFoundException($"The template '{templateName}' was not found. The following locations were searched:<br/>{string.Join("<br/>", DiscoveryPaths)}");
}
var templateContent = ReadTemplateByPath(templatePath);
var retVal = await RenderTemplateAsync(templateContent, templatePath, context);
return retVal;
}
/// <summary>
/// Render template by content and parameters
/// </summary>
/// <param name="templateContent"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public ValueTask<string> RenderTemplateAsync(string templateContent, string templatePath, object context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (!(context is IScriptObject scriptObject))
{
throw new StorefrontException($"{ nameof(context) } must implement IScriptObject");
}
if (string.IsNullOrEmpty(templateContent))
{
return new ValueTask<string>(templateContent);
}
var isLiquidTemplate = _isLiquid.Match(templateContent);
if (!isLiquidTemplate.Success)
{
return new ValueTask<string>(templateContent);
}
//TODO: Handle _options.RethrowLiquidRenderErrors
var cacheKey = CacheKey.With(GetType(), "ParseTemplate", templatePath ?? templateContent);
var parsedTemplate = _memoryCache.GetOrCreate(cacheKey, (cacheItem) =>
{
if (!string.IsNullOrEmpty(templatePath))
{
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(templatePath) }));
}
else
{
cacheItem.AddExpirationToken(ThemeEngineCacheRegion.CreateChangeToken());
}
return Template.ParseLiquid(templateContent, templatePath);
});
if (parsedTemplate.HasErrors)
{
throw new InvalidOperationException(string.Join("\n", parsedTemplate.Messages));
}
var templateContext = new TemplateContext()
{
TemplateLoader = this,
EnableRelaxedMemberAccess = true,
NewLine = Environment.NewLine,
TemplateLoaderLexerOptions = new LexerOptions
{
Mode = ScriptMode.Liquid
}
};
templateContext.PushGlobal(scriptObject);
var result = parsedTemplate.Render(templateContext);
return new ValueTask<string>(result);
}
/// <summary>
/// Read shopify theme settings from 'config' folder
/// </summary>
/// <param name="defaultValue"></param>
/// <returns></returns>
public IDictionary<string, object> GetSettings(string defaultValue = null)
{
var cacheKey = CacheKey.With(GetType(), "GetSettings", CurrentThemeSettingPath, defaultValue);
return _memoryCache.GetOrCreateExclusive(cacheKey, cacheItem =>
{
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(CurrentThemeSettingPath) }));
JObject result;
var baseThemeSettings = new JObject();
var currentThemeSettings = result = GetCurrentSettingsPreset(InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath));
//Try to load settings from base theme path and merge them with resources for local theme
if ((_options.MergeBaseSettings || currentThemeSettings == null) && !string.IsNullOrEmpty(BaseThemeSettingPath))
{
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(BaseThemeSettingPath) }));
result = baseThemeSettings = GetCurrentSettingsPreset(InnerGetAllSettings(_themeBlobProvider, BaseThemeSettingPath));
}
if (_options.MergeBaseSettings)
{
result = baseThemeSettings;
result.Merge(currentThemeSettings ?? new JObject(), new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge });
}
return result.ToObject<Dictionary<string, object>>().ToDictionary(x => x.Key, x => x.Value).WithDefaultValue(defaultValue);
});
}
/// <summary>
/// Read localization resources
/// </summary>
/// <returns></returns>
public JObject ReadLocalization()
{
var cacheKey = CacheKey.With(GetType(), "ReadLocalization", CurrentThemeLocalePath, WorkContext.CurrentLanguage.CultureName);
return _memoryCache.GetOrCreateExclusive(cacheKey, (cacheItem) =>
{
var result = new JObject();
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(CurrentThemeLocalePath + "/*") }));
//Try to load localization resources from base theme path and merge them with resources for local theme
if (BaseThemeLocalePath != null)
{
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(BaseThemeLocalePath + "/*") }));
result = InnerReadLocalization(_themeBlobProvider, BaseThemeLocalePath, WorkContext.CurrentLanguage);
}
result.Merge(InnerReadLocalization(_themeBlobProvider, CurrentThemeLocalePath, WorkContext.CurrentLanguage) ?? new JObject(), new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge });
return result;
});
}
/// <summary>
/// Get relative url for assets (assets folder)
/// </summary>
/// <param name="assetName"></param>
/// <returns></returns>
public string GetAssetAbsoluteUrl(string assetName)
{
return UrlBuilder.ToAppAbsolute(_options.ThemesAssetsRelativeUrl.TrimEnd('/') + "/" + assetName.TrimStart('/'), WorkContext.CurrentStore, WorkContext.CurrentLanguage);
}
#endregion
private static JObject InnerReadLocalization(IContentBlobProvider themeBlobProvider, string localePath, Language language)
{
JObject retVal = null;
if (themeBlobProvider.PathExists(localePath))
{
JObject localeJson = null;
JObject defaultJson = null;
foreach (var languageName in new[] { language.CultureName, language.TwoLetterLanguageName })
{
var currentLocalePath = Path.Combine(localePath, string.Concat(languageName, ".json"));
if (themeBlobProvider.PathExists(currentLocalePath))
{
using (var stream = themeBlobProvider.OpenRead(currentLocalePath))
{
localeJson = JsonConvert.DeserializeObject<dynamic>(stream.ReadToString());
}
break;
}
}
var localeDefaultPath = themeBlobProvider.Search(localePath, "*.default.json", false).FirstOrDefault();
if (localeDefaultPath != null && themeBlobProvider.PathExists(localeDefaultPath))
{
using (var stream = themeBlobProvider.OpenRead(localeDefaultPath))
{
defaultJson = JsonConvert.DeserializeObject<dynamic>(stream.ReadToString());
}
}
//Need merge default and requested localization json to resulting object
retVal = defaultJson ?? localeJson;
if (defaultJson != null && localeJson != null)
{
retVal.Merge(localeJson, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge });
}
}
return retVal;
}
private static JObject InnerGetAllSettings(IContentBlobProvider themeBlobProvider, string settingsPath)
{
if (settingsPath == null)
{
throw new ArgumentNullException(nameof(settingsPath));
}
var result = new JObject();
if (themeBlobProvider.PathExists(settingsPath))
{
using (var stream = themeBlobProvider.OpenRead(settingsPath))
{
result = JsonConvert.DeserializeObject<JObject>(stream.ReadToString());
}
}
return result;
}
/// <summary>
/// Get actual preset from config
/// </summary>
/// <returns></returns>
private static JObject GetCurrentSettingsPreset(JObject allSettings)
{
var result = allSettings;
var currentPreset = allSettings.GetValue("current");
if (currentPreset is JValue currentPresetValue)
{
var currentPresetName = currentPresetValue.Value.ToString();
if (!(allSettings.GetValue("presets") is JObject presets) || !presets.Children().Any())
{
throw new StorefrontException("Setting presets not defined");
}
IList<JProperty> allPresets = presets.Children().Cast<JProperty>().ToList();
result = allPresets.FirstOrDefault(p => p.Name == currentPresetName)?.Value as JObject;
if (result == null)
{
throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found");
}
}
if (currentPreset is JObject preset)
{
result = preset;
}
return result;
}
private string ReadTemplateByPath(string templatePath)
{
if (string.IsNullOrEmpty(templatePath))
{
throw new ArgumentNullException(nameof(templatePath));
}
var cacheKey = CacheKey.With(GetType(), "ReadTemplateByName", templatePath);
return _memoryCache.GetOrCreateExclusive(cacheKey, (cacheItem) =>
{
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(templatePath) }));
using (var stream = _themeBlobProvider.OpenRead(templatePath))
{
return stream.ReadToString();
}
});
}
private string GetSettingsFilePath()
{
var prefix = _httpContextAccessor.HttpContext.Request.Query["preview_mode"];
return prefix.IsNullOrEmpty() ? "settings_data.json" : $"drafts\\{prefix}_settings_data.json";
}
}
}