Improving how dependencies for themes are handled: enabling dependencies (modules) for themes too, handling this also when themes are just previewed and disabling themes when their dependencies are disabled.

Fixes the tightly connected following issues: #4336, #5004, #4667
This commit is contained in:
Lombiq 2015-11-30 23:37:59 +01:00
parent c2dd09af5e
commit 0e1cca0fce
5 changed files with 116 additions and 6 deletions

View File

@ -37,6 +37,8 @@ namespace Orchard.Themes.Controllers {
private readonly IReportsCoordinator _reportsCoordinator;
private readonly ShellSettings _shellSettings;
private const string AlreadyEnabledFeatures = "Orchard.Themes.AlreadyEnabledFeatures";
public AdminController(
IEnumerable<IExtensionDisplayEventHandler> extensionDisplayEventHandlers,
IOrchardServices services,
@ -136,8 +138,11 @@ namespace Orchard.Themes.Controllers {
Services.Notifier.Error(T("Theme {0} was not found", themeId));
} else {
var alreadyEnabledFeatures = GetEnabledFeatures();
_themeService.EnableThemeFeatures(themeId);
_previewTheme.SetPreviewTheme(themeId);
alreadyEnabledFeatures.Except(new[] { themeId });
TempData[AlreadyEnabledFeatures] = alreadyEnabledFeatures;
}
return this.RedirectLocal(returnUrl, "~/");
@ -165,6 +170,18 @@ namespace Orchard.Themes.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
return new HttpUnauthorizedResult();
if (TempData.ContainsKey(AlreadyEnabledFeatures)) {
var alreadyEnabledFeatures = TempData[AlreadyEnabledFeatures] as IEnumerable<string>;
if (alreadyEnabledFeatures != null) {
var afterEnabledFeatures = GetEnabledFeatures();
if (afterEnabledFeatures.Count() > alreadyEnabledFeatures.Count()) {
var disableFeatures = afterEnabledFeatures.Except(alreadyEnabledFeatures);
_themeService.DisablePreviewFeatures(disableFeatures);
}
}
}
_previewTheme.SetPreviewTheme(null);
return RedirectToAction("Index");
@ -256,5 +273,9 @@ namespace Orchard.Themes.Controllers {
return string.IsNullOrEmpty(value);
}
}
public IEnumerable<string> GetEnabledFeatures() {
return _featureManager.GetEnabledFeatures().Select(f => f.Id);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Environment.Features;
using Orchard.Localization;
using Orchard.Themes.Services;
using Orchard.UI.Notify;
namespace Orchard.Themes.Events {
public class ThemeDisableEventHandler : IFeatureEventHandler {
private readonly IFeatureManager _featureManager;
private readonly ISiteThemeService _siteThemeService;
private readonly INotifier _notifier;
public ThemeDisableEventHandler(
IFeatureManager featureManager,
ISiteThemeService siteThemeService,
INotifier notifier) {
_featureManager = featureManager;
_siteThemeService = siteThemeService;
_notifier = notifier;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Installing(Feature feature) {
}
public void Installed(Feature feature) {
}
public void Enabling(Feature feature) {
}
public void Enabled(Feature feature) {
}
public void Disabling(Feature feature) {
}
public void Disabled(Feature feature) {
var currentTheme = _siteThemeService.GetCurrentThemeName();
if (feature.Descriptor.Name == currentTheme) {
_siteThemeService.SetSiteTheme(null);
// Notifications don't work in feature events. See: https://github.com/OrchardCMS/Orchard/issues/6106
_notifier.Warning(T("The current theme was disabled, because one of its dependencies was disabled."));
}
}
public void Uninstalling(Feature feature) {
}
public void Uninstalled(Feature feature) {
}
}
}

View File

@ -66,6 +66,7 @@
<Compile Include="AdminMenu.cs" />
<Compile Include="Commands\ThemeCommands.cs" />
<Compile Include="Drivers\DisableThemePartDriver.cs" />
<Compile Include="Events\ThemeDisableEventHandler.cs" />
<Compile Include="Events\IExtensionDisplayEventHandler.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Models\ThemeEntry.cs" />

View File

@ -1,9 +1,11 @@
using Orchard.Environment.Extensions.Models;
using System.Collections.Generic;
using Orchard.Environment.Extensions.Models;
namespace Orchard.Themes.Services {
public interface IThemeService : IDependency {
void DisableThemeFeatures(string themeName);
void EnableThemeFeatures(string themeName);
bool IsRecentlyInstalled(ExtensionDescriptor module);
void DisablePreviewFeatures(IEnumerable<string> features);
}
}

View File

@ -20,6 +20,7 @@ namespace Orchard.Themes.Services {
private readonly IEnumerable<IThemeSelector> _themeSelectors;
private readonly IVirtualPathProvider _virtualPathProvider;
private readonly ICacheManager _cacheManager;
private readonly ISiteThemeService _siteThemeService;
public ThemeService(
IOrchardServices orchardServices,
@ -27,7 +28,8 @@ namespace Orchard.Themes.Services {
IFeatureManager featureManager,
IEnumerable<IThemeSelector> themeSelectors,
IVirtualPathProvider virtualPathProvider,
ICacheManager cacheManager) {
ICacheManager cacheManager,
ISiteThemeService siteThemeService) {
Services = orchardServices;
@ -36,6 +38,7 @@ namespace Orchard.Themes.Services {
_themeSelectors = themeSelectors;
_virtualPathProvider = virtualPathProvider;
_cacheManager = cacheManager;
_siteThemeService = siteThemeService;
if (_featureManager.FeatureDependencyNotification == null) {
_featureManager.FeatureDependencyNotification = GenerateWarning;
@ -64,8 +67,16 @@ namespace Orchard.Themes.Services {
: null;
}
while (themes.Count > 0)
_featureManager.DisableFeatures(new[] { themes.Dequeue() });
var currentTheme = _siteThemeService.GetCurrentThemeName();
while (themes.Count > 0) {
var themeId = themes.Dequeue();
// Not disabling base theme if it's the current theme.
if (themeId != currentTheme) {
_featureManager.DisableFeatures(new[] { themeId });
}
}
}
public void EnableThemeFeatures(string themeName) {
@ -81,8 +92,15 @@ namespace Orchard.Themes.Services {
: null;
}
while (themes.Count > 0)
_featureManager.EnableFeatures(new[] {themes.Pop()});
while (themes.Count > 0) {
var themeId = themes.Pop();
foreach (var featureId in _featureManager.EnableFeatures(new[] { themeId }, true)) {
if (themeId != featureId) {
var featureName = _featureManager.GetAvailableFeatures().First(f => f.Id.Equals(featureId, StringComparison.OrdinalIgnoreCase)).Name;
Services.Notifier.Information(T("{0} was enabled", featureName));
}
}
}
}
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) {
@ -174,5 +192,12 @@ namespace Orchard.Themes.Services {
: "{0}, "), fn).ToString()).ToArray())
: featuresInQuestion.First()));
}
public void DisablePreviewFeatures(IEnumerable<string> features) {
foreach (var featureId in _featureManager.DisableFeatures(features,true)) {
var featureName = _featureManager.GetAvailableFeatures().First(f => f.Id.Equals(featureId, StringComparison.OrdinalIgnoreCase)).Name;
Services.Notifier.Information(T("{0} was disabled", featureName));
}
}
}
}