Files
Umbraco-CMS/src/Umbraco.Core/Configuration/Grid/GridEditorsConfig.cs

73 lines
2.8 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using Umbraco.Core.Cache;
using Umbraco.Core.Hosting;
using Umbraco.Core.Logging;
using Umbraco.Core.Manifest;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Serialization;
namespace Umbraco.Core.Configuration.Grid
{
2017-09-15 18:22:19 +02:00
internal class GridEditorsConfig : IGridEditorsConfig
{
private readonly AppCaches _appCaches;
private readonly IHostingEnvironment _hostingEnvironment;
2019-11-06 13:35:34 +01:00
private readonly IManifestParser _manifestParser;
private readonly IJsonSerializer _jsonSerializer;
private readonly ILogger _logger;
public GridEditorsConfig(AppCaches appCaches, IHostingEnvironment hostingEnvironment, IManifestParser manifestParser,IJsonSerializer jsonSerializer, ILogger logger)
{
_appCaches = appCaches;
_hostingEnvironment = hostingEnvironment;
_manifestParser = manifestParser;
_jsonSerializer = jsonSerializer;
_logger = logger;
}
public IEnumerable<IGridEditorConfig> Editors
{
get
{
2019-11-07 12:58:56 +01:00
List<IGridEditorConfig> GetResult()
{
var configFolder = new DirectoryInfo(_hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.Config));
2019-11-07 12:58:56 +01:00
var editors = new List<IGridEditorConfig>();
var gridConfig = Path.Combine(configFolder.FullName, "grid.editors.config.js");
if (File.Exists(gridConfig))
{
var sourceString = File.ReadAllText(gridConfig);
try
{
editors.AddRange(_jsonSerializer.Deserialize<IEnumerable<GridEditor>>(sourceString));
}
catch (Exception ex)
{
_logger.Error<GridEditorsConfig>(ex, "Could not parse the contents of grid.editors.config.js into a JSON array '{Json}", sourceString);
}
}
2018-01-19 19:08:12 +01:00
// add manifest editors, skip duplicates
foreach (var gridEditor in _manifestParser.Manifest.GridEditors)
{
2019-01-07 10:43:28 +01:00
if (editors.Contains(gridEditor) == false) editors.Add(gridEditor);
}
2018-01-19 19:08:12 +01:00
return editors;
2019-01-07 10:43:28 +01:00
}
//cache the result if debugging is disabled
var result = _hostingEnvironment.IsDebugMode
2019-01-07 10:43:28 +01:00
? GetResult()
2019-11-07 12:58:56 +01:00
: _appCaches.RuntimeCache.GetCacheItem<List<IGridEditorConfig>>(typeof(GridEditorsConfig) + ".Editors",GetResult, TimeSpan.FromMinutes(10));
return result;
}
}
}
2017-07-20 11:21:28 +02:00
}