2012-09-26 11:04:07 +07:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Web.Mvc ;
using System.Web.Routing ;
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// A custom area for controllers that are plugins
/// </summary>
internal class PluginControllerArea : AreaRegistration
{
2012-09-26 13:42:03 +07:00
private readonly IEnumerable < PluginControllerMetadata > _surfaceControllers ;
2012-09-26 11:04:07 +07:00
private readonly string _areaName ;
/// <summary>
/// The constructor accepts all types of plugin controllers and will verify that ALL of them have the same areaName assigned to them
/// based on their PluginControllerAttribute. If they are not the same an exception will be thrown.
/// </summary>
/// <param name="pluginControllers"></param>
2012-09-26 13:42:03 +07:00
public PluginControllerArea ( IEnumerable < PluginControllerMetadata > pluginControllers )
2012-09-26 11:04:07 +07:00
{
//TODO: When we have other future plugin controllers we need to combine them all into one list here to do our validation.
var controllers = pluginControllers . ToArray ( ) ;
2012-09-26 13:42:03 +07:00
if ( controllers . Any ( x = > x . AreaName . IsNullOrWhiteSpace ( ) ) )
2012-09-26 11:04:07 +07:00
{
throw new InvalidOperationException ( "Cannot create a PluginControllerArea unless all plugin controllers assigned have a PluginControllerAttribute assigned" ) ;
}
2012-09-26 13:42:03 +07:00
_areaName = controllers . First ( ) . AreaName ;
2012-09-26 11:04:07 +07:00
foreach ( var c in controllers )
{
2012-09-26 13:42:03 +07:00
if ( c . AreaName ! = _areaName )
2012-09-26 11:04:07 +07:00
{
2012-09-26 13:42:03 +07:00
throw new InvalidOperationException ( "Cannot create a PluginControllerArea unless all plugin controllers assigned have the same AreaName. The first AreaName found was " + _areaName + " however, the controller of type " + c . GetType ( ) . FullName + " has an AreaName of " + c . AreaName ) ;
2012-09-26 11:04:07 +07:00
}
}
//get the surface controllers
2012-09-26 13:42:03 +07:00
_surfaceControllers = controllers . Where ( x = > TypeHelper . IsTypeAssignableFrom < SurfaceController > ( x . ControllerType ) ) ;
2012-09-26 11:04:07 +07:00
}
public override void RegisterArea ( AreaRegistrationContext context )
{
MapRouteSurfaceControllers ( context . Routes , _surfaceControllers ) ;
}
public override string AreaName
{
get { return _areaName ; }
}
/// <summary>
/// Registers all surface controller routes
/// </summary>
/// <param name="routes"></param>
/// <param name="surfaceControllers"></param>
2012-10-01 23:43:47 +05:00
/// <remarks>
/// The routes will be:
///
/// /Umbraco/[AreaName]/[ControllerName]/[Action]/[Id]
/// </remarks>
2012-09-26 13:42:03 +07:00
private void MapRouteSurfaceControllers ( RouteCollection routes , IEnumerable < PluginControllerMetadata > surfaceControllers )
2012-09-26 11:04:07 +07:00
{
foreach ( var s in surfaceControllers )
{
2013-06-18 15:05:13 +10:00
this . RouteControllerPlugin ( s . ControllerName , s . ControllerType , routes , "" , "Index" , UrlParameter . Optional , "surface" ) ;
2012-09-26 11:04:07 +07:00
}
}
}
}