Getting closer to restoring existing functionality

--HG--
branch : perf
This commit is contained in:
Louis DeJardin
2010-11-07 14:40:51 -08:00
parent d2960b2c1b
commit 85b1276d68
15 changed files with 344 additions and 67 deletions

View File

@@ -5,6 +5,7 @@ using System.Web;
using System.Web.Mvc;
using Orchard.Data.Migration;
using Orchard.DisplayManagement;
using Orchard.Environment.Descriptor.Models;
using Orchard.Environment.Extensions;
using Orchard.Environment.Features;
using Orchard.Localization;
@@ -18,11 +19,11 @@ using Orchard.UI.Notify;
namespace Orchard.Themes.Controllers {
[ValidateInput(false)]
public class AdminController : Controller {
private readonly IThemeManager _themeManager;
private readonly IFeatureManager _featureManager;
private readonly ISiteThemeService _siteThemeService;
private readonly IPreviewTheme _previewTheme;
private readonly IExtensionManager _extensionManager;
private readonly ShellDescriptor _shellDescriptor;
private readonly IThemeService _themeService;
private readonly IDataMigrationManager _dataMigrationManager;
private readonly IReportsCoordinator _reportsCoordinator;
@@ -36,27 +37,38 @@ namespace Orchard.Themes.Controllers {
IPreviewTheme previewTheme,
IAuthorizer authorizer,
INotifier notifier,
IExtensionManager extensionManager) {
IExtensionManager extensionManager,
ShellDescriptor shellDescriptor,
IThemeService themeService) {
Services = services;
_dataMigrationManager = dataMigraitonManager;
_reportsCoordinator = reportsCoordinator;
_themeManager = themeManager;
_featureManager = featureManager;
_siteThemeService = siteThemeService;
_previewTheme = previewTheme;
_extensionManager = extensionManager;
_shellDescriptor = shellDescriptor;
_themeService = themeService;
T = NullLocalizer.Instance;
}
public IOrchardServices Services{ get; set; }
public IOrchardServices Services { get; set; }
public Localizer T { get; set; }
public ActionResult Index() {
try {
var themes = _extensionManager.AvailableExtensions().Where(d => d.ExtensionType == "Theme");
var currentTheme = _siteThemeService.GetSiteTheme();
var featuresThatNeedUpdate = _dataMigrationManager.GetFeaturesThatNeedUpdate();
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes, FeaturesThatNeedUpdate = featuresThatNeedUpdate };
var themes = _extensionManager.AvailableExtensions()
.Where(d => d.ExtensionType == "Theme")
.Select(d => new ThemeEntry {
Descriptor = d,
NeedsUpdate = featuresThatNeedUpdate.Contains(d.Name),
Enabled = _shellDescriptor.Features.Any(sf => sf.Name == d.Name)
})
.ToArray();
var model = new ThemesIndexViewModel { CurrentTheme = currentTheme, Themes = themes };
return View(model);
}
catch (Exception exception) {
@@ -112,8 +124,7 @@ namespace Orchard.Themes.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't enable the theme")))
return new HttpUnauthorizedResult();
// feature id always == extension id, in this case
_featureManager.EnableFeature(themeName);
_themeService.EnableThemeFeatures(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Enabling theme failed: " + exception.Message));
@@ -127,8 +138,7 @@ namespace Orchard.Themes.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't disable the current theme")))
return new HttpUnauthorizedResult();
// feature id always == extension id, in this case
_featureManager.DisableFeature(themeName);
_themeService.DisableThemeFeatures(themeName);
}
catch (Exception exception) {
Services.Notifier.Error(T("Disabling theme failed: " + exception.Message));
@@ -141,6 +151,8 @@ namespace Orchard.Themes.Controllers {
try {
if (!Services.Authorizer.Authorize(Permissions.ApplyTheme, T("Couldn't set the current theme")))
return new HttpUnauthorizedResult();
_themeService.EnableThemeFeatures(themeName);
_siteThemeService.SetSiteTheme(themeName);
}
catch (Exception exception) {

View File

@@ -86,7 +86,7 @@
<Compile Include="Services\SafeModeThemeSelector.cs" />
<Compile Include="Services\SiteThemeSelector.cs" />
<Compile Include="Services\SiteThemeService.cs" />
<Compile Include="Services\ThemeManager.cs" />
<Compile Include="Services\ThemeService.cs" />
<Compile Include="ViewModels\ThemesIndexViewModel.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using JetBrains.Annotations;
using Orchard.Environment.Descriptor;
using Orchard.Environment.Descriptor.Models;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
using Orchard.Environment.Features;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.ContentManagement;
using Orchard.Themes.Models;
namespace Orchard.Themes.Services {
public interface IThemeService : IDependency {
void DisableThemeFeatures(string themeName);
void EnableThemeFeatures(string themeName);
}
[UsedImplicitly]
public class ThemeService : IThemeService {
private readonly IExtensionManager _extensionManager;
private readonly IFeatureManager _featureManager;
private readonly IEnumerable<IThemeSelector> _themeSelectors;
private readonly IWorkContextAccessor _workContextAccessor;
private readonly ShellDescriptor _shellDescriptor;
private readonly IOrchardServices _orchardServices;
private readonly IShellDescriptorManager _shellDescriptorManager;
public ThemeService(
IShellDescriptorManager shellDescriptorManager,
IExtensionManager extensionManager,
IFeatureManager featureManager,
IEnumerable<IThemeSelector> themeSelectors,
IWorkContextAccessor workContextAccessor,
ShellDescriptor shellDescriptor,
IOrchardServices orchardServices) {
_shellDescriptorManager = shellDescriptorManager;
_extensionManager = extensionManager;
_featureManager = featureManager;
_themeSelectors = themeSelectors;
_workContextAccessor = workContextAccessor;
_shellDescriptor = shellDescriptor;
_orchardServices = orchardServices;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
private bool AllBaseThemesAreInstalled(string baseThemeName) {
var themesSeen = new List<string>();
while (!string.IsNullOrWhiteSpace(baseThemeName)) {
//todo: (heskew) need a better way to protect from recursive references
if (themesSeen.Contains(baseThemeName))
throw new InvalidOperationException(T("The theme \"{0}\" was already seen - looks like we're going around in circles.", baseThemeName).Text);
themesSeen.Add(baseThemeName);
var baseTheme = _extensionManager.GetExtension(baseThemeName);
if (baseTheme == null)
return false;
baseThemeName = baseTheme.BaseTheme;
}
return true;
}
public void DisableThemeFeatures(string themeName) {
var themes = new Queue<string>();
while (themeName != null) {
if (themes.Contains(themeName))
throw new InvalidOperationException(T("The theme \"{0}\" is already in the stack of themes that need features disabled.", themeName).Text);
var theme = _extensionManager.GetExtension(themeName);
if (theme == null)
break;
themes.Enqueue(themeName);
themeName = !string.IsNullOrWhiteSpace(theme.BaseTheme)
? theme.BaseTheme
: null;
}
while (themes.Count > 0)
_featureManager.DisableFeatures(new[] { themes.Dequeue() });
}
public void EnableThemeFeatures(string themeName) {
var themes = new Stack<string>();
while(themeName != null) {
if (themes.Contains(themeName))
throw new InvalidOperationException(T("The theme \"{0}\" is already in the stack of themes that need features enabled.", themeName).Text);
themes.Push(themeName);
var theme = _extensionManager.GetExtension(themeName);
themeName = !string.IsNullOrWhiteSpace(theme.BaseTheme)
? theme.BaseTheme
: null;
}
while (themes.Count > 0)
_featureManager.EnableFeatures(new[] {themes.Pop()});
}
private bool DoEnableTheme(string themeName) {
if (string.IsNullOrWhiteSpace(themeName))
return false;
//todo: (heskew) need messages given in addition to all of these early returns so something meaningful can be presented to the user
var themeToEnable = _extensionManager.GetExtension(themeName);
if (themeToEnable == null)
return false;
// 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.BaseTheme))
return false;
// enable all theme features
EnableThemeFeatures(themeToEnable.Name);
return true;
}
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
.Where(x => x != null)
.OrderByDescending(x => x.Priority);
if (requestTheme.Count() < 1)
return null;
foreach (var theme in requestTheme) {
var t = _extensionManager.GetExtension(theme.ThemeName);
if (t != null)
return t;
}
return _extensionManager.GetExtension("SafeMode");
}
/// <summary>
/// Loads only installed themes
/// </summary>
public IEnumerable<ExtensionDescriptor> GetInstalledThemes() {
return GetThemes(_extensionManager.AvailableExtensions());
}
private IEnumerable<ExtensionDescriptor> GetThemes(IEnumerable<ExtensionDescriptor> extensions) {
var themes = new List<ExtensionDescriptor>();
foreach (var descriptor in extensions) {
if (!string.Equals(descriptor.ExtensionType, "Theme", StringComparison.OrdinalIgnoreCase)) {
continue;
}
ExtensionDescriptor theme = descriptor;
if (!theme.Tags.Contains("hidden")) {
themes.Add(theme);
}
}
return themes;
}
private static string TryLocalize(string key, string original, Localizer localizer) {
var localized = localizer(key).Text;
if ( key == localized ) {
// no specific localization available
return original;
}
return localized;
}
private bool IsThemeEnabled(ExtensionDescriptor descriptor) {
return (descriptor.Name == "TheAdmin" || descriptor.Name == "SafeMode") ||
_shellDescriptorManager.GetShellDescriptor().Features.Any(sf => sf.Name == descriptor.Name);
}
//private ITheme CreateTheme(ExtensionDescriptor descriptor) {
// var localizer = LocalizationUtilities.Resolve(_workContextAccessor.GetContext(), String.Concat(descriptor.Location, "/", descriptor.Name, "/Theme.txt"));
// return new Theme {
// //Author = TryLocalize("Author", descriptor.Author, localizer) ?? "",
// //Description = TryLocalize("Description", descriptor.Description, localizer) ?? "",
// DisplayName = TryLocalize("Name", descriptor.DisplayName, localizer) ?? "",
// //HomePage = TryLocalize("Website", descriptor.WebSite, localizer) ?? "",
// ThemeName = descriptor.Name,
// //Version = descriptor.Version ?? "",
// Tags = TryLocalize("Tags", descriptor.Tags, localizer) ?? "",
// Zones = descriptor.Zones ?? "",
// BaseTheme = descriptor.BaseTheme ?? "",
// Enabled = IsThemeEnabled(descriptor)
// };
//}
}
}

View File

@@ -4,7 +4,18 @@ using Orchard.Environment.Extensions.Models;
namespace Orchard.Themes.ViewModels {
public class ThemesIndexViewModel {
public ExtensionDescriptor CurrentTheme { get; set; }
public IEnumerable<ExtensionDescriptor> Themes { get; set; }
public IEnumerable<string> FeaturesThatNeedUpdate { get; set; }
public IEnumerable<ThemeEntry> Themes { get; set; }
}
public class ThemeEntry {
public ExtensionDescriptor Descriptor { get; set; }
public bool Enabled { get; set; }
public bool NeedsUpdate { get; set; }
public string ThemeName { get { return Descriptor.Name; } }
public string DisplayName { get { return Descriptor.DisplayName; } }
public string ThemePath(string path) {
return Descriptor.Location + "/" + Descriptor.Name + path;
}
}
}

View File

@@ -15,7 +15,7 @@
<p>
@T("Version:") @Model.CurrentTheme.Version<br />
@Model.CurrentTheme.Description<br />
@Model.CurrentTheme.HomePage
@Model.CurrentTheme.WebSite
</p>
@Html.ActionLink(T("Install a new Theme").ToString(), "Install", null, new { @class = "button primaryAction" })
@@ -24,11 +24,11 @@
<h2>@T("Available Themes")</h2>
<ul class="templates">
@foreach (var theme in Model.Themes) {
if (Model.CurrentTheme == null || theme.ThemeName != Model.CurrentTheme.ThemeName) {
if (Model.CurrentTheme == null || theme.ThemeName != Model.CurrentTheme.Name) {
<li>
<div>
<h3>@theme.DisplayName</h3>
@Html.Image(Href(Html.ThemePath(theme, "/Theme.png")), Html.Encode(theme.DisplayName), null)
@Html.Image(Href(theme.ThemePath("/Theme.png")), Html.Encode(theme.DisplayName), null)
@using (Html.BeginFormAntiForgeryPost(Url.Action(theme.Enabled ? "Disable" : "Enable"), FormMethod.Post, new { @class = "inline" })) {
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" title="@T(theme.Enabled ? "Disable" : "Enable")">@T(theme.Enabled ? "Disable" : "Enable")</button>
@@ -41,13 +41,13 @@
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" title="@T("Preview")">@T("Preview")</button>
}
<h5>@T("By") @theme.Author</h5>
<h5>@T("By") @theme.Descriptor.Author</h5>
<p>
@T("Version:") @theme.Version<br />
@theme.Description<br />
@theme.HomePage
@T("Version:") @theme.Descriptor.Version<br />
@theme.Descriptor.Description<br />
@theme.Descriptor.WebSite
</p>
@if(Model.FeaturesThatNeedUpdate.Contains(theme.ThemeName)){
@if(theme.NeedsUpdate){
using (Html.BeginFormAntiForgeryPost(Url.Action("Update"), FormMethod.Post, new { @class = "inline link" })) {
@Html.Hidden("themeName", theme.ThemeName)
<button type="submit" class="update">@T("Update")</button> <br/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

View File

@@ -0,0 +1,6 @@
Name: Primus
Author: Lou
Description: desc
Version: 0.1
Website: http://whereslou.com
Zones: Main, Sidebar

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

View File

@@ -0,0 +1,6 @@
Name: Secundus
Author: Lou
Description: desc
Version: 0.1
Website: http://whereslou.com
Zones: Main, Sidebar

View File

@@ -49,6 +49,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Primus\Theme.png" />
<Content Include="Primus\Theme.txt" />
<Content Include="Secundus\Theme.png" />
<Content Include="Secundus\Theme.txt" />
<Content Include="TheAdmin\Scripts\admin.js" />
<Content Include="TheAdmin\Styles\ie.css" />
<Content Include="TheAdmin\Styles\images\menuClosed.gif" />
@@ -93,7 +97,6 @@
<Content Include="TheAdmin\Views\Header.cshtml" />
<Content Include="TheThemeMachine\Views\Layout.cshtml" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -57,7 +57,7 @@
affects performance, set this value to true only
during development.
-->
<compilation debug="false" targetFramework="4.0">
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<add extension=".csproj" type="Orchard.Environment.Extensions.Compilers.CSharpExtensionBuildProviderShim"/>
</buildProviders>

View File

@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment.Descriptor;
using Orchard.Environment.Descriptor.Models;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
@@ -8,16 +11,22 @@ namespace Orchard.Environment.Features {
IEnumerable<FeatureDescriptor> GetAvailableFeatures();
IEnumerable<FeatureDescriptor> GetEnabledFeatures();
void EnableFeature(string name);
void DisableFeature(string name);
void EnableFeatures(IEnumerable<string> featureNames);
void DisableFeatures(IEnumerable<string> featureNames);
}
public class FeatureManager : IFeatureManager {
private readonly IExtensionManager _extensionManager;
private readonly ShellDescriptor _shellDescriptor;
private readonly IShellDescriptorManager _shellDescriptorManager;
public FeatureManager(IExtensionManager extensionManager) {
public FeatureManager(
IExtensionManager extensionManager,
ShellDescriptor shellDescriptor,
IShellDescriptorManager shellDescriptorManager) {
_extensionManager = extensionManager;
_shellDescriptor = shellDescriptor;
_shellDescriptorManager = shellDescriptorManager;
}
public IEnumerable<FeatureDescriptor> GetAvailableFeatures() {
@@ -28,12 +37,30 @@ namespace Orchard.Environment.Features {
throw new NotImplementedException();
}
public void EnableFeature(string name) {
throw new NotImplementedException();
public void EnableFeatures(IEnumerable<string> featureNames) {
var currentShellDescriptor = _shellDescriptorManager.GetShellDescriptor();
var updatedFeatures = currentShellDescriptor.Features
.Union(featureNames
.Where(name => !currentShellDescriptor.Features.Any(sf => sf.Name == name))
.Select(name => new ShellFeature {Name = name}));
_shellDescriptorManager.UpdateShellDescriptor(
currentShellDescriptor.SerialNumber,
updatedFeatures,
currentShellDescriptor.Parameters);
}
public void DisableFeature(string name) {
throw new NotImplementedException();
public void DisableFeatures(IEnumerable<string> featureNames) {
var currentShellDescriptor = _shellDescriptorManager.GetShellDescriptor();
var updatedFeatures = currentShellDescriptor.Features
.Where(sf => !featureNames.Contains(sf.Name));
_shellDescriptorManager.UpdateShellDescriptor(
currentShellDescriptor.SerialNumber,
updatedFeatures,
currentShellDescriptor.Parameters);
}

View File

@@ -186,6 +186,7 @@
<Compile Include="Security\CurrentUserWorkContext.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="Themes\ThemeManager.cs" />
<Compile Include="UI\FlatPositionComparer.cs" />
<Compile Include="UI\Resources\IResourceManifestProvider.cs" />
<Compile Include="UI\Resources\ResourceManifestBuilder.cs" />

View File

@@ -1,42 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
namespace Orchard.Themes {
public interface IThemeManager : IDependency {
[Obsolete]
ExtensionDescriptor GetRequestTheme(RequestContext requestContext);
}
public class ThemeManager : IThemeManager {
private readonly IEnumerable<IThemeSelector> _themeSelectors;
private readonly IExtensionManager _extensionManager;
public ThemeManager(IEnumerable<IThemeSelector> themeSelectors,
IExtensionManager extensionManager) {
_themeSelectors = themeSelectors;
_extensionManager = extensionManager;
}
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
.Where(x => x != null)
.OrderByDescending(x => x.Priority);
if (requestTheme.Count() < 1)
return null;
foreach (var theme in requestTheme) {
var t = _extensionManager.GetExtension(theme.ThemeName);
if (t != null)
return t;
}
return _extensionManager.GetExtension("SafeMode");
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using Orchard.Environment.Extensions;
using Orchard.Environment.Extensions.Models;
namespace Orchard.Themes {
public class ThemeManager : IThemeManager {
private readonly IEnumerable<IThemeSelector> _themeSelectors;
private readonly IExtensionManager _extensionManager;
public ThemeManager(IEnumerable<IThemeSelector> themeSelectors,
IExtensionManager extensionManager) {
_themeSelectors = themeSelectors;
_extensionManager = extensionManager;
}
public ExtensionDescriptor GetRequestTheme(RequestContext requestContext) {
var requestTheme = _themeSelectors
.Select(x => x.GetTheme(requestContext))
.Where(x => x != null)
.OrderByDescending(x => x.Priority);
if (requestTheme.Count() < 1)
return null;
foreach (var theme in requestTheme) {
var t = _extensionManager.GetExtension(theme.ThemeName);
if (t != null)
return t;
}
return _extensionManager.GetExtension("SafeMode");
}
}
}