mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding OrchardDependencyReplace attribute
--HG-- branch : dev
This commit is contained in:
@@ -7,6 +7,7 @@ using Autofac;
|
||||
using Autofac.Core;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.Configuration;
|
||||
@@ -294,5 +295,32 @@ namespace Orchard.Tests.Environment {
|
||||
Assert.That(bar.TableName, Is.EqualTo("Yadda_Bar_BarRecord"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FeatureReplacement() {
|
||||
var descriptor = Build.ShellDescriptor().WithFeatures("Bar");
|
||||
|
||||
_extensionDescriptors = new[] {
|
||||
Build.ExtensionDescriptor("Foo").WithFeatures("Bar"),
|
||||
};
|
||||
|
||||
_featureTypes["Bar"] = new[] { typeof(ReplacedStubType), typeof(StubType), typeof(ReplacedStubNestedType), typeof(StubNestedType) };
|
||||
|
||||
var compositionStrategy = _container.Resolve<ICompositionStrategy>();
|
||||
var blueprint = compositionStrategy.Compose(BuildDefaultSettings(), descriptor);
|
||||
|
||||
Assert.That(blueprint.Dependencies.Count(), Is.EqualTo(2));
|
||||
Assert.That(blueprint.Dependencies.FirstOrDefault(dependency => dependency.Type.Equals(typeof(StubType))), Is.Not.Null);
|
||||
Assert.That(blueprint.Dependencies.FirstOrDefault(dependency => dependency.Type.Equals(typeof(StubNestedType))), Is.Not.Null);
|
||||
}
|
||||
|
||||
[OrchardDependencyReplace("Orchard.Tests.Environment.DefaultCompositionStrategyTests+ReplacedStubNestedType")]
|
||||
internal class StubNestedType : IDependency {}
|
||||
|
||||
internal class ReplacedStubNestedType : IDependency {}
|
||||
}
|
||||
|
||||
[OrchardDependencyReplace("Orchard.Tests.Environment.ReplacedStubType")]
|
||||
internal class StubType : IDependency { }
|
||||
|
||||
internal class ReplacedStubType : IDependency { }
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Orchard.Environment.Extensions {
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
|
||||
public class OrchardDependencyReplaceAttribute : Attribute {
|
||||
public OrchardDependencyReplaceAttribute(string dependency) {
|
||||
Dependency = dependency;
|
||||
}
|
||||
|
||||
public string Dependency { get; set; }
|
||||
}
|
||||
}
|
@@ -72,9 +72,22 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
IEnumerable<Feature> features,
|
||||
Func<Type, bool> predicate,
|
||||
Func<Type, Feature, T> selector) {
|
||||
HashSet<string> excludedTypes = new HashSet<string>();
|
||||
|
||||
// Identify replaced types
|
||||
foreach(Feature feature in features) {
|
||||
foreach (Type type in feature.ExportedTypes) {
|
||||
foreach (OrchardDependencyReplaceAttribute replacedType in type.GetCustomAttributes(typeof(OrchardDependencyReplaceAttribute), false)) {
|
||||
excludedTypes.Add(replacedType.Dependency);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load types excluding the replaced types
|
||||
return features.SelectMany(
|
||||
feature => feature.ExportedTypes
|
||||
.Where(predicate)
|
||||
.Where(type => !excludedTypes.Contains(type.FullName))
|
||||
.Select(type => selector(type, feature)))
|
||||
.ToArray();
|
||||
}
|
||||
|
Reference in New Issue
Block a user