Skip to content

Commit f9f0b2f

Browse files
committed
Merge branch 'issue-8104-refactoring-of-ThemeableViewLocationExpander' into develop
2 parents 16a4767 + e81ed84 commit f9f0b2f

File tree

13 files changed

+122
-52
lines changed

13 files changed

+122
-52
lines changed

src/Libraries/Nop.Services/Plugins/NopPluginDefaults.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ public static partial class NopPluginDefaults
6767
/// </summary>
6868
public static string UploadedItemsFileName => "uploadedItems.json";
6969

70-
/// <summary>
71-
/// Gets the path to themes folder
72-
/// </summary>
73-
public static string ThemesPath => "~/Themes";
74-
75-
/// <summary>
76-
/// Gets the name of the theme description file
77-
/// </summary>
78-
public static string ThemeDescriptionFileName => "theme.json";
79-
8070
/// <summary>
8171
/// Gets a key for caching plugins for admin navigation
8272
/// </summary>

src/Libraries/Nop.Services/Plugins/UploadService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ protected virtual async Task<IDescriptor> UploadSingleItemAsync(string archivePa
8383

8484
//get path to the themes directory
8585
var themesDirectory = string.Empty;
86-
if (!string.IsNullOrEmpty(NopPluginDefaults.ThemesPath))
87-
themesDirectory = _fileProvider.MapPath(NopPluginDefaults.ThemesPath);
86+
if (!string.IsNullOrEmpty(NopThemeDefaults.ThemesPath))
87+
themesDirectory = _fileProvider.MapPath(NopThemeDefaults.ThemesPath);
8888

8989
IDescriptor descriptor = null;
9090
string uploadedItemDirectoryName;
@@ -112,7 +112,7 @@ protected virtual async Task<IDescriptor> UploadSingleItemAsync(string archivePa
112112

113113
//or whether it's a theme descriptor
114114
var isThemeDescriptor = entry.FullName
115-
.Equals($"{uploadedItemDirectoryName}/{NopPluginDefaults.ThemeDescriptionFileName}", StringComparison.InvariantCultureIgnoreCase);
115+
.Equals($"{uploadedItemDirectoryName}/{NopThemeDefaults.ThemeDescriptionFileName}", StringComparison.InvariantCultureIgnoreCase);
116116

117117
if (!isPluginDescriptor && !isThemeDescriptor)
118118
continue;
@@ -180,8 +180,8 @@ protected virtual async Task<IList<IDescriptor>> UploadMultipleItemsAsync(string
180180

181181
//get path to the themes directory
182182
var themesDirectory = string.Empty;
183-
if (!string.IsNullOrEmpty(NopPluginDefaults.ThemesPath))
184-
themesDirectory = _fileProvider.MapPath(NopPluginDefaults.ThemesPath);
183+
if (!string.IsNullOrEmpty(NopThemeDefaults.ThemesPath))
184+
themesDirectory = _fileProvider.MapPath(NopThemeDefaults.ThemesPath);
185185

186186
//get descriptors of items contained in the archive
187187
var descriptors = new List<IDescriptor>();
@@ -204,8 +204,8 @@ protected virtual async Task<IList<IDescriptor>> UploadMultipleItemsAsync(string
204204
if (item.Type == UploadedItemType.Plugin)
205205
descriptorPath = $"{itemPath}{NopPluginDefaults.DescriptionFileName}";
206206

207-
if (item.Type == UploadedItemType.Theme && !string.IsNullOrEmpty(NopPluginDefaults.ThemeDescriptionFileName))
208-
descriptorPath = $"{itemPath}{NopPluginDefaults.ThemeDescriptionFileName}";
207+
if (item.Type == UploadedItemType.Theme && !string.IsNullOrEmpty(NopThemeDefaults.ThemeDescriptionFileName))
208+
descriptorPath = $"{itemPath}{NopThemeDefaults.ThemeDescriptionFileName}";
209209

210210
//try to get the descriptor entry
211211
var descriptorEntry = archive.Entries.FirstOrDefault(entry => entry.FullName.Equals(descriptorPath, StringComparison.InvariantCultureIgnoreCase));

src/Presentation/Nop.Web.Framework/Themes/IThemeContext.cs renamed to src/Libraries/Nop.Services/Themes/IThemeContext.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
namespace Nop.Web.Framework.Themes;
2-
3-
/// <summary>
4-
/// Represents a theme context
5-
/// </summary>
6-
public partial interface IThemeContext
7-
{
8-
/// <summary>
9-
/// Get current theme system name
10-
/// </summary>
11-
/// <returns>A task that represents the asynchronous operation</returns>
12-
Task<string> GetWorkingThemeNameAsync();
13-
14-
/// <summary>
15-
/// Set current theme system name
16-
/// </summary>
17-
/// <returns>A task that represents the asynchronous operation</returns>
18-
Task SetWorkingThemeNameAsync(string workingThemeName);
1+
namespace Nop.Services.Themes;
2+
3+
/// <summary>
4+
/// Represents a theme context
5+
/// </summary>
6+
public partial interface IThemeContext
7+
{
8+
/// <summary>
9+
/// Get current theme system name
10+
/// </summary>
11+
/// <returns>A task that represents the asynchronous operation</returns>
12+
Task<string> GetWorkingThemeNameAsync();
13+
14+
/// <summary>
15+
/// Set current theme system name
16+
/// </summary>
17+
/// <returns>A task that represents the asynchronous operation</returns>
18+
Task SetWorkingThemeNameAsync(string workingThemeName);
1919
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Nop.Services.Themes;
2+
3+
/// <summary>
4+
/// Represents default values related to themes
5+
/// </summary>
6+
public static partial class NopThemeDefaults
7+
{
8+
/// <summary>
9+
/// Gets the path to themes folder
10+
/// </summary>
11+
public static string ThemesPath => "~/Themes";
12+
13+
/// <summary>
14+
/// Gets the name of the theme description file
15+
/// </summary>
16+
public static string ThemeDescriptionFileName => "theme.json";
17+
18+
/// <summary>
19+
/// Gets the theme cache key used to store the theme name in the HTTP context items collection
20+
/// </summary>
21+
public static string HttpContextThemeCacheKey => "http-context-theme-cache-key";
22+
23+
/// <summary>
24+
/// Gets the key for store the name of the theme in the <see cref="Microsoft.AspNetCore.Mvc.Razor.ViewLocationExpanderContext"/>
25+
/// </summary>
26+
public static string ThemeKey => "nop.themename";
27+
}

src/Libraries/Nop.Services/Themes/ThemeProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public virtual async Task InitializeAsync()
3131
//load all theme descriptors
3232
_themeDescriptors = new Dictionary<string, ThemeDescriptor>(StringComparer.InvariantCultureIgnoreCase);
3333

34-
var themeDirectoryPath = fileProvider.MapPath(NopPluginDefaults.ThemesPath);
35-
foreach (var descriptionFile in fileProvider.GetFiles(themeDirectoryPath, NopPluginDefaults.ThemeDescriptionFileName, false))
34+
var themeDirectoryPath = fileProvider.MapPath(NopThemeDefaults.ThemesPath);
35+
foreach (var descriptionFile in fileProvider.GetFiles(themeDirectoryPath, NopThemeDefaults.ThemeDescriptionFileName, false))
3636
{
3737
var text = await fileProvider.ReadAllTextAsync(descriptionFile, Encoding.UTF8);
3838
if (string.IsNullOrEmpty(text))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Nop.Data;
3+
4+
namespace Nop.Services.Themes;
5+
6+
/// <summary>
7+
/// Represents middleware that enables themes
8+
/// </summary>
9+
public partial class ThemesMiddleware
10+
{
11+
#region Fields
12+
protected readonly RequestDelegate _next;
13+
14+
#endregion
15+
16+
#region Ctor
17+
18+
public ThemesMiddleware(RequestDelegate next)
19+
{
20+
_next = next;
21+
}
22+
23+
#endregion
24+
25+
#region Methods
26+
27+
/// <summary>
28+
/// Invoke middleware actions
29+
/// </summary>
30+
/// <param name="context">HTTP context</param>
31+
/// <param name="themeContext">The theme context</param>
32+
/// <returns>A task that represents the asynchronous operation</returns>
33+
public virtual async Task InvokeAsync(HttpContext context, IThemeContext themeContext)
34+
{
35+
if (DataSettingsManager.IsDatabaseInstalled() && !context.Items.TryGetValue(NopThemeDefaults.HttpContextThemeCacheKey, out var cachedThemeName))
36+
{
37+
cachedThemeName = await themeContext.GetWorkingThemeNameAsync();
38+
context.Items[NopThemeDefaults.HttpContextThemeCacheKey] = cachedThemeName;
39+
}
40+
41+
await _next(context);
42+
}
43+
44+
#endregion
45+
}

src/Presentation/Nop.Web.Framework/Factories/WidgetModelFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using Nop.Core.Caching;
44
using Nop.Services.Cms;
55
using Nop.Services.Customers;
6+
using Nop.Services.Themes;
67
using Nop.Web.Framework.Models.Cms;
7-
using Nop.Web.Framework.Themes;
88

99
namespace Nop.Web.Framework.Factories;
1010

src/Presentation/Nop.Web.Framework/Infrastructure/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using Nop.Services.Media;
3131
using Nop.Services.Security;
3232
using Nop.Services.Seo;
33+
using Nop.Services.Themes;
3334
using Nop.Web.Framework.Globalization;
3435
using Nop.Web.Framework.Mvc.Routing;
3536
using Nop.Web.Framework.WebOptimizer;
@@ -372,6 +373,15 @@ public static void UseKeepAlive(this IApplicationBuilder application)
372373
application.UseMiddleware<KeepAliveMiddleware>();
373374
}
374375

376+
/// <summary>
377+
/// Configure middleware storing the current user theme in the context
378+
/// </summary>
379+
/// <param name="application">Builder for configuring an application's request pipeline</param>
380+
public static void UseThemes(this IApplicationBuilder application)
381+
{
382+
application.UseMiddleware<ThemesMiddleware>();
383+
}
384+
375385
/// <summary>
376386
/// Configure middleware checking whether database is installed
377387
/// </summary>

src/Presentation/Nop.Web.Framework/Infrastructure/NopCommonStartup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public virtual void Configure(IApplicationBuilder application)
6565

6666
//configure PDF
6767
application.UseNopPdf();
68+
69+
//configure Themes
70+
application.UseThemes();
6871
}
6972

7073
/// <summary>

src/Presentation/Nop.Web.Framework/Themes/ThemeableViewLocationExpander.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Microsoft.AspNetCore.Mvc.Razor;
2-
using Nop.Core.Infrastructure;
2+
using Nop.Services.Themes;
33

44
namespace Nop.Web.Framework.Themes;
55

@@ -8,9 +8,6 @@ namespace Nop.Web.Framework.Themes;
88
/// </summary>
99
public partial class ThemeableViewLocationExpander : IViewLocationExpander
1010
{
11-
protected const string THEME_KEY = "nop.themename";
12-
protected const string HTTP_CONTEXT_THEME_CACHE_KEY = "http-context-theme-cache-key";
13-
1411
/// <summary>
1512
/// Invoked by a Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine to determine the
1613
/// values that would be consumed by this instance of Microsoft.AspNetCore.Mvc.Razor.IViewLocationExpander.
@@ -23,14 +20,10 @@ public virtual void PopulateValues(ViewLocationExpanderContext context)
2320
if (context.AreaName?.Equals(AreaNames.ADMIN) ?? false)
2421
return;
2522

26-
var httpContext = context.ActionContext.HttpContext;
27-
if (!httpContext.Items.TryGetValue(HTTP_CONTEXT_THEME_CACHE_KEY, out var cachedThemeName))
28-
{
29-
cachedThemeName = EngineContext.Current.Resolve<IThemeContext>().GetWorkingThemeNameAsync().Result;
30-
httpContext.Items[HTTP_CONTEXT_THEME_CACHE_KEY] = cachedThemeName;
31-
}
23+
if (!context.ActionContext.HttpContext.Items.TryGetValue(NopThemeDefaults.HttpContextThemeCacheKey, out var cachedThemeName))
24+
return;
3225

33-
context.Values[THEME_KEY] = (string)cachedThemeName;
26+
context.Values[NopThemeDefaults.ThemeKey] = (string)cachedThemeName;
3427
}
3528

3629
/// <summary>
@@ -41,7 +34,7 @@ public virtual void PopulateValues(ViewLocationExpanderContext context)
4134
/// <returns>View locations</returns>
4235
public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
4336
{
44-
if (context.Values.TryGetValue(THEME_KEY, out string theme))
37+
if (context.Values.TryGetValue(NopThemeDefaults.ThemeKey, out var theme))
4538
{
4639
viewLocations = new[] {
4740
$"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",

0 commit comments

Comments
 (0)