diff --git a/back/src/Kyoo.Host/PluginsStartup.cs b/back/src/Kyoo.Host/PluginsStartup.cs index 07fa1621..ddd6a57b 100644 --- a/back/src/Kyoo.Host/PluginsStartup.cs +++ b/back/src/Kyoo.Host/PluginsStartup.cs @@ -136,42 +136,12 @@ public class PluginsStartup /// A simple host service provider used to activate plugins instance. /// The same services as a generic host are available and an has been added. /// - private class HostServiceProvider : IServiceProvider + private class HostServiceProvider( + IWebHostEnvironment hostEnvironment, + IConfiguration configuration, + ILoggerFactory loggerFactory + ) : IServiceProvider { - /// - /// The host environment that could be used by plugins to configure themself. - /// - private readonly IWebHostEnvironment _hostEnvironment; - - /// - /// The configuration context. - /// - private readonly IConfiguration _configuration; - - /// - /// A logger factory used to create a logger for the plugin manager. - /// - private readonly ILoggerFactory _loggerFactory; - - /// - /// Create a new that will return given services when asked. - /// - /// - /// The host environment that could be used by plugins to configure themself. - /// - /// The configuration context - /// A logger factory used to create a logger for the plugin manager. - public HostServiceProvider( - IWebHostEnvironment hostEnvironment, - IConfiguration configuration, - ILoggerFactory loggerFactory - ) - { - _hostEnvironment = hostEnvironment; - _configuration = configuration; - _loggerFactory = loggerFactory; - } - /// public object GetService(Type serviceType) { @@ -179,20 +149,45 @@ public class PluginsStartup serviceType == typeof(IWebHostEnvironment) || serviceType == typeof(IHostEnvironment) ) - return _hostEnvironment; + return hostEnvironment; if (serviceType == typeof(IConfiguration)) - return _configuration; - if (serviceType.GetGenericTypeDefinition() == typeof(ILogger<>)) + return configuration; + if (serviceType == typeof(IServiceProviderIsService)) + return new ProviderIsService(); + if ( + serviceType.IsGenericType + && serviceType.GetGenericTypeDefinition() == typeof(ILogger<>) + ) { return Utility.RunGenericMethod( typeof(LoggerFactoryExtensions), nameof(LoggerFactoryExtensions.CreateLogger), serviceType.GetGenericArguments().First(), - _loggerFactory + loggerFactory ); } - return null; + throw new ArgumentException( + $"{serviceType.Name} is not available in configuration stpe" + ); + } + + public class ProviderIsService : IServiceProviderIsService + { + public bool IsService(Type serviceType) + { + Type[] supported = + [ + typeof(IWebHostEnvironment), + typeof(IHostEnvironment), + typeof(IConfiguration), + typeof(IServiceProviderIsService), + ]; + if (supported.Contains(serviceType)) + return true; + return serviceType.IsGenericType + && serviceType.GetGenericTypeDefinition() == typeof(ILogger<>); + } } } }