Merge branch 'v9/dev' into v9/contrib

This commit is contained in:
Paul Johnson
2021-10-18 08:31:20 +01:00
762 changed files with 1808 additions and 1893 deletions

View File

@@ -3,15 +3,15 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
@@ -165,7 +165,15 @@ namespace Umbraco.Extensions
return ContentStatus.Unpublished;
}
/// <summary>
/// Gets a collection containing the ids of all ancestors.
/// </summary>
/// <param name="content"><see cref="IContent"/> to retrieve ancestors for</param>
/// <returns>An Enumerable list of integer ids</returns>
public static IEnumerable<int> GetAncestorIds(this IContent content) =>
content.Path.Split(Constants.CharArrays.Comma)
.Where(x => x != Constants.System.RootString && x != content.Id.ToString(CultureInfo.InvariantCulture)).Select(s =>
int.Parse(s, CultureInfo.InvariantCulture));
#endregion

View File

@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Core.Extensions
{
public static class UmbracoBuilderExtensions
{
/// <summary>
/// Registers all <see cref="INotificationHandler{TNotification}"/> within an assembly
/// </summary>
/// <param name="self"><see cref="IUmbracoBuilder"/></param>
/// <typeparam name="T">Type contained within the targeted assembly</typeparam>
/// <returns></returns>
public static IUmbracoBuilder AddNotificationsFromAssembly<T>(this IUmbracoBuilder self)
{
AddNotificationHandlers<T>(self);
AddAsyncNotificationHandlers<T>(self);
return self;
}
private static void AddNotificationHandlers<T>(IUmbracoBuilder self)
{
var notificationHandlers = GetNotificationHandlers<T>();
foreach (var notificationHandler in notificationHandlers)
{
var handlerImplementations = GetNotificationHandlerImplementations<T>(notificationHandler);
foreach (var implementation in handlerImplementations)
{
RegisterNotificationHandler(self, implementation, notificationHandler);
}
}
}
private static List<Type> GetNotificationHandlers<T>() =>
typeof(T).Assembly.GetTypes()
.Where(x => x.IsAssignableToGenericType(typeof(INotificationHandler<>)))
.ToList();
private static List<Type> GetNotificationHandlerImplementations<T>(Type handlerType) =>
handlerType
.GetInterfaces()
.Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(INotificationHandler<>))
.ToList();
private static void AddAsyncNotificationHandlers<T>(IUmbracoBuilder self)
{
var notificationHandlers = GetAsyncNotificationHandlers<T>();
foreach (var notificationHandler in notificationHandlers)
{
var handlerImplementations = GetAsyncNotificationHandlerImplementations<T>(notificationHandler);
foreach (var handler in handlerImplementations)
{
RegisterNotificationHandler(self, handler, notificationHandler);
}
}
}
private static List<Type> GetAsyncNotificationHandlers<T>() =>
typeof(T).Assembly.GetTypes()
.Where(x => x.IsAssignableToGenericType(typeof(INotificationAsyncHandler<>)))
.ToList();
private static List<Type> GetAsyncNotificationHandlerImplementations<T>(Type handlerType) =>
handlerType
.GetInterfaces()
.Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(INotificationAsyncHandler<>))
.ToList();
private static void RegisterNotificationHandler(IUmbracoBuilder self, Type notificationHandlerType, Type implementingHandlerType)
{
var descriptor = new UniqueServiceDescriptor(notificationHandlerType, implementingHandlerType, ServiceLifetime.Transient);
if (!self.Services.Contains(descriptor))
{
self.Services.Add(descriptor);
}
}
private static bool IsAssignableToGenericType(this Type givenType, Type genericType)
{
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
{
if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
{
return true;
}
}
if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
{
return true;
}
var baseType = givenType.BaseType;
return baseType != null && IsAssignableToGenericType(baseType, genericType);
}
}
}