-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ConfigurationExtensions.cs
97 lines (87 loc) · 4.41 KB
/
ConfigurationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Microsoft.Extensions.Configuration;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
namespace Umbraco.Extensions;
/// <summary>
/// Extensions for <see cref="IConfiguration" />.
/// </summary>
public static class ConfigurationExtensions
{
/// <summary>
/// The postfix used to identify a connection string provider setting.
/// </summary>
internal const string ProviderNamePostfix = "_ProviderName";
/// <summary>
/// Gets the provider name for the connection string name (shorthand for <c>GetSection("ConnectionStrings")[name + "_ProviderName"]</c>).
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="name">The connection string key.</param>
/// <returns>
/// The provider name.
/// </returns>
/// <remarks>
/// This uses the same convention as the <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#connection-string-prefixes">Configuration API for connection string environment variables</a>.
/// </remarks>
public static string? GetConnectionStringProviderName(this IConfiguration configuration, string name)
=> configuration.GetConnectionString(name + ProviderNamePostfix);
/// <summary>
/// Gets the Umbraco connection string (shorthand for <c>GetSection("ConnectionStrings")[name]</c> and replacing the <c>|DataDirectory|</c> placeholder).
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="name">The connection string key.</param>
/// <returns>
/// The Umbraco connection string.
/// </returns>
public static string? GetUmbracoConnectionString(this IConfiguration configuration, string name = Constants.System.UmbracoConnectionName)
=> configuration.GetUmbracoConnectionString(name, out _);
/// <summary>
/// Gets the Umbraco connection string and provider name (shorthand for <c>GetSection("ConnectionStrings")[Constants.System.UmbracoConnectionName]</c> and replacing the <c>|DataDirectory|</c> placeholder).
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="providerName">The provider name.</param>
/// <returns>
/// The Umbraco connection string.
/// </returns>
public static string? GetUmbracoConnectionString(this IConfiguration configuration, out string? providerName)
=> configuration.GetUmbracoConnectionString(Constants.System.UmbracoConnectionName, out providerName);
/// <summary>
/// Gets the Umbraco connection string and provider name (shorthand for <c>GetSection("ConnectionStrings")[name]</c> and replacing the <c>|DataDirectory|</c> placeholder).
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="name">The name.</param>
/// <param name="providerName">The provider name.</param>
/// <returns>
/// The Umbraco connection string.
/// </returns>
public static string? GetUmbracoConnectionString(this IConfiguration configuration, string name, out string? providerName)
{
string? connectionString = configuration.GetConnectionString(name);
if (!string.IsNullOrEmpty(connectionString))
{
// Replace data directory
string? dataDirectory = AppDomain.CurrentDomain.GetData(Constants.System.DataDirectoryName)?.ToString();
if (!string.IsNullOrEmpty(dataDirectory))
{
connectionString = connectionString.Replace(Constants.System.DataDirectoryPlaceholder, dataDirectory);
}
// Get provider name
providerName = configuration.GetConnectionStringProviderName(name);
}
else
{
providerName = null;
}
return connectionString;
}
/// <summary>
/// Gets the Umbraco runtime mode.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>
/// The Umbraco runtime mode.
/// </returns>
public static RuntimeMode GetRuntimeMode(this IConfiguration configuration)
=> configuration.GetValue<RuntimeMode>(Constants.Configuration.ConfigRuntimeMode);
public static ModelsMode GetModelsMode(this IConfiguration configuration) => (configuration.GetSection(Constants.Configuration.ConfigModelsBuilder).Get<ModelsBuilderSettings>() ?? new ModelsBuilderSettings()).ModelsMode;
}