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