Move some of the config to Umbraco.Configuration

This commit is contained in:
Bjarke Berg
2019-11-07 10:10:12 +01:00
parent c6ca955d5a
commit c77d910e95
8 changed files with 56 additions and 73 deletions

View File

@@ -0,0 +1,28 @@
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Grid;
using Umbraco.Core.Configuration.HealthChecks;
using Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Core
{
/// <summary>
/// Provides extension methods for the <see cref="Configs"/> class.
/// </summary>
public static class ConfigsExtensions
{
public static IGlobalSettings Global(this Configs configs)
=> configs.GetConfig<IGlobalSettings>();
public static IUmbracoSettingsSection Settings(this Configs configs)
=> configs.GetConfig<IUmbracoSettingsSection>();
public static IHealthChecks HealthChecks(this Configs configs)
=> configs.GetConfig<IHealthChecks>();
public static IGridConfig Grids(this Configs configs)
=> configs.GetConfig<IGridConfig>();
public static CoreDebug CoreDebug(this Configs configs)
=> configs.GetConfig<CoreDebug>();
}
}

View File

@@ -0,0 +1,21 @@
using System.Configuration;
using Umbraco.Core.Configuration.HealthChecks;
using Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Core.Configuration
{
public class ConfigsFactory : IConfigsFactory
{
public Configs Create() {
var configs = new Configs(section => ConfigurationManager.GetSection(section));
configs.Add<IGlobalSettings>(() => new GlobalSettings());
configs.Add<IUmbracoSettingsSection>("umbracoConfiguration/settings");
configs.Add<IHealthChecks>("umbracoConfiguration/HealthChecks");
configs.Add(() => new CoreDebug());
configs.Add<IHealthChecks>("umbracoConfiguration/HealthChecks");
configs.AddCoreConfigs();
return configs;
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Configuration;
namespace Umbraco.Core.Configuration
{
public class CoreDebug
{
public CoreDebug()
{
var appSettings = ConfigurationManager.AppSettings;
LogUncompletedScopes = string.Equals("true", appSettings[Constants.AppSettings.Debug.LogUncompletedScopes], StringComparison.OrdinalIgnoreCase);
DumpOnTimeoutThreadAbort = string.Equals("true", appSettings[Constants.AppSettings.Debug.DumpOnTimeoutThreadAbort], StringComparison.OrdinalIgnoreCase);
}
// when true, Scope logs the stack trace for any scope that gets disposed without being completed.
// this helps troubleshooting rogue scopes that we forget to complete
public bool LogUncompletedScopes { get; }
// when true, the Logger creates a mini dump of w3wp in ~/App_Data/MiniDump whenever it logs
// an error due to a ThreadAbortException that is due to a timeout.
public bool DumpOnTimeoutThreadAbort { get; }
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Xml;
using System.Xml.Linq;
namespace Umbraco.Core.Configuration
{
/// <summary>
/// A full config section is required for any full element and we have some elements that are defined like this:
/// {element}MyValue{/element} instead of as attribute values.
/// </summary>
/// <typeparam name="T"></typeparam>
public class InnerTextConfigurationElement<T> : RawXmlConfigurationElement
{
public InnerTextConfigurationElement()
{
}
public InnerTextConfigurationElement(XElement rawXml) : base(rawXml)
{
}
protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
base.DeserializeElement(reader, serializeCollectionKey);
//now validate and set the raw value
if (RawXml.HasElements)
throw new InvalidOperationException("An InnerTextConfigurationElement cannot contain any child elements, only attributes and a value");
RawValue = RawXml.Value.Trim();
//RawValue = reader.ReadElementContentAsString();
}
public virtual T Value
{
get
{
var converted = RawValue.TryConvertTo<T>();
if (converted.Success == false)
throw new InvalidCastException("Could not convert value " + RawValue + " to type " + typeof(T));
return converted.Result;
}
}
/// <summary>
/// Exposes the raw string value
/// </summary>
internal string RawValue { get; set; }
/// <summary>
/// Implicit operator so we don't need to use the 'Value' property explicitly
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public static implicit operator T(InnerTextConfigurationElement<T> m)
{
return m.Value;
}
/// <summary>
/// Return the string value of Value
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("{0}", Value);
}
}
}

View File

@@ -0,0 +1,64 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.UmbracoSettings
{
public class ContentImagingElement : ConfigurationElement
{
[ConfigurationProperty("imageFileTypes")]
internal CommaDelimitedConfigurationElement ImageFileTypes =>
new OptionalCommaDelimitedConfigurationElement(
(CommaDelimitedConfigurationElement)this["imageFileTypes"],
//set the default
GetDefaultImageFileTypes());
public static string[] GetDefaultImageFileTypes()
{
return new[] {"jpeg", "jpg", "gif", "bmp", "png", "tiff", "tif"};
}
private ImagingAutoFillPropertiesCollection _defaultImageAutoFill;
[ConfigurationCollection(typeof(ImagingAutoFillPropertiesCollection), AddItemName = "uploadField")]
[ConfigurationProperty("autoFillImageProperties", IsDefaultCollection = true)]
internal ImagingAutoFillPropertiesCollection ImageAutoFillProperties
{
get
{
if (_defaultImageAutoFill != null)
{
return _defaultImageAutoFill;
}
//here we need to check if this element is defined, if it is not then we'll setup the defaults
var prop = Properties["autoFillImageProperties"];
var autoFill = this[prop] as ConfigurationElement;
if (autoFill != null && autoFill.ElementInformation.IsPresent == false)
{
_defaultImageAutoFill = new ImagingAutoFillPropertiesCollection
{
new ImagingAutoFillUploadFieldElement
{
Alias = "umbracoFile"
}
};
return _defaultImageAutoFill;
}
return (ImagingAutoFillPropertiesCollection) base["autoFillImageProperties"];
}
}
public static ImagingAutoFillPropertiesCollection GetDefaultImageAutoFillProperties()
{
return new ImagingAutoFillPropertiesCollection
{
new ImagingAutoFillUploadFieldElement
{
Alias = "umbracoFile"
}
};
}
}
}

View File

@@ -0,0 +1,96 @@
using System;
using System.Configuration;
using System.Globalization;
using System.Collections.Generic;
namespace Umbraco.Core.Configuration.UmbracoSettings
{
public class RequestHandlerElement : UmbracoConfigurationElement, IRequestHandlerSection
{
[ConfigurationProperty("addTrailingSlash")]
public InnerTextConfigurationElement<bool> AddTrailingSlash => GetOptionalTextElement("addTrailingSlash", true);
private UrlReplacingElement _defaultUrlReplacing;
[ConfigurationProperty("urlReplacing")]
public UrlReplacingElement UrlReplacing
{
get
{
if (_defaultUrlReplacing != null)
{
return _defaultUrlReplacing;
}
//here we need to check if this element is defined, if it is not then we'll setup the defaults
var prop = Properties["urlReplacing"];
var urls = this[prop] as ConfigurationElement;
if (urls != null && urls.ElementInformation.IsPresent == false)
{
_defaultUrlReplacing = new UrlReplacingElement()
{
CharCollection = GetDefaultCharReplacements()
};
return _defaultUrlReplacing;
}
return (UrlReplacingElement)this["urlReplacing"];
}
}
public static CharCollection GetDefaultCharReplacements()
{
var dictionary = new Dictionary<char, string>()
{
{' ',"-"},
{'\"',""},
{'\'',""},
{'%',""},
{'.',""},
{';',""},
{'/',""},
{'\\',""},
{':',""},
{'#',""},
{'+',"plus"},
{'*',"star"},
{'&',""},
{'?',""},
{'æ',"ae"},
{'ø',"oe"},
{'å',"aa"},
{'ä',"ae"},
{'ö',"oe"},
{'ü',"ue"},
{'ß',"ss"},
{'Ä',"ae"},
{'Ö',"oe"},
{'|',"-"},
{'<',""},
{'>',""}
};
//const string chars = @" ,"",',%,.,;,/,\,:,#,+,*,&,?,æ,ø,å,ä,ö,ü,ß,Ä,Ö,|,<,>";
var collection = new CharCollection();
foreach (var c in dictionary)
{
collection.Add(new CharElement
{
Char = c.Key.ToString(CultureInfo.InvariantCulture),
Replacement = c.Value.ToString(CultureInfo.InvariantCulture)
});
}
return collection;
}
bool IRequestHandlerSection.AddTrailingSlash => AddTrailingSlash;
bool IRequestHandlerSection.ConvertUrlsToAscii => UrlReplacing.ConvertUrlsToAscii.InvariantEquals("true");
bool IRequestHandlerSection.TryConvertUrlsToAscii => UrlReplacing.ConvertUrlsToAscii.InvariantEquals("try");
IEnumerable<IChar> IRequestHandlerSection.CharCollection => UrlReplacing.CharCollection;
}
}