using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; using umbraco.interfaces; namespace Umbraco.Core { /// /// A resolver to return all IAction objects /// public sealed class ActionsResolver : LazyManyObjectsResolverBase { /// /// Constructor /// /// /// /// internal ActionsResolver(IServiceProvider serviceProvider, ILogger logger, Func> packageActions) : base(serviceProvider, logger, packageActions) { } /// /// Gets the implementations. /// public IEnumerable Actions { get { return Values; } } /// /// This method will return a list of IAction's based on a string (letter) list. Each character in the list may represent /// an IAction. This will associate any found IActions based on the Letter property of the IAction with the character being referenced. /// /// /// returns a list of actions that have an associated letter found in the action string list public IEnumerable FromActionSymbols(IEnumerable actions) { var allActions = Actions.ToArray(); return actions .Select(c => allActions.FirstOrDefault(a => a.Letter.ToString(CultureInfo.InvariantCulture) == c)) .WhereNotNull() .ToArray(); } /// /// Returns the string (letter) representation of the actions that make up the actions collection /// /// public IEnumerable ToActionSymbols(IEnumerable actions) { return actions.Select(x => x.Letter.ToString(CultureInfo.InvariantCulture)).ToArray(); } /// /// Gets an Action if it exists. /// /// /// internal IAction GetAction() where T : IAction { return Actions.SingleOrDefault(x => x.GetType() == typeof (T)); } protected override IEnumerable CreateInstances() { var actions = new List(); var foundIActions = InstanceTypes; foreach (var type in foundIActions) { IAction typeInstance; var instance = type.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static); //if the singletone initializer is not found, try simply creating an instance of the IAction if it supports public constructors if (instance == null) typeInstance = ServiceProvider.GetService(type) as IAction; else typeInstance = instance.GetValue(null, null) as IAction; if (typeInstance != null) { actions.Add(typeInstance); } } return actions; } } }