Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

ThemeProvider.cs

using System.Text;
using Newtonsoft.Json;
using Nop.Core.Infrastructure;
using Nop.Services.Plugins;

namespace Nop.Services.Themes;

/// 
/// Represents a default theme provider implementation
/// 
public partial class ThemeProvider : IThemeProvider
{
    #region Fields

    protected static readonly object _locker = new();

    protected readonly INopFileProvider _fileProvider;

    protected Dictionary _themeDescriptors;

    #endregion

    #region Ctor

    public ThemeProvider(INopFileProvider fileProvider)
    {
        _fileProvider = fileProvider;
        Initialize();
    }

    #endregion

    #region Utilities

    /// 
    /// Initializes theme provider
    /// 
    protected virtual void Initialize()
    {
        if (_themeDescriptors != null)
            return;

        //prevent multi loading data
        lock (_locker)
        {
            //data can be loaded while we waited
            if (_themeDescriptors != null)
                return;

            //load all theme descriptors
            _themeDescriptors =
                new Dictionary(StringComparer.InvariantCultureIgnoreCase);

            var themeDirectoryPath = _fileProvider.MapPath(NopPluginDefaults.ThemesPath);
            foreach (var descriptionFile in _fileProvider.GetFiles(themeDirectoryPath,
                         NopPluginDefaults.ThemeDescriptionFileName, false))
            {
                var text = _fileProvider.ReadAllText(descriptionFile, Encoding.UTF8);
                if (string.IsNullOrEmpty(text))
                    continue;

                //get theme descriptor
                var themeDescriptor = GetThemeDescriptorFromText(text);

                //some validation
                if (string.IsNullOrEmpty(themeDescriptor?.SystemName))
                    throw new Exception($"A theme descriptor '{descriptionFile}' has no system name");

                _themeDescriptors.TryAdd(themeDescriptor.SystemName, themeDescriptor);
            }
        }
    }

    #endregion

    #region Methods

    /// 
    /// Get theme descriptor from the description text
    /// 
    /// Description text
    /// Theme descriptor
    public ThemeDescriptor GetThemeDescriptorFromText(string text)
    {
        //get theme description from the JSON file
        var themeDescriptor = JsonConvert.DeserializeObject(text);

        //some validation
        if (_themeDescriptors.ContainsKey(themeDescriptor.SystemName))
            throw new Exception($"A theme with '{themeDescriptor.SystemName}' system name is already defined");

        return themeDescriptor;
    }

    /// 
    /// Get all themes
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the list of the theme descriptor
    /// 
    public Task> GetThemesAsync()
    {
        return Task.FromResult>(_themeDescriptors.Values.ToList());
    }

    /// 
    /// Get a theme by the system name
    /// 
    /// Theme system name
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the me descriptor
    /// 
    public Task GetThemeBySystemNameAsync(string systemName)
    {
        if (string.IsNullOrEmpty(systemName))
            return Task.FromResult(null);

        _themeDescriptors.TryGetValue(systemName, out var descriptor);

        return Task.FromResult(descriptor);
    }

    /// 
    /// Check whether the theme with specified system name exists
    /// 
    /// Theme system name
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains true if the theme exists; otherwise false
    /// 
    public Task ThemeExistsAsync(string systemName)
    {
        if (string.IsNullOrEmpty(systemName))
            return Task.FromResult(false);

        return Task.FromResult(_themeDescriptors.ContainsKey(systemName));
    }

    #endregion
}