2022-12-07 14:05:11 +01:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
namespace Finch.Modules;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
public class FinchModuleCollection : FinchModule
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
ConcurrentDictionary<Type, IFinchModule> _modules = new();
|
2022-12-07 14:05:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all registered modules
|
|
|
|
|
/// </summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
public IEnumerable<IFinchModule> GetAll() => _modules.Values;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
/// Adds a finch module
|
2022-12-07 14:05:11 +01:00
|
|
|
/// </summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
public void Add<T>() where T : class, IFinchModule, new()
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
Add(new T());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
/// Adds a finch module
|
2022-12-07 14:05:11 +01:00
|
|
|
/// </summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
public void Add<T>(T moduleInstance) where T : IFinchModule
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
Add(typeof(T), moduleInstance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
/// Adds a finch module
|
2022-12-07 14:05:11 +01:00
|
|
|
/// </summary>
|
2026-04-07 14:23:29 +02:00
|
|
|
public void Add(Type moduleType, IFinchModule moduleInstance)
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
if (_modules.ContainsKey(moduleType))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_modules.TryAdd(moduleType, moduleInstance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
foreach (var module in _modules.OrderBy(x => x.Value.Order))
|
|
|
|
|
{
|
|
|
|
|
module.Value.ConfigureServices(services, configuration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
foreach (var module in _modules.OrderBy(x => x.Value.ConfigureOrder))
|
|
|
|
|
{
|
|
|
|
|
module.Value.Configure(app, routes, serviceProvider);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|