Upgraded macr to use PluginTypeResolver to resolve XsltExtensions and added unit tests
to support finding these types.
This commit is contained in:
@@ -125,7 +125,7 @@ namespace Umbraco.Core
|
||||
return instances.FirstOrDefault();
|
||||
}
|
||||
|
||||
private IEnumerable<Type> ResolveTypes<T>(Func<IEnumerable<Type>> finder)
|
||||
private IEnumerable<Type> ResolveTypes<T>(Func<IEnumerable<Type>> finder, bool typeIsAttribute = false)
|
||||
{
|
||||
using (var readLock = new UpgradeableReadLock(_lock))
|
||||
{
|
||||
@@ -140,7 +140,7 @@ namespace Umbraco.Core
|
||||
//upgrade to a write lock since we're adding to the collection
|
||||
readLock.UpgradeToWriteLock();
|
||||
|
||||
typeList = new TypeList<T>();
|
||||
typeList = new TypeList<T>(typeIsAttribute);
|
||||
|
||||
foreach (var t in finder())
|
||||
{
|
||||
@@ -185,7 +185,9 @@ namespace Umbraco.Core
|
||||
internal IEnumerable<Type> ResolveAttributedTypes<TAttribute>()
|
||||
where TAttribute : Attribute
|
||||
{
|
||||
return ResolveTypes<object>(() => TypeFinder.FindClassesWithAttribute<TAttribute>(AssembliesToScan));
|
||||
return ResolveTypes<TAttribute>(
|
||||
() => TypeFinder.FindClassesWithAttribute<TAttribute>(AssembliesToScan),
|
||||
true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -209,11 +211,20 @@ namespace Umbraco.Core
|
||||
|
||||
internal class TypeList<T> : TypeList
|
||||
{
|
||||
private readonly bool _typeIsAttribute;
|
||||
|
||||
public TypeList(bool typeIsAttribute = false)
|
||||
{
|
||||
_typeIsAttribute = typeIsAttribute;
|
||||
}
|
||||
|
||||
private readonly List<Type> _types = new List<Type>();
|
||||
|
||||
public override void AddType(Type t)
|
||||
{
|
||||
if (t.IsType<T>())
|
||||
|
||||
//if the type is an attribute type we won't do the type check
|
||||
if (_typeIsAttribute || t.IsType<T>())
|
||||
{
|
||||
_types.Add(t);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using NUnit.Framework;
|
||||
using SqlCE4Umbraco;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Web;
|
||||
using umbraco;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.MacroEngines;
|
||||
using umbraco.MacroEngines.Iron;
|
||||
@@ -111,8 +112,21 @@ namespace Umbraco.Tests
|
||||
Assert.AreEqual(1, types.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Resolves_XsltExtensions()
|
||||
{
|
||||
var types = PluginTypeResolver.Current.ResolveXsltExtensions();
|
||||
Assert.AreEqual(1, types.Count());
|
||||
}
|
||||
|
||||
[XsltExtension("Blah.Blah")]
|
||||
public class MyXsltExtension
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[RestExtension("Blah")]
|
||||
public class MyTestExtension
|
||||
public class MyRestExtension
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Web.Routing;
|
||||
using umbraco;
|
||||
using umbraco.interfaces;
|
||||
using umbraco.presentation.umbracobase;
|
||||
|
||||
@@ -33,5 +34,15 @@ namespace Umbraco.Web
|
||||
{
|
||||
return resolver.ResolveAttributedTypes<RestExtension>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all classes attributed with XsltExtensionAttribute attribute
|
||||
/// </summary>
|
||||
/// <param name="resolver"></param>
|
||||
/// <returns></returns>
|
||||
internal static IEnumerable<Type> ResolveXsltExtensions(this PluginTypeResolver resolver)
|
||||
{
|
||||
return resolver.ResolveAttributedTypes<XsltExtensionAttribute>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.Xml;
|
||||
using System.Xml.Xsl;
|
||||
using Umbraco.Core;
|
||||
using umbraco.BusinessLogic;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
using umbraco.cms.businesslogic.macro;
|
||||
@@ -775,6 +776,10 @@ namespace umbraco
|
||||
// We could cache the extensions in a static variable but then the cache
|
||||
// would not be refreshed when the .config file is modified. An application
|
||||
// restart would be required. Better use the cache and add a dependency.
|
||||
|
||||
// SD: Not sure what is meant by the above statement? Having these in a static variable would be preferred!
|
||||
// If you modify a config file, the app restarts and thus all static variables are reset.
|
||||
// Having this stuff in cache just adds to the gigantic amount of cache data and will cause more cache turnover to happen.
|
||||
|
||||
return cms.businesslogic.cache.Cache.GetCacheItem(
|
||||
_xsltExtensionsCacheKey, _xsltExtensionsSyncLock,
|
||||
@@ -782,7 +787,7 @@ namespace umbraco
|
||||
null, // no refresh action
|
||||
_xsltExtensionsDependency.Value, // depends on the .config file
|
||||
TimeSpan.FromDays(1), // expires in 1 day (?)
|
||||
() => { return GetXsltExtensionsImpl(); });
|
||||
GetXsltExtensionsImpl);
|
||||
}
|
||||
|
||||
// zb-00041 #29966 : cache the extensions
|
||||
@@ -840,13 +845,16 @@ namespace umbraco
|
||||
//also get types marked with XsltExtension attribute
|
||||
|
||||
// zb-00042 #29949 : do not hide errors, refactor
|
||||
var typeFinder = new Umbraco.Core.TypeFinder2();
|
||||
foreach (var xsltType in typeFinder.FindClassesWithAttribute<XsltExtensionAttribute>())
|
||||
|
||||
var foundExtensions = Umbraco.Web.PluginTypeResolverExtensions.ResolveXsltExtensions(PluginTypeResolver.Current);
|
||||
foreach (var xsltType in foundExtensions)
|
||||
{
|
||||
object[] tpAttributes = xsltType.GetCustomAttributes(typeof(XsltExtensionAttribute), true);
|
||||
var tpAttributes = xsltType.GetCustomAttributes(typeof(XsltExtensionAttribute), true);
|
||||
foreach (XsltExtensionAttribute tpAttribute in tpAttributes)
|
||||
{
|
||||
string ns = !string.IsNullOrEmpty(tpAttribute.Namespace) ? tpAttribute.Namespace : xsltType.FullName;
|
||||
var ns = !string.IsNullOrEmpty(tpAttribute.Namespace)
|
||||
? tpAttribute.Namespace
|
||||
: xsltType.FullName;
|
||||
extensions.Add(ns, Activator.CreateInstance(xsltType));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user