Try your search with a different keyword or use * as a wildcard.
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Nop.Core.Infrastructure;
namespace Nop.Core.Configuration;
///
/// Represents the app settings helper
///
public partial class AppSettingsHelper
{
#region Fields
protected static Dictionary _configurationOrder;
#endregion
#region Methods
///
/// Create app settings with the passed configurations and save it to the file
///
/// Configurations to save
/// File provider
/// Whether to overwrite appsettings file
/// App settings
public static AppSettings SaveAppSettings(IList configurations, INopFileProvider fileProvider, bool overwrite = true)
{
ArgumentNullException.ThrowIfNull(configurations);
_configurationOrder ??= configurations.ToDictionary(config => config.Name, config => config.GetOrder());
//create app settings
var appSettings = Singleton.Instance ?? new AppSettings();
appSettings.Update(configurations);
Singleton.Instance = appSettings;
//create file if not exists
var filePath = fileProvider.MapPath(NopConfigurationDefaults.AppSettingsFilePath);
var fileExists = fileProvider.FileExists(filePath);
fileProvider.CreateFile(filePath);
//get raw configuration parameters
var configuration = JsonConvert.DeserializeObject(fileProvider.ReadAllText(filePath, Encoding.UTF8))
?.Configuration
?? new();
foreach (var config in configurations)
{
configuration[config.Name] = JToken.FromObject(config);
}
//sort configurations for display by order (e.g. data configuration with 0 will be the first)
appSettings.Configuration = configuration
.SelectMany(outConfig => _configurationOrder.Where(inConfig => inConfig.Key == outConfig.Key).DefaultIfEmpty(),
(outConfig, inConfig) => new { OutConfig = outConfig, InConfig = inConfig })
.OrderBy(config => config.InConfig.Value)
.Select(config => config.OutConfig)
.ToDictionary(config => config.Key, config => config.Value);
//save app settings to the file
if (!fileExists || overwrite)
{
var text = JsonConvert.SerializeObject(appSettings, Formatting.Indented);
fileProvider.WriteAllText(filePath, text, Encoding.UTF8);
}
return appSettings;
}
#endregion
}