|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.FileProviders; |
| 9 | +using Microsoft.Extensions.Primitives; |
| 10 | + |
| 11 | +// TODO: Microsft.Extensions.Configuration API Proposal |
| 12 | +namespace Microsoft.AspNetCore.Builder |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Configuration is mutable configuration object. It is both a configuration builder and an IConfigurationRoot. |
| 16 | + /// As sources are added, it updates its current view of configuration. Once Build is called, configuration is frozen. |
| 17 | + /// </summary> |
| 18 | + public sealed class Configuration : IConfigurationRoot, IConfigurationBuilder |
| 19 | + { |
| 20 | + private readonly ConfigurationBuilder _builder = new(); |
| 21 | + private IConfigurationRoot _configuration; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets or sets a configuration value. |
| 25 | + /// </summary> |
| 26 | + /// <param name="key">The configuration key.</param> |
| 27 | + /// <returns>The configuration value.</returns> |
| 28 | + public string this[string key] { get => _configuration[key]; set => _configuration[key] = value; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets a configuration sub-section with the specified key. |
| 32 | + /// </summary> |
| 33 | + /// <param name="key">The key of the configuration section.</param> |
| 34 | + /// <returns>The <see cref="IConfigurationSection"/>.</returns> |
| 35 | + /// <remarks> |
| 36 | + /// This method will never return <c>null</c>. If no matching sub-section is found with the specified key, |
| 37 | + /// an empty <see cref="IConfigurationSection"/> will be returned. |
| 38 | + /// </remarks> |
| 39 | + public IConfigurationSection GetSection(string key) |
| 40 | + { |
| 41 | + return _configuration.GetSection(key); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the immediate descendant configuration sub-sections. |
| 46 | + /// </summary> |
| 47 | + /// <returns>The configuration sub-sections.</returns> |
| 48 | + public IEnumerable<IConfigurationSection> GetChildren() => _configuration.GetChildren(); |
| 49 | + |
| 50 | + IDictionary<string, object> IConfigurationBuilder.Properties => _builder.Properties; |
| 51 | + |
| 52 | + // TODO: Handle modifications to Sources and keep the configuration root in sync |
| 53 | + IList<IConfigurationSource> IConfigurationBuilder.Sources => Sources; |
| 54 | + |
| 55 | + internal IList<IConfigurationSource> Sources { get; } |
| 56 | + |
| 57 | + IEnumerable<IConfigurationProvider> IConfigurationRoot.Providers => _configuration.Providers; |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Creates a new <see cref="Configuration"/>. |
| 61 | + /// </summary> |
| 62 | + public Configuration() |
| 63 | + { |
| 64 | + _configuration = _builder.Build(); |
| 65 | + |
| 66 | + var sources = new ConfigurationSources(_builder.Sources, UpdateConfigurationRoot); |
| 67 | + |
| 68 | + Sources = sources; |
| 69 | + } |
| 70 | + |
| 71 | + internal void ChangeBasePath(string path) |
| 72 | + { |
| 73 | + this.SetBasePath(path); |
| 74 | + UpdateConfigurationRoot(); |
| 75 | + } |
| 76 | + |
| 77 | + internal void ChangeFileProvider(IFileProvider fileProvider) |
| 78 | + { |
| 79 | + this.SetFileProvider(fileProvider); |
| 80 | + UpdateConfigurationRoot(); |
| 81 | + } |
| 82 | + |
| 83 | + private void UpdateConfigurationRoot() |
| 84 | + { |
| 85 | + var current = _configuration; |
| 86 | + if (current is IDisposable disposable) |
| 87 | + { |
| 88 | + disposable.Dispose(); |
| 89 | + } |
| 90 | + _configuration = _builder.Build(); |
| 91 | + } |
| 92 | + |
| 93 | + IConfigurationBuilder IConfigurationBuilder.Add(IConfigurationSource source) |
| 94 | + { |
| 95 | + Sources.Add(source); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + IConfigurationRoot IConfigurationBuilder.Build() |
| 100 | + { |
| 101 | + // No more modification is expected after this final build |
| 102 | + UpdateConfigurationRoot(); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + IChangeToken IConfiguration.GetReloadToken() |
| 107 | + { |
| 108 | + // REVIEW: Is this correct? |
| 109 | + return _configuration.GetReloadToken(); |
| 110 | + } |
| 111 | + |
| 112 | + void IConfigurationRoot.Reload() |
| 113 | + { |
| 114 | + _configuration.Reload(); |
| 115 | + } |
| 116 | + |
| 117 | + // On source modifications, we rebuild configuration |
| 118 | + private class ConfigurationSources : IList<IConfigurationSource> |
| 119 | + { |
| 120 | + private readonly IList<IConfigurationSource> _sources; |
| 121 | + private readonly Action _sourcesModified; |
| 122 | + |
| 123 | + public ConfigurationSources(IList<IConfigurationSource> sources, Action sourcesModified) |
| 124 | + { |
| 125 | + _sources = sources; |
| 126 | + _sourcesModified = sourcesModified; |
| 127 | + } |
| 128 | + |
| 129 | + public IConfigurationSource this[int index] |
| 130 | + { |
| 131 | + get => _sources[index]; |
| 132 | + set |
| 133 | + { |
| 134 | + _sources[index] = value; |
| 135 | + _sourcesModified(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public int Count => _sources.Count; |
| 140 | + |
| 141 | + public bool IsReadOnly => _sources.IsReadOnly; |
| 142 | + |
| 143 | + public void Add(IConfigurationSource item) |
| 144 | + { |
| 145 | + _sources.Add(item); |
| 146 | + _sourcesModified(); |
| 147 | + } |
| 148 | + |
| 149 | + public void Clear() |
| 150 | + { |
| 151 | + _sources.Clear(); |
| 152 | + _sourcesModified(); |
| 153 | + } |
| 154 | + |
| 155 | + public bool Contains(IConfigurationSource item) |
| 156 | + { |
| 157 | + return _sources.Contains(item); |
| 158 | + } |
| 159 | + |
| 160 | + public void CopyTo(IConfigurationSource[] array, int arrayIndex) |
| 161 | + { |
| 162 | + _sources.CopyTo(array, arrayIndex); |
| 163 | + } |
| 164 | + |
| 165 | + public IEnumerator<IConfigurationSource> GetEnumerator() |
| 166 | + { |
| 167 | + return _sources.GetEnumerator(); |
| 168 | + } |
| 169 | + |
| 170 | + public int IndexOf(IConfigurationSource item) |
| 171 | + { |
| 172 | + return _sources.IndexOf(item); |
| 173 | + } |
| 174 | + |
| 175 | + public void Insert(int index, IConfigurationSource item) |
| 176 | + { |
| 177 | + _sources.Insert(index, item); |
| 178 | + _sourcesModified(); |
| 179 | + } |
| 180 | + |
| 181 | + public bool Remove(IConfigurationSource item) |
| 182 | + { |
| 183 | + var removed = _sources.Remove(item); |
| 184 | + _sourcesModified(); |
| 185 | + return removed; |
| 186 | + } |
| 187 | + |
| 188 | + public void RemoveAt(int index) |
| 189 | + { |
| 190 | + _sources.RemoveAt(index); |
| 191 | + _sourcesModified(); |
| 192 | + } |
| 193 | + |
| 194 | + IEnumerator IEnumerable.GetEnumerator() |
| 195 | + { |
| 196 | + return GetEnumerator(); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments