| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LocalizationServiceResolver.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Threading; |
| | | 13 | | using MyNet.Globalization.Localization.Providers.Registration; |
| | | 14 | | |
| | | 15 | | namespace MyNet.Globalization.Localization.Providers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Resolves localization providers based on their type and culture, with caching and cycle detection to prevent circula |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="registry">The localization provider registry.</param> |
| | | 21 | | public sealed class LocalizationServiceResolver(ILocalizationFactoryRegistry registry) : ILocalizationServiceResolver |
| | | 22 | | { |
| | 6 | 23 | | private static readonly AsyncLocal<(Type Type, string CultureName)[]?> ResolutionPath = new(); |
| | 354 | 24 | | private readonly ConcurrentDictionary<(Type, string), Lazy<object?>> _cache = new(); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public bool TryGet<TService>(CultureInfo culture, [NotNullWhen(true)] out TService? provider) |
| | | 28 | | where TService : class, ICultureScoped |
| | | 29 | | { |
| | 6798 | 30 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 31 | | |
| | 6798 | 32 | | var cacheKey = (typeof(TService), culture.Name); |
| | | 33 | | |
| | 6798 | 34 | | if (IsCycle(cacheKey)) |
| | 3 | 35 | | throw CreateCycleException(cacheKey); |
| | | 36 | | |
| | 6795 | 37 | | var lazyProvider = _cache.GetOrAdd(cacheKey, |
| | 6795 | 38 | | _ => new(() => ResolveWithCycleCheck<TService>(culture), LazyThreadSafetyMode.ExecutionAndPublication)); |
| | | 39 | | |
| | 6795 | 40 | | provider = lazyProvider.Value as TService; |
| | 6789 | 41 | | return lazyProvider.Value is not null; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public TService GetRequired<TService>(CultureInfo culture) |
| | | 46 | | where TService : class, ICultureScoped |
| | 6729 | 47 | | => !TryGet<TService>(culture, out var provider) |
| | 6729 | 48 | | ? throw new InvalidOperationException($"Required localization provider of type {typeof(TService).Name} for c |
| | 6729 | 49 | | : provider; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Creates an exception indicating a circular dependency in localization provider resolution, including the resolut |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="requested">The requested provider and culture that caused the cycle.</param> |
| | | 55 | | /// <returns>An <see cref="InvalidOperationException"/> describing the circular dependency.</returns> |
| | | 56 | | private static InvalidOperationException CreateCycleException((Type Type, string CultureName) requested) |
| | | 57 | | { |
| | 3 | 58 | | var path = ResolutionPath.Value ?? []; |
| | 3 | 59 | | var chain = string.Join(" -> ", path.Select(x => $"{x.Type.Name}({x.CultureName})")); |
| | 3 | 60 | | var requestedNode = $"{requested.Type.Name}({requested.CultureName})"; |
| | 3 | 61 | | var message = string.IsNullOrWhiteSpace(chain) |
| | 3 | 62 | | ? $"Circular localization provider dependency detected for {requestedNode}." |
| | 3 | 63 | | : $"Circular localization provider dependency detected: {chain} -> {requestedNode}."; |
| | | 64 | | |
| | 3 | 65 | | return new(message); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Checks if the given provider and culture combination is already in the current resolution path, indicating a cir |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="key">The provider and culture combination to check.</param> |
| | | 72 | | /// <returns>True if a circular dependency is detected; otherwise, false.</returns> |
| | | 73 | | private static bool IsCycle((Type Type, string CultureName) key) |
| | | 74 | | { |
| | 6798 | 75 | | var path = ResolutionPath.Value; |
| | 6798 | 76 | | return path?.Contains(key) == true; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | private TService? ResolveWithCycleCheck<TService>(CultureInfo culture) |
| | | 80 | | where TService : class, ICultureScoped |
| | | 81 | | { |
| | 126 | 82 | | var key = (typeof(TService), culture.Name); |
| | 126 | 83 | | var path = ResolutionPath.Value ?? []; |
| | | 84 | | |
| | 126 | 85 | | if (path.Contains(key)) |
| | 0 | 86 | | throw CreateCycleException(key); |
| | | 87 | | |
| | 126 | 88 | | ResolutionPath.Value = [.. path, key]; |
| | | 89 | | |
| | | 90 | | try |
| | | 91 | | { |
| | 126 | 92 | | return Resolve<TService>(culture); |
| | | 93 | | } |
| | | 94 | | finally |
| | | 95 | | { |
| | 126 | 96 | | ResolutionPath.Value = path.Length == 0 ? null : path; |
| | 126 | 97 | | } |
| | 120 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Resolves the provider of type <typeparamref name="TService"/> for the given culture by querying the registry. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <typeparam name="TService">The type of the provider to resolve.</typeparam> |
| | | 104 | | /// <param name="culture">The culture for which to resolve the provider.</param> |
| | | 105 | | /// <returns>The resolved provider instance, or null if not found.</returns> |
| | | 106 | | private TService? Resolve<TService>(CultureInfo culture) |
| | | 107 | | where TService : class, ICultureScoped |
| | 126 | 108 | | => !registry.TryGetFactory<TService>(out var factory) ? null : factory.Create(culture); |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | public ILocalizationServiceContext ForCulture(CultureInfo culture) |
| | | 112 | | { |
| | 6711 | 113 | | ArgumentNullException.ThrowIfNull(culture); |
| | 6711 | 114 | | return new LocalizationServiceContext(this, culture); |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |