Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Theme list - load only once to the memory of the application

KrzysztofPajakcd71bc820c57
diff --git a/src/Web/Grand.Web.Common/Startup/StartupApplication.cs b/src/Web/Grand.Web.Common/Startup/StartupApplication.cs
index f2c9d489a..a4a48f6a7 100644
--- a/src/Web/Grand.Web.Common/Startup/StartupApplication.cs
+++ b/src/Web/Grand.Web.Common/Startup/StartupApplication.cs
@@ -118,6 +118,8 @@ namespace Grand.Web.Common.Startup
{
serviceCollection.AddScoped<IPageHeadBuilder, PageHeadBuilder>();
+ serviceCollection.AddSingleton<IThemeList, ThemeList>();
+
serviceCollection.AddScoped<IThemeProvider, ThemeProvider>();
serviceCollection.AddScoped<IThemeContext, ThemeContext>();
diff --git a/src/Web/Grand.Web.Common/Themes/IThemeList.cs b/src/Web/Grand.Web.Common/Themes/IThemeList.cs
new file mode 100644
index 000000000..059e62b36
--- /dev/null
+++ b/src/Web/Grand.Web.Common/Themes/IThemeList.cs
@@ -0,0 +1,9 @@
+using System.Collections.Generic;
+
+namespace Grand.Web.Common.Themes
+{
+ public interface IThemeList
+ {
+ IList<ThemeConfiguration> ThemeConfigurations { get; }
+ }
+}
diff --git a/src/Web/Grand.Web.Common/Themes/IThemeProvider.cs b/src/Web/Grand.Web.Common/Themes/IThemeProvider.cs
index f4e3a1df3..29cbe60d5 100644
--- a/src/Web/Grand.Web.Common/Themes/IThemeProvider.cs
+++ b/src/Web/Grand.Web.Common/Themes/IThemeProvider.cs
@@ -7,8 +7,6 @@ namespace Grand.Web.Common.Themes
{
bool ThemeConfigurationExists(string themeName);
- ThemeConfiguration GetConfiguration(string themeName);
-
IList<ThemeConfiguration> GetConfigurations();
ThemeInfo GetThemeDescriptorFromText(string text);
diff --git a/src/Web/Grand.Web.Common/Themes/ThemeConfiguration.cs b/src/Web/Grand.Web.Common/Themes/ThemeConfiguration.cs
index c79a31bb9..dd701e1af 100644
--- a/src/Web/Grand.Web.Common/Themes/ThemeConfiguration.cs
+++ b/src/Web/Grand.Web.Common/Themes/ThemeConfiguration.cs
@@ -8,6 +8,7 @@
public string PreviewText { get; set; }
public bool SupportRtl { get; set; }
public string Version { get; set; }
+ public string Directory { get; set; }
}
}
diff --git a/src/Web/Grand.Web.Common/Themes/ThemeList.cs b/src/Web/Grand.Web.Common/Themes/ThemeList.cs
new file mode 100644
index 000000000..d67513c9b
--- /dev/null
+++ b/src/Web/Grand.Web.Common/Themes/ThemeList.cs
@@ -0,0 +1,43 @@
+using Grand.SharedKernel.Extensions;
+using Newtonsoft.Json;
+using System.Collections.Generic;
+using System.IO;
+
+namespace Grand.Web.Common.Themes
+{
+ public partial class ThemeList : IThemeList
+ {
+ public ThemeList()
+ {
+ ThemeConfigurations = new List<ThemeConfiguration>();
+ foreach (var themeName in Directory.GetDirectories(CommonPath.ThemePath))
+ {
+ var configuration = CreateThemeConfiguration(themeName);
+ if (configuration != null)
+ {
+ ThemeConfigurations.Add(configuration);
+ }
+ }
+ }
+
+ public IList<ThemeConfiguration> ThemeConfigurations { get; }
+
+ private ThemeConfiguration CreateThemeConfiguration(string themePath)
+ {
+ var themeDirectory = new DirectoryInfo(themePath);
+ var themeConfigFile = new FileInfo(Path.Combine(themeDirectory.FullName, "theme.cfg"));
+
+ if (themeConfigFile.Exists)
+ {
+ var themeConfiguration = JsonConvert.DeserializeObject<ThemeConfiguration>(File.ReadAllText(themeConfigFile.FullName));
+ if (themeConfiguration != null)
+ {
+ themeConfiguration.Name = themeDirectory.Name;
+ return themeConfiguration;
+ }
+ }
+ return null;
+ }
+
+ }
+}
diff --git a/src/Web/Grand.Web.Common/Themes/ThemeProvider.cs b/src/Web/Grand.Web.Common/Themes/ThemeProvider.cs
index 7dd2e287c..ae2edacf7 100644
--- a/src/Web/Grand.Web.Common/Themes/ThemeProvider.cs
+++ b/src/Web/Grand.Web.Common/Themes/ThemeProvider.cs
@@ -3,7 +3,6 @@ using Grand.SharedKernel.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
-using System.IO;
using System.Linq;
namespace Grand.Web.Common.Themes
@@ -12,50 +11,15 @@ namespace Grand.Web.Common.Themes
{
#region Fields
- private readonly IList<ThemeConfiguration> _themeConfigurations = new List<ThemeConfiguration>();
- private readonly string _basePath = string.Empty;
+ private readonly IThemeList _themeList;
#endregion
#region Constructors
- public ThemeProvider()
+ public ThemeProvider(IThemeList themeList)
{
- _basePath = CommonPath.ThemePath;
- LoadConfigurations();
- }
-
- #endregion
-
- #region Utility
-
- private void LoadConfigurations()
- {
- foreach (string themeName in Directory.GetDirectories(_basePath))
- {
- var configuration = CreateThemeConfiguration(themeName);
- if (configuration != null)
- {
- _themeConfigurations.Add(configuration);
- }
- }
- }
-
- private ThemeConfiguration CreateThemeConfiguration(string themePath)
- {
- var themeDirectory = new DirectoryInfo(themePath);
- var themeConfigFile = new FileInfo(Path.Combine(themeDirectory.FullName, "theme.cfg"));
-
- if (themeConfigFile.Exists)
- {
- var themeConfiguration = JsonConvert.DeserializeObject<ThemeConfiguration>(File.ReadAllText(themeConfigFile.FullName));
- if (themeConfiguration != null)
- {
- themeConfiguration.Name = themeDirectory.Name;
- return themeConfiguration;
- }
- }
- return null;
+ _themeList = themeList;
}
#endregion
@@ -67,14 +31,12 @@ namespace Grand.Web.Common.Themes
return GetConfigurations().Any(configuration => configuration.Name.Equals(themeName, StringComparison.OrdinalIgnoreCase));
}
- public ThemeConfiguration GetConfiguration(string themeName)
- {
- return _themeConfigurations.SingleOrDefault(x => x.Name.Equals(themeName, StringComparison.OrdinalIgnoreCase));
- }
-
public IList<ThemeConfiguration> GetConfigurations()
{
- return _themeConfigurations;
+ if(string.IsNullOrEmpty(CommonPath.Param))
+ return _themeList.ThemeConfigurations;
+
+ return _themeList.ThemeConfigurations.Where(x => string.IsNullOrEmpty(x.Directory) || x.Directory == CommonPath.Param).ToList();
}
public ThemeInfo GetThemeDescriptorFromText(string text)