Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vnext dev refresh #319

Merged
merged 9 commits into from
Dec 13, 2016
13 changes: 12 additions & 1 deletion Composite/AspNet/Razor/RazorFunction.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using Composite.Core.Xml;
using Composite.Functions;
using Composite.Plugins.Functions.FunctionProviders.FileBasedFunctionProvider;

namespace Composite.AspNet.Razor
{
/// <summary>
/// Base class for c1 functions based on razor
/// </summary>
public abstract class RazorFunction : CompositeC1WebPage
public abstract class RazorFunction : CompositeC1WebPage, IParameterWidgetsProvider

{
/// <summary>
/// Gets the function description. Override this to make a custom description.
Expand Down Expand Up @@ -35,5 +39,12 @@ public virtual Type FunctionReturnType
{
get { return typeof (XhtmlDocument); }
}

/// <exclude />
public virtual IDictionary<string, WidgetFunctionProvider> GetParameterWidgets()
{
return null;
}

}
}
14 changes: 12 additions & 2 deletions Composite/AspNet/UserControlFunction.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI;
using Composite.Functions;
using Composite.Plugins.Functions.FunctionProviders.FileBasedFunctionProvider;

namespace Composite.AspNet
{
/// <summary>
/// Base class for a UserControls that represents a C1 function
/// </summary>
public abstract class UserControlFunction : UserControl
public abstract class UserControlFunction : UserControl, IParameterWidgetsProvider

{
/// <summary>
/// Gets the function description.
Expand All @@ -24,5 +27,12 @@ public FunctionContextContainer FunctionContextContainer
get;
set;
}

/// <exclude />
public virtual IDictionary<string, WidgetFunctionProvider> GetParameterWidgets()
{
return null;
}

}
}
1 change: 1 addition & 0 deletions Composite/Composite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
<Compile Include="Plugins\Forms\WebChannel\UiControlFactories\TemplatedSvgIconSelectorUiControlFactory.cs">
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Plugins\Functions\FunctionProviders\FileBasedFunctionProvider\IParameterWidgetsProvider.cs" />
<Compile Include="Plugins\Functions\WidgetFunctionProviders\StandardWidgetFunctionProvider\Utils\ConsoleIconSelectorWidgetFuntion.cs" />
<Compile Include="Plugins\Functions\WidgetFunctionProviders\StandardWidgetFunctionProvider\Utils\SvgIconSelectorWidgetFuntion.cs" />
<Compile Include="Plugins\Logging\LogTraceListeners\FileLogTraceListener\CircullarList.cs" />
Expand Down
30 changes: 19 additions & 11 deletions Composite/Core/ServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ namespace Composite.Core
public static class ServiceLocator
{
private const string HttpContextKey = "HttpApplication.ServiceScope";
private static IServiceCollection _serviceCollection;
private static IServiceCollection _serviceCollection = new ServiceCollection();
private static IServiceProvider _serviceProvider = null;
private static ConcurrentDictionary<Type, bool> _hasTypeLookup = new ConcurrentDictionary<Type, bool>();
private static Func<IServiceCollection, IServiceProvider> _serviceProviderBuilder = s => s.BuildServiceProvider();

/// <summary>
/// Get service of type T
Expand Down Expand Up @@ -78,7 +79,7 @@ public static object GetRequiredService(Type serviceType)
return ServiceProvider.GetRequiredService(serviceType);
}


/// <summary>
/// Get services of the specified type
/// </summary>
Expand All @@ -90,7 +91,18 @@ public static IEnumerable<object> GetServices(Type serviceType)
}


/// <summary>
/// Replaces the default services container by registering a builder for your own IServiceProvider.
/// You must register this during the service configuration phase, see <see cref="Composite.Core.Application.ApplicationStartupAttribute"/> and the ConfigureServices method.
/// </summary>
/// <param name="serviceProviderBuilder">A callback function that returns your custom IServiceProvider</param>
public static void SetServiceProvider(Func<IServiceCollection, IServiceProvider> serviceProviderBuilder)
{
Verify.ArgumentNotNull(serviceProviderBuilder, nameof(serviceProviderBuilder));
Verify.IsNull(_serviceProvider, "ServiceProvider already created and cannot be replaced at this time. Your custom ServiceProvider must be set during application startup - see ApplicationStartupAttribute and ConfigureServices().");

_serviceProviderBuilder = serviceProviderBuilder;
}

/// <summary>
/// Gets an application service provider
Expand All @@ -106,7 +118,7 @@ internal static IServiceProvider ServiceProvider
}



internal static bool HasService(Type serviceType)
{
Verify.ArgumentNotNull(serviceType, nameof(serviceType));
Expand All @@ -116,9 +128,9 @@ internal static bool HasService(Type serviceType)

if (!_hasTypeLookup.TryGetValue(serviceType, out hasType))
{
if (_serviceCollection.Any(sd =>
if (_serviceCollection.Any(sd =>
sd.ServiceType.IsAssignableFrom(serviceType)
|| (serviceType.IsGenericType
|| (serviceType.IsGenericType
&& sd.ServiceType.IsAssignableFrom(serviceType.GetGenericTypeDefinition()))))
{
hasType = true;
Expand Down Expand Up @@ -151,18 +163,14 @@ internal static IServiceCollection ServiceCollection
{
Verify.IsNull(_serviceProvider, "ServiceCollection accessed after ServiceProvider build-up - call out of sequence.");

if (_serviceCollection == null)
{
_serviceCollection = new ServiceCollection();
}
return _serviceCollection;
}
}
}


internal static void BuildServiceProvider()
{
_serviceProvider = _serviceCollection.BuildServiceProvider();
_serviceProvider = _serviceProviderBuilder(_serviceCollection);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public virtual IEnumerable<ParameterProfile> ParameterProfiles
foreach (var param in Parameters.Values)
{
BaseValueProvider defaultValueProvider = new NoValueValueProvider();
WidgetFunctionProvider widgetProvider = null;
WidgetFunctionProvider widgetProvider = param.WidgetProvider;
string label = param.Name;
bool isRequired = true;
string helpText = String.Empty;
Expand All @@ -82,8 +82,6 @@ public virtual IEnumerable<ParameterProfile> ParameterProfiles
defaultValueProvider = new ConstantValueProvider(param.Attribute.DefaultValue);
}

widgetProvider = param.WidgetProvider;

hideInSimpleView = param.Attribute.HideInSimpleView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Composite.Plugins.Functions.FunctionProviders.FileBasedFunctionProvide
/// </summary>
public static class FunctionBasedFunctionProviderHelper
{
private static readonly string LogTitle = typeof (FunctionBasedFunctionProviderHelper).FullName;
private static readonly string LogTitle = typeof(FunctionBasedFunctionProviderHelper).FullName;

/// <summary>
/// Gets the function description from the <see cref="FunctionAttribute" />.
Expand Down Expand Up @@ -44,7 +44,8 @@ public static string GetDescription(string functionName, object functionObject)
/// <returns></returns>
public static IDictionary<string, FunctionParameter> GetParameters(object functionObject, Type baseFunctionType, string filePath)
{
var dict = new Dictionary<string, FunctionParameter>();
var functionParameters = new Dictionary<string, FunctionParameter>();
IDictionary<string, WidgetFunctionProvider> parameterWidgets = GetParameterWidgets(functionObject);

var type = functionObject.GetType();
while (type != baseFunctionType && type != null)
Expand All @@ -59,15 +60,13 @@ public static IDictionary<string, FunctionParameter> GetParameters(object functi
// Skipping explicitly ignored attributes
if (property.GetCustomAttributes(typeof(FunctionParameterIgnoreAttribute), false).Any()) continue;


var propType = property.PropertyType;
var name = property.Name;

FunctionParameterAttribute attr = null;
var attributes = property.GetCustomAttributes(typeof(FunctionParameterAttribute), false).Cast<FunctionParameterAttribute>().ToList();


if(attributes.Count > 1)
if (attributes.Count > 1)
{
Log.LogWarning(LogTitle, "More than one '{0}' attribute defined on property '{1}'. Location: '{2}'"
.FormatWith(typeof(FunctionParameterAttribute).Name, name, filePath));
Expand All @@ -77,13 +76,14 @@ public static IDictionary<string, FunctionParameter> GetParameters(object functi
attr = attributes.FirstOrDefault();
}

WidgetFunctionProvider widgetProvider = null;
WidgetFunctionProvider attibuteBasedWidgetProvider = null;
WidgetFunctionProvider methodBasedWidgetProvider = null;

if (attr != null && attr.HasWidgetMarkup)
{
try
{
widgetProvider = attr.GetWidgetFunctionProvider(type, property);
attibuteBasedWidgetProvider = attr.GetWidgetFunctionProvider(type, property);
}
catch (Exception ex)
{
Expand All @@ -93,16 +93,32 @@ public static IDictionary<string, FunctionParameter> GetParameters(object functi
}
}

if (!dict.ContainsKey(name))
parameterWidgets.TryGetValue(name, out methodBasedWidgetProvider);

if (methodBasedWidgetProvider!=null && attibuteBasedWidgetProvider!=null)
{
Log.LogWarning(LogTitle, "Widget for property {0} is defined in both {1} attribute and in {2}() method. Remove one of the definitions. Location: '{3}'"
.FormatWith(property.Name, nameof(FunctionParameterAttribute), nameof(IParameterWidgetsProvider.GetParameterWidgets), filePath));
}

if (!functionParameters.ContainsKey(name))
{
dict.Add(name, new FunctionParameter(name, propType, attr, widgetProvider));
functionParameters.Add(name, new FunctionParameter(name, propType, attr, attibuteBasedWidgetProvider ?? methodBasedWidgetProvider));
}
}

type = type.BaseType;
}

return dict;
return functionParameters;
}

private static IDictionary<string, WidgetFunctionProvider> GetParameterWidgets(object functionObject)
{
var widgetsProvider = functionObject as IParameterWidgetsProvider;
IDictionary<string, WidgetFunctionProvider> parameterWidgets = widgetsProvider != null ? widgetsProvider.GetParameterWidgets() : null;

return parameterWidgets ?? new Dictionary<string, WidgetFunctionProvider>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using Composite.Functions;

namespace Composite.Plugins.Functions.FunctionProviders.FileBasedFunctionProvider
{
/// <exclude />
public interface IParameterWidgetsProvider
{
/// <exclude />
IDictionary<string, WidgetFunctionProvider> GetParameterWidgets();
}
}