// Kyoo - A portable and vast media library solution. // Copyright (c) Kyoo. // // See AUTHORS.md and LICENSE file in the project root for full license information. // // Kyoo is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // Kyoo is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . using System; using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.DependencyInjection; namespace Kyoo.Abstractions.Controllers { /// /// A list of constant priorities used for 's . /// It also contains helper methods for creating new . /// [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name should match first type name", Justification = "StartupAction is nested and the name SA is short to improve readability in plugin's startup.")] public static class SA { /// /// The highest predefined priority existing for . /// public const int Before = 5000; /// /// Items defining routing (see IApplicationBuilder.UseRouting use this priority. /// public const int Routing = 4000; /// /// Actions defining new static files router use this priority. /// public const int StaticFiles = 3000; /// /// Actions calling IApplicationBuilder.UseAuthentication use this priority. /// public const int Authentication = 2000; /// /// Actions calling IApplicationBuilder.UseAuthorization use this priority. /// public const int Authorization = 1000; /// /// Action adding endpoint should use this priority (with a negative modificator if there is a catchall). /// public const int Endpoint = 0; /// /// The lowest predefined priority existing for . /// It should run after all other actions. /// public const int After = -1000; /// /// Create a new . /// /// The action to run /// The priority of the new action /// A new public static StartupAction New(Action action, int priority) => new(action, priority); /// /// Create a new . /// /// The action to run /// The priority of the new action /// A dependency that this action will use. /// A new public static StartupAction New(Action action, int priority) => new(action, priority); /// /// Create a new . /// /// The action to run /// The priority of the new action /// A dependency that this action will use. /// A second dependency that this action will use. /// A new public static StartupAction New(Action action, int priority) => new(action, priority); /// /// Create a new . /// /// The action to run /// The priority of the new action /// A dependency that this action will use. /// A second dependency that this action will use. /// A third dependency that this action will use. /// A new public static StartupAction New(Action action, int priority) => new(action, priority); /// /// A with no dependencies. /// public class StartupAction : IStartupAction { /// /// The action to execute at startup. /// private readonly Action _action; /// public int Priority { get; } /// /// Create a new . /// /// The action to execute on startup. /// The priority of this action (see ). public StartupAction(Action action, int priority) { _action = action; Priority = priority; } /// public void Run(IServiceProvider provider) { _action.Invoke(); } } /// /// A with one dependencies. /// /// The dependency to use. public class StartupAction : IStartupAction { /// /// The action to execute at startup. /// private readonly Action _action; /// public int Priority { get; } /// /// Create a new . /// /// The action to execute on startup. /// The priority of this action (see ). public StartupAction(Action action, int priority) { _action = action; Priority = priority; } /// public void Run(IServiceProvider provider) { _action.Invoke(provider.GetRequiredService()); } } /// /// A with two dependencies. /// /// The dependency to use. /// The second dependency to use. public class StartupAction : IStartupAction { /// /// The action to execute at startup. /// private readonly Action _action; /// public int Priority { get; } /// /// Create a new . /// /// The action to execute on startup. /// The priority of this action (see ). public StartupAction(Action action, int priority) { _action = action; Priority = priority; } /// public void Run(IServiceProvider provider) { _action.Invoke( provider.GetRequiredService(), provider.GetRequiredService() ); } } /// /// A with three dependencies. /// /// The dependency to use. /// The second dependency to use. /// The third dependency to use. public class StartupAction : IStartupAction { /// /// The action to execute at startup. /// private readonly Action _action; /// public int Priority { get; } /// /// Create a new . /// /// The action to execute on startup. /// The priority of this action (see ). public StartupAction(Action action, int priority) { _action = action; Priority = priority; } /// public void Run(IServiceProvider provider) { _action.Invoke( provider.GetRequiredService(), provider.GetRequiredService(), provider.GetRequiredService() ); } } } /// /// An action executed on kyoo's startup to initialize the asp-net container. /// /// /// This is the base interface, see for a simpler use of this. /// public interface IStartupAction { /// /// The priority of this action. The actions will be executed on descending priority order. /// If two actions have the same priority, their order is undefined. /// int Priority { get; } /// /// Run this action to configure the container, a service provider containing all services can be used. /// /// The service provider containing all services can be used. void Run(IServiceProvider provider); } }