Consolidating redundant theme/module services

Breaking off site's "default theme" changing methods
Centralizing enable/disable functionality

--HG--
branch : perf
This commit is contained in:
Louis DeJardin
2010-11-04 20:37:35 -07:00
parent 04bb3d90dc
commit 7b508d72d2
22 changed files with 132 additions and 118 deletions

View File

@@ -98,6 +98,10 @@
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
<Name>Orchard.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Themes\Orchard.Themes.csproj">
<Project>{CDE24A24-01D3-403C-84B9-37722E18DFB7}</Project>
<Name>Orchard.Themes</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Widgets\Orchard.Widgets.csproj">
<Project>{194D3CCC-1153-474D-8176-FDE8D7D0D0BD}</Project>
<Name>Orchard.Widgets</Name>

View File

@@ -28,6 +28,7 @@ using Orchard.Settings;
using Orchard.Themes;
using Orchard.Environment.State;
using Orchard.Data.Migration;
using Orchard.Themes.Services;
using Orchard.Widgets.Models;
using Orchard.Widgets;
@@ -197,7 +198,7 @@ namespace Orchard.Setup.Services {
siteSettings.Record.SiteCulture = "en-US";
// set site theme
var themeService = environment.Resolve<IThemeService>();
var themeService = environment.Resolve<ISiteThemeService>();
themeService.SetSiteTheme("TheThemeMachine");
// add default culture

View File

@@ -113,18 +113,17 @@ namespace Orchard.Setup {
public string BaseTheme { get; set; }
}
private readonly FeatureDescriptor _theme = new FeatureDescriptor {
private readonly ExtensionDescriptor _theme = new ExtensionDescriptor {
Name = "SafeMode",
DisplayName = "SafeMode",
Extension = new ExtensionDescriptor { Name = "SafeMode" },
};
public FeatureDescriptor GetThemeByName(string themeName) { return _theme; }
public FeatureDescriptor GetSiteTheme() { return _theme; }
public ExtensionDescriptor GetThemeByName(string themeName) { return _theme; }
public ExtensionDescriptor GetSiteTheme() { return _theme; }
public void SetSiteTheme(string themeName) { }
public FeatureDescriptor GetRequestTheme(RequestContext requestContext) { return _theme; }
public IEnumerable<FeatureDescriptor> GetInstalledThemes() { return new[] { _theme }; }
public IEnumerable<FeatureDescriptor> GetEnabledThemes() { return new[] { _theme }; }
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) { return _theme; }
public IEnumerable<ExtensionDescriptor> GetInstalledThemes() { return new[] { _theme }; }
public IEnumerable<ExtensionDescriptor> GetEnabledThemes() { return new[] { _theme }; }
public void InstallTheme(HttpPostedFileBase file) { }
public void UninstallTheme(string themeName) { }

View File

@@ -4,10 +4,12 @@ using System.Web;
using System.Web.Mvc;
using Orchard.Data.Migration;
using Orchard.DisplayManagement;
using Orchard.Environment.Features;
using Orchard.Localization;
using Orchard.Reports.Services;
using Orchard.Security;
using Orchard.Themes.Preview;
using Orchard.Themes.Services;
using Orchard.Themes.ViewModels;
using Orchard.UI.Notify;
@@ -15,6 +17,8 @@ namespace Orchard.Themes.Controllers {
[ValidateInput(false)]
public class AdminController : Controller {
private readonly IThemeService _themeService;
private readonly IFeatureManager _featureManager;
private readonly ISiteThemeService _siteThemeService;
private readonly IPreviewTheme _previewTheme;
private readonly IDataMigrationManager _dataMigrationManager;
private readonly IReportsCoordinator _reportsCoordinator;
@@ -24,6 +28,8 @@ namespace Orchard.Themes.Controllers {
IReportsCoordinator reportsCoordinator,
IOrchardServices services,
IThemeService themeService,
IFeatureManager featureManager,
ISiteThemeService siteThemeService,
IPreviewTheme previewTheme,
IAuthorizer authorizer,
INotifier notifier) {
@@ -31,6 +37,8 @@ namespace Orchard.Themes.Controllers {
_dataMigrationManager = dataMigraitonManager;
_reportsCoordinator = reportsCoordinator;
_themeService = themeService;
_featureManager = featureManager;
_siteThemeService = siteThemeService;
_previewTheme = previewTheme;
T = NullLocalizer.Instance;
}
@@ -41,7 +49,7 @@ namespace Orchard.Themes.Controllers {
public ActionResult Index() {
try {
var themes = _themeService.GetInstalledThemes();
var currentTheme = _themeService.GetSiteTheme();
var currentTheme = _siteThemeService.GetSiteTheme();
var featuresThatNeedUpdate = _dataMigrationManager.GetFeaturesThatNeedUpdate();
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes, FeaturesThatNeedUpdate = featuresThatNeedUpdate };
return View(model);
@@ -71,8 +79,8 @@ namespace Orchard.Themes.Controllers {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't preview the current theme")))
return new HttpUnauthorizedResult();
_previewTheme.SetPreviewTheme(null);
_themeService.SetSiteTheme(themeName);
_previewTheme.SetPreviewTheme(null);
_siteThemeService.SetSiteTheme(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Previewing theme failed: " + exception.Message));
@@ -98,7 +106,9 @@ namespace Orchard.Themes.Controllers {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't enable the theme")))
return new HttpUnauthorizedResult();
_themeService.EnableTheme(themeName);
// feature id always == extension id, in this case
_featureManager.EnableFeature(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Enabling theme failed: " + exception.Message));
@@ -111,7 +121,9 @@ namespace Orchard.Themes.Controllers {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't disable the current theme")))
return new HttpUnauthorizedResult();
_themeService.DisableTheme(themeName);
// feature id always == extension id, in this case
_featureManager.DisableFeature(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Disabling theme failed: " + exception.Message));
@@ -124,7 +136,7 @@ namespace Orchard.Themes.Controllers {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't set the current theme")))
return new HttpUnauthorizedResult();
_themeService.SetSiteTheme(themeName);
_siteThemeService.SetSiteTheme(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Activating theme failed: " + exception.Message));

View File

@@ -2,6 +2,10 @@
using System.Web.Routing;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Settings;
using Orchard.Themes.Models;
@@ -21,4 +25,35 @@ namespace Orchard.Themes.Services {
return new ThemeSelectorResult { Priority = -5, ThemeName = currentThemeName };
}
}
}
public interface ISiteThemeService : IDependency {
ExtensionDescriptor GetSiteTheme();
void SetSiteTheme(string themeName);
}
public class SiteThemeService : ISiteThemeService {
private readonly IExtensionManager _extensionManager;
public SiteThemeService(IExtensionManager extensionManager) {
_extensionManager = extensionManager;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ExtensionDescriptor GetSiteTheme() {
string currentThemeName = CurrentSite.As<ThemeSiteSettingsPart>().CurrentThemeName;
if (string.IsNullOrEmpty(currentThemeName)) {
return null;
}
return _extensionManager.GetExtensionDescriptor(currentThemeName);
}
public void SetSiteTheme(string themeName) {
CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName = themeName;
}
}
}

View File

@@ -46,22 +46,6 @@ namespace Orchard.Themes.Services {
public ILogger Logger { get; set; }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public FeatureDescriptor GetSiteTheme() {
string currentThemeName = CurrentSite.As<ThemeSiteSettingsPart>().CurrentThemeName;
if (string.IsNullOrEmpty(currentThemeName)) {
return null;
}
return GetThemeByName(currentThemeName);
}
public void SetSiteTheme(string themeName) {
if (DoEnableTheme(themeName)) {
CurrentSite.As<ThemeSiteSettingsPart>().Record.CurrentThemeName = themeName;
}
}
public void EnableTheme(string themeName) {
DoEnableTheme(themeName);
}
@@ -81,7 +65,7 @@ namespace Orchard.Themes.Services {
var baseTheme = GetThemeByName(baseThemeName);
if (baseTheme == null)
return false;
baseThemeName = baseTheme.Extension.BaseTheme;
baseThemeName = baseTheme.BaseTheme;
}
return true;
@@ -97,8 +81,8 @@ namespace Orchard.Themes.Services {
break;
themes.Enqueue(themeName);
themeName = !string.IsNullOrWhiteSpace(theme.Extension.BaseTheme)
? theme.Extension.BaseTheme
themeName = !string.IsNullOrWhiteSpace(theme.BaseTheme)
? theme.BaseTheme
: null;
}
@@ -115,8 +99,8 @@ namespace Orchard.Themes.Services {
themes.Push(themeName);
var theme = GetThemeByName(themeName);
themeName = !string.IsNullOrWhiteSpace(theme.Extension.BaseTheme)
? theme.Extension.BaseTheme
themeName = !string.IsNullOrWhiteSpace(theme.BaseTheme)
? theme.BaseTheme
: null;
}
@@ -135,7 +119,7 @@ namespace Orchard.Themes.Services {
// ensure all base themes down the line are present and accounted for
//todo: (heskew) dito on the need of a meaningful message
if (!AllBaseThemesAreInstalled(themeToEnable.Extension.BaseTheme))
if (!AllBaseThemesAreInstalled(themeToEnable.BaseTheme))
return false;
// enable all theme features
@@ -143,7 +127,7 @@ namespace Orchard.Themes.Services {
return true;
}
public FeatureDescriptor GetRequestTheme(RequestContext requestContext) {
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
.Where(x => x != null)
@@ -161,8 +145,8 @@ namespace Orchard.Themes.Services {
return GetThemeByName("SafeMode");
}
public FeatureDescriptor GetThemeByName(string name) {
foreach (var descriptor in _extensionManager.AvailableFeatures()) {
public ExtensionDescriptor GetThemeByName(string name) {
foreach (var descriptor in _extensionManager.AvailableExtensions()) {
if (string.Equals(descriptor.Name, name, StringComparison.OrdinalIgnoreCase)) {
return descriptor;
}
@@ -173,28 +157,28 @@ namespace Orchard.Themes.Services {
/// <summary>
/// Loads only installed themes
/// </summary>
public IEnumerable<FeatureDescriptor> GetInstalledThemes() {
public IEnumerable<ExtensionDescriptor> GetInstalledThemes() {
return GetThemes(_extensionManager.AvailableExtensions());
}
/// <summary>
/// Loads only enabled themes
/// </summary>
public IEnumerable<FeatureDescriptor> GetEnabledThemes() {
public IEnumerable<ExtensionDescriptor> GetEnabledThemes() {
return GetThemes(_extensionManager.EnabledExtensions(_shellDescriptor));
}
private IEnumerable<FeatureDescriptor> GetThemes(IEnumerable<ExtensionDescriptor> extensions) {
var themes = new List<FeatureDescriptor>();
foreach (var descriptor in extensions.SelectMany(x=>x.Features)) {
private IEnumerable<ExtensionDescriptor> GetThemes(IEnumerable<ExtensionDescriptor> extensions) {
var themes = new List<ExtensionDescriptor>();
foreach (var descriptor in extensions) {
if (!string.Equals(descriptor.Extension.ExtensionType, "Theme", StringComparison.OrdinalIgnoreCase)) {
if (!string.Equals(descriptor.ExtensionType, "Theme", StringComparison.OrdinalIgnoreCase)) {
continue;
}
FeatureDescriptor theme = descriptor;
ExtensionDescriptor theme = descriptor;
if (!theme.Extension.Tags.Contains("hidden")) {
if (!theme.Tags.Contains("hidden")) {
themes.Add(theme);
}
}

View File

@@ -3,8 +3,8 @@ using Orchard.Environment.Extensions.Models;
namespace Orchard.Themes.ViewModels {
public class ThemesIndexViewModel {
public FeatureDescriptor CurrentTheme { get; set; }
public IEnumerable<FeatureDescriptor> Themes { get; set; }
public ExtensionDescriptor CurrentTheme { get; set; }
public IEnumerable<ExtensionDescriptor> Themes { get; set; }
public IEnumerable<string> FeaturesThatNeedUpdate { get; set; }
}
}

View File

@@ -43,8 +43,8 @@ namespace Orchard.Widgets.Services {
public IEnumerable<string> GetZones() {
HashSet<string> zones = new HashSet<string>();
foreach (var theme in _themeService.GetEnabledThemes().Where(theme => theme.Extension.Zones != null && !theme.Extension.Zones.Trim().Equals(string.Empty))) {
foreach (string zone in theme.Extension.Zones.Split(',').Where(zone => !zones.Contains(zone))) {
foreach (var theme in _themeService.GetEnabledThemes().Where(theme => theme.Zones != null && !theme.Zones.Trim().Equals(string.Empty))) {
foreach (string zone in theme.Zones.Split(',').Where(zone => !zones.Contains(zone))) {
zones.Add(zone.Trim());
}
}