Merge pull request #9996 from umbraco/netcore/notifications/packaging-service

Events to Notifications Migration: Packaging Service
This commit is contained in:
Bjarke Berg
2021-03-18 10:32:40 +01:00
committed by GitHub
29 changed files with 124 additions and 145 deletions

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class CancelableEnumerableObjectNotification<T> : CancelableObjectNotification<IEnumerable<T>>
{

View File

@@ -1,9 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class CancelableObjectNotification<T> : ObjectNotification<T>, ICancelableNotification where T : class
{

View File

@@ -1,9 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class CopiedNotification<T> : ObjectNotification<T> where T : class
{

View File

@@ -1,9 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class CopyingNotification<T> : CancelableObjectNotification<T> where T : class
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class DeletedNotification<T> : EnumerableObjectNotification<T>
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class EnumerableObjectNotification<T> : ObjectNotification<IEnumerable<T>>
{

View File

@@ -55,6 +55,30 @@ namespace Umbraco.Cms.Core.Events
PublishNotification(notification);
Task.WaitAll(PublishNotificationAsync(notification));
}
public bool PublishCancelable<TCancelableNotification>(TCancelableNotification notification)
where TCancelableNotification : ICancelableNotification
{
if (notification == null)
{
throw new ArgumentNullException(nameof(notification));
}
Publish(notification);
return notification.Cancel;
}
public async Task<bool> PublishCancelableAsync<TCancelableNotification>(TCancelableNotification notification)
where TCancelableNotification : ICancelableNotification
{
if (notification == null)
{
throw new ArgumentNullException(nameof(notification));
}
await PublishAsync(notification);
return notification.Cancel;
}
}
/// <summary>

View File

@@ -29,5 +29,23 @@ namespace Umbraco.Cms.Core.Events
/// <param name="notification">The notification object.</param>
void Publish<TNotification>(TNotification notification)
where TNotification : INotification;
/// <summary>
/// Publishes a cancelable notification to the notification subscribers
/// </summary>
/// <typeparam name="TNotification">The type of notification being handled.</typeparam>
/// <param name="notification"></param>
/// <returns>True if the notification was cancelled by a subscriber, false otherwise</returns>
bool PublishCancelable<TCancelableNotification>(TCancelableNotification notification)
where TCancelableNotification : ICancelableNotification;
/// <summary>
/// Publishes a cancelable notification async to the notification subscribers
/// </summary>
/// <typeparam name="TNotification">The type of notification being handled.</typeparam>
/// <param name="notification"></param>
/// <returns>True if the notification was cancelled by a subscriber, false otherwise</returns>
Task<bool> PublishCancelableAsync<TCancelableNotification>(TCancelableNotification notification)
where TCancelableNotification : ICancelableNotification;
}
}

View File

@@ -1,59 +0,0 @@
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models.Packaging;
namespace Umbraco.Cms.Core.Events
{
public class ImportPackageEventArgs<TEntity> : CancellableEnumerableObjectEventArgs<TEntity>, IEquatable<ImportPackageEventArgs<TEntity>>
{
public ImportPackageEventArgs(TEntity eventObject, IPackageInfo packageMetaData, bool canCancel)
: base(new[] { eventObject }, canCancel)
{
PackageMetaData = packageMetaData ?? throw new ArgumentNullException(nameof(packageMetaData));
}
public ImportPackageEventArgs(TEntity eventObject, IPackageInfo packageMetaData)
: this(eventObject, packageMetaData, true)
{
}
public IPackageInfo PackageMetaData { get; }
public IEnumerable<TEntity> InstallationSummary => EventObject;
public bool Equals(ImportPackageEventArgs<TEntity> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// TODO: MetaData for package metadata has no equality operators :/
return base.Equals(other) && PackageMetaData.Equals(other.PackageMetaData);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((ImportPackageEventArgs<TEntity>) obj);
}
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ PackageMetaData.GetHashCode();
}
}
public static bool operator ==(ImportPackageEventArgs<TEntity> left, ImportPackageEventArgs<TEntity> right)
{
return Equals(left, right);
}
public static bool operator !=(ImportPackageEventArgs<TEntity> left, ImportPackageEventArgs<TEntity> right)
{
return !Equals(left, right);
}
}
}

View File

@@ -0,0 +1,19 @@
using Umbraco.Cms.Core.Models.Packaging;
using Umbraco.Cms.Core.Packaging;
namespace Umbraco.Cms.Core.Events
{
public class ImportedPackageNotification : StatefulNotification
{
public ImportedPackageNotification(InstallationSummary installationSummary, IPackageInfo packageMetaData)
{
InstallationSummary = installationSummary;
PackageMetaData = packageMetaData;
}
public IPackageInfo PackageMetaData { get; }
public InstallationSummary InstallationSummary { get; }
}
}

View File

@@ -0,0 +1,19 @@
using Umbraco.Cms.Core.Models.Packaging;
namespace Umbraco.Cms.Core.Events
{
public class ImportingPackageNotification : StatefulNotification, ICancelableNotification
{
public ImportingPackageNotification(string packageName, IPackageInfo packageMetaData)
{
PackageName = packageName;
PackageMetaData = packageMetaData;
}
public string PackageName { get; }
public IPackageInfo PackageMetaData { get; }
public bool Cancel { get; set; }
}
}

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class MovedNotification<T> : ObjectNotification<IEnumerable<MoveEventInfo<T>>>
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class MovingNotification<T> : CancelableObjectNotification<IEnumerable<MoveEventInfo<T>>>
{

View File

@@ -1,9 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class ObjectNotification<T> : StatefulNotification where T : class
{

View File

@@ -1,9 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class RolledBackNotification<T> : ObjectNotification<T> where T : class
{

View File

@@ -1,9 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class RollingBackNotification<T> : CancelableObjectNotification<T> where T : class
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class SavedNotification<T> : EnumerableObjectNotification<T>
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class SavingNotification<T> : CancelableEnumerableObjectNotification<T>
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class SortedNotification<T> : EnumerableObjectNotification<T>
{

View File

@@ -2,9 +2,8 @@
// See LICENSE for more details.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class SortingNotification<T> : CancelableEnumerableObjectNotification<T>
{

View File

@@ -1,9 +1,8 @@
// Copyright (c) Umbraco.
using System.Collections.Generic;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Cms.Infrastructure.Services.Notifications
namespace Umbraco.Cms.Core.Events
{
public abstract class StatefulNotification : IStatefulNotification
{

View File

@@ -1,7 +1,4 @@
using System;
using Umbraco.Cms.Core.Events;
namespace Umbraco.Core.Events
namespace Umbraco.Cms.Core.Events
{
/// <summary>
/// Used to notify that an Unattended install has completed

View File

@@ -1,15 +0,0 @@
using System.Collections.Generic;
using Umbraco.Cms.Core.Packaging;
namespace Umbraco.Cms.Core.Events
{
public class UninstallPackageEventArgs: CancellableObjectEventArgs<IEnumerable<UninstallationSummary>>
{
public UninstallPackageEventArgs(IEnumerable<UninstallationSummary> eventObject, bool canCancel)
: base(eventObject, canCancel)
{
}
public IEnumerable<UninstallationSummary> UninstallationSummary => EventObject;
}
}

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Umbraco.Cms.Core.Packaging;
namespace Umbraco.Cms.Core.Events
{
public class UninstallPackageNotification : INotification
{
public UninstallPackageNotification(IEnumerable<UninstallationSummary> uninstallationSummary) => UninstallationSummary = uninstallationSummary;
public IEnumerable<UninstallationSummary> UninstallationSummary { get; }
}
}

View File

@@ -11,7 +11,6 @@ using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Core.Events;
namespace Umbraco.Cms.Core
{

View File

@@ -22,6 +22,7 @@ namespace Umbraco.Cms.Core.Services.Implement
{
private readonly IPackageInstallation _packageInstallation;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IEventAggregator _eventAggregator;
private readonly IAuditService _auditService;
private readonly ICreatedPackagesRepository _createdPackages;
private readonly IInstalledPackagesRepository _installedPackages;
@@ -32,13 +33,15 @@ namespace Umbraco.Cms.Core.Services.Implement
ICreatedPackagesRepository createdPackages,
IInstalledPackagesRepository installedPackages,
IPackageInstallation packageInstallation,
IHostingEnvironment hostingEnvironment)
IHostingEnvironment hostingEnvironment,
IEventAggregator eventAggregator)
{
_auditService = auditService;
_createdPackages = createdPackages;
_installedPackages = installedPackages;
_packageInstallation = packageInstallation;
_hostingEnvironment = hostingEnvironment;
_eventAggregator = eventAggregator;
}
#region Package Files
@@ -117,8 +120,12 @@ namespace Umbraco.Cms.Core.Services.Implement
var compiledPackage = GetCompiledPackageInfo(packageFile);
if (compiledPackage == null) throw new InvalidOperationException("Could not read the package file " + packageFile);
if (ImportingPackage.IsRaisedEventCancelled(new ImportPackageEventArgs<string>(packageFile.Name, compiledPackage), this))
// Trigger the Importing Package Notification and stop execution if event/user is cancelling it
var importingPackageNotification = new ImportingPackageNotification(packageFile.Name, compiledPackage);
if (_eventAggregator.PublishCancelable(importingPackageNotification))
{
return new InstallationSummary { MetaData = compiledPackage };
}
var summary = _packageInstallation.InstallPackageData(packageDefinition, compiledPackage, userId);
@@ -126,7 +133,8 @@ namespace Umbraco.Cms.Core.Services.Implement
_auditService.Add(AuditType.PackagerInstall, userId, -1, "Package", $"Package data installed for package '{compiledPackage.Name}'.");
ImportedPackage.RaiseEvent(new ImportPackageEventArgs<InstallationSummary>(summary, compiledPackage, false), this);
// trigger the ImportedPackage event
_eventAggregator.Publish(new ImportedPackageNotification(summary, compiledPackage).WithStateFrom(importingPackageNotification));
return summary;
}
@@ -168,7 +176,7 @@ namespace Umbraco.Cms.Core.Services.Implement
}
// trigger the UninstalledPackage event
UninstalledPackage.RaiseEvent(new UninstallPackageEventArgs(allSummaries, false), this);
_eventAggregator.Publish(new UninstallPackageNotification(allSummaries));
return summary;
}
@@ -236,26 +244,5 @@ namespace Umbraco.Cms.Core.Services.Implement
}
#endregion
#region Event Handlers
/// <summary>
/// Occurs before Importing umbraco package
/// </summary>
public static event TypedEventHandler<IPackagingService, ImportPackageEventArgs<string>> ImportingPackage;
/// <summary>
/// Occurs after a package is imported
/// </summary>
public static event TypedEventHandler<IPackagingService, ImportPackageEventArgs<InstallationSummary>> ImportedPackage;
/// <summary>
/// Occurs after a package is uninstalled
/// </summary>
public static event TypedEventHandler<IPackagingService, UninstallPackageEventArgs> UninstalledPackage;
#endregion
}
}

View File

@@ -1,6 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Infrastructure.Services.Notifications

View File

@@ -55,8 +55,6 @@ using Umbraco.Cms.Web.Common.Routing;
using Umbraco.Cms.Web.Common.Security;
using Umbraco.Cms.Web.Common.Templates;
using Umbraco.Cms.Web.Common.UmbracoContext;
using Umbraco.Core.Events;
using static Umbraco.Cms.Core.Cache.HttpContextRequestAppCache;
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
namespace Umbraco.Extensions

View File

@@ -9,7 +9,6 @@ using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Core.Events;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.Install