Files
Umbraco-CMS/src/Umbraco.Web/Mvc/PluginController.cs

119 lines
4.2 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Collections.Concurrent;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Security;
using Umbraco.Web.WebApi;
using Current = Umbraco.Web.Composing.Current;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Web.Mvc
{
/// <summary>
/// Provides a base class for plugin controllers.
/// </summary>
public abstract class PluginController : Controller, IDiscoverable
{
private static readonly ConcurrentDictionary<Type, PluginControllerMetadata> MetadataStorage
= new ConcurrentDictionary<Type, PluginControllerMetadata>();
// for debugging purposes
internal Guid InstanceId { get; } = Guid.NewGuid();
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets the Umbraco context.
2018-06-29 19:52:40 +02:00
/// </summary>
2020-02-09 18:53:37 +01:00
public virtual IUmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext;
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets the database context accessor.
/// </summary>
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; }
/// <summary>
/// Gets the database context.
2018-06-29 19:52:40 +02:00
/// </summary>
2018-07-06 17:36:33 +02:00
public IUmbracoDatabaseFactory DatabaseFactory { get; }
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets or sets the services context.
/// </summary>
2018-07-06 17:36:33 +02:00
public ServiceContext Services { get; }
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets or sets the application cache.
/// </summary>
2019-01-18 15:05:20 +01:00
public AppCaches AppCaches { get; }
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets or sets the logger.
/// </summary>
2018-07-06 17:36:33 +02:00
public ILogger Logger { get; }
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets or sets the profiling logger.
/// </summary>
2018-11-27 13:46:43 +01:00
public IProfilingLogger ProfilingLogger { get; }
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets the Umbraco helper.
/// </summary>
public UmbracoHelper Umbraco { get; }
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets metadata for this instance.
/// </summary>
internal PluginControllerMetadata Metadata => GetMetadata(GetType());
protected PluginController()
: this(
Current.Factory.GetInstance<IUmbracoContextAccessor>(),
2018-11-28 11:05:41 +01:00
Current.Factory.GetInstance<IUmbracoDatabaseFactory>(),
Current.Factory.GetInstance<ServiceContext>(),
2019-01-17 08:34:29 +01:00
Current.Factory.GetInstance<AppCaches>(),
2018-11-28 11:05:41 +01:00
Current.Factory.GetInstance<ILogger>(),
Current.Factory.GetInstance<IProfilingLogger>()
)
{
}
2018-07-06 17:36:33 +02:00
protected PluginController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, ILogger logger, IProfilingLogger profilingLogger)
2018-07-06 17:36:33 +02:00
{
UmbracoContextAccessor = umbracoContextAccessor;
2018-07-06 17:36:33 +02:00
DatabaseFactory = databaseFactory;
Services = services;
2019-01-18 15:05:20 +01:00
AppCaches = appCaches;
2018-07-06 17:36:33 +02:00
Logger = logger;
ProfilingLogger = profilingLogger;
}
2018-06-29 19:52:40 +02:00
/// <summary>
/// Gets metadata for a controller type.
/// </summary>
/// <param name="controllerType">The controller type.</param>
/// <returns>Metadata for the controller type.</returns>
internal static PluginControllerMetadata GetMetadata(Type controllerType)
{
return MetadataStorage.GetOrAdd(controllerType, type =>
{
// plugin controller? back-office controller?
var pluginAttribute = controllerType.GetCustomAttribute<PluginControllerAttribute>(false);
var backOfficeAttribute = controllerType.GetCustomAttribute<IsBackOfficeAttribute>(true);
return new PluginControllerMetadata
{
AreaName = pluginAttribute?.AreaName,
ControllerName = ControllerExtensions.GetControllerName(controllerType),
ControllerNamespace = controllerType.Namespace,
ControllerType = controllerType,
IsBackOffice = backOfficeAttribute != null
};
});
}
}
}