Creating and using shell settings loader

All *.txt files in ~/app_data/sites are discovered
Only one file should be present at the moment
If no files are present the setup shell container will be used

--HG--
branch : dev
This commit is contained in:
Louis DeJardin
2010-02-04 22:17:17 -08:00
parent b50dfa7c50
commit 69c2d9475d
12 changed files with 103 additions and 35 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
@@ -11,6 +10,7 @@ using Autofac.Modules;
using Moq;
using NUnit.Framework;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Mvc;
using Orchard.Mvc.ModelBinders;
using Orchard.Mvc.Routes;
@@ -46,9 +46,16 @@ namespace Orchard.Tests.Environment {
builder.Register(new ViewEngineCollection { new WebFormViewEngine() });
builder.Register(new StuExtensionManager()).As<IExtensionManager>();
builder.Register(new Mock<IHackInstallationGenerator>().Object);
builder.Register(new StubShellSettingsLoader()).As<IShellSettingsLoader>();
});
}
public class StubShellSettingsLoader : IShellSettingsLoader {
public IEnumerable<IShellSettings> LoadSettings() {
return new[] { new ShellSettings { Name = "testing" } };
}
}
public class StuExtensionManager : IExtensionManager {
public IEnumerable<ExtensionDescriptor> AvailableExtensions() {
return Enumerable.Empty<ExtensionDescriptor>();

View File

@@ -0,0 +1,9 @@
namespace Orchard.Environment.Configuration {
public interface IShellSettings {
string Name { get; set; }
}
public class ShellSettings : IShellSettings {
public string Name { get; set; }
}
}

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using Yaml.Grammar;
namespace Orchard.Environment.Configuration {
public interface IShellSettingsLoader {
IEnumerable<IShellSettings> LoadSettings();
}
public class ShellSettingsLoader : IShellSettingsLoader {
IEnumerable<IShellSettings> IShellSettingsLoader.LoadSettings() {
return LoadSettings().ToArray();
}
static IEnumerable<IShellSettings> LoadSettings() {
foreach (var yamlDocument in LoadFiles()) {
yield return ParseSettings(yamlDocument);
}
}
static IEnumerable<YamlDocument> LoadFiles() {
var sitesPath = HostingEnvironment.MapPath("~/App_Data/Sites");
if (sitesPath != null && Directory.Exists(sitesPath)) {
foreach (var settingsFilePath in Directory.GetFiles(sitesPath, "*.txt")) {
var yamlStream = YamlParser.Load(settingsFilePath);
yield return yamlStream.Documents.Single();
}
}
}
static IShellSettings ParseSettings(YamlDocument document) {
var mapping = (Mapping)document.Root;
var fields = mapping.Entities
.Where(x => x.Key is Scalar)
.ToDictionary(x => ((Scalar)x.Key).Text, x => x.Value);
return new ShellSettings { Name = GetValue(fields, "Name") };
}
static string GetValue(
IDictionary<string, DataItem> fields,
string key) {
DataItem value;
return fields.TryGetValue(key, out value) ? value.ToString() : null;
}
}
}

View File

@@ -1,12 +1,9 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Autofac;
using Autofac.Builder;
using Autofac.Integration.Web;
using System.Collections.Generic;
using AutofacContrib.DynamicProxy2;
using Orchard.Environment.Configuration;
using Orchard.Environment.ShellBuilders;
using Orchard.Extensions;
using Orchard.Mvc;
@@ -19,14 +16,17 @@ namespace Orchard.Environment {
private readonly ControllerBuilder _controllerBuilder;
private readonly IEnumerable<IShellContainerFactory> _shellContainerFactories;
private readonly IShellSettingsLoader _shellSettingsLoader;
private IOrchardShell _current;
public DefaultOrchardHost(
IContainerProvider containerProvider,
IShellSettingsLoader shellSettingsLoader,
ControllerBuilder controllerBuilder,
IEnumerable<IShellContainerFactory> shellContainerFactories) {
_containerProvider = containerProvider;
_shellSettingsLoader = shellSettingsLoader;
_controllerBuilder = controllerBuilder;
_shellContainerFactories = shellContainerFactories;
}
@@ -71,10 +71,21 @@ namespace Orchard.Environment {
}
public virtual IContainer CreateShellContainer() {
foreach(var factory in _shellContainerFactories) {
var container = factory.CreateContainer(null);
if (container != null)
return container;
var settings = _shellSettingsLoader.LoadSettings();
if (settings.Any()) {
//TEMP: multi-tenancy not implemented yet
foreach (var factory in _shellContainerFactories) {
var container = factory.CreateContainer(settings.Single());
if (container != null)
return container;
}
}
else {
foreach (var factory in _shellContainerFactories) {
var container = factory.CreateContainer(null);
if (container != null)
return container;
}
}
return null;
}

View File

@@ -5,6 +5,7 @@ using Autofac.Builder;
using Autofac.Integration.Web;
using Autofac.Modules;
using AutofacContrib.DynamicProxy2;
using Orchard.Environment.Configuration;
using Orchard.Environment.ShellBuilders;
using Orchard.Extensions;
using Orchard.Extensions.Loaders;
@@ -21,6 +22,7 @@ namespace Orchard.Environment {
builder.Register<DefaultOrchardHost>().As<IOrchardHost>().SingletonScoped();
builder.Register<DefaultCompositionStrategy>().As<ICompositionStrategy>().SingletonScoped();
builder.Register<DefaultShellContainerFactory>().As<IShellContainerFactory>().SingletonScoped();
builder.Register<ShellSettingsLoader>().As<IShellSettingsLoader>().SingletonScoped();
builder.Register<SetupShellContainerFactory>().As<IShellContainerFactory>().SingletonScoped();
// The container provider gives you access to the lowest container at the time,

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Autofac;
using Autofac.Builder;
using AutofacContrib.DynamicProxy2;
using Orchard.Environment.Configuration;
namespace Orchard.Environment.ShellBuilders {
public class DefaultShellContainerFactory : IShellContainerFactory {

View File

@@ -1,4 +1,5 @@
using Autofac;
using Orchard.Environment.Configuration;
namespace Orchard.Environment.ShellBuilders {
public interface IShellContainerFactory {

View File

@@ -6,6 +6,7 @@ using System.Web.Routing;
using Autofac;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Environment.Configuration;
using Orchard.Extensions;
using Orchard.Localization;
using Orchard.Mvc;

View File

@@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Orchard.Environment.ShellBuilders {
public interface IShellSettings {
}
public class ShellSettings : IShellSettings {
}
}

View File

@@ -43,10 +43,10 @@ namespace Orchard.Extensions {
if (File.Exists(extensionManifestPath)) {
var yamlStream = YamlParser.Load(extensionManifestPath);
return new ParseResult {
Location = path,
Name = name,
YamlDocument = yamlStream.Documents.Single()
};
Location = path,
Name = name,
YamlDocument = yamlStream.Documents.Single()
};
}
if (_manifestIsOptional) {
@@ -55,10 +55,10 @@ namespace Orchard.Extensions {
bool success;
var yamlStream = parser.ParseYamlStream(yamlInput, out success);
return new ParseResult {
Location = path,
Name = name,
YamlDocument = yamlStream.Documents.Single()
};
Location = path,
Name = name,
YamlDocument = yamlStream.Documents.Single()
};
}
}
return null;

View File

@@ -139,10 +139,6 @@ namespace Orchard.Extensions {
foreach (var descriptor in AvailableExtensions()) {
// Extensions that are Themes don't have buildable components.
if (String.Equals(descriptor.ExtensionType, "Package", StringComparison.OrdinalIgnoreCase)) {
//TEMP!!!!
if (descriptor.DisplayName!="Setup") continue;
yield return BuildEntry(descriptor);
}
}

View File

@@ -133,11 +133,12 @@
<Compile Include="ContentManagement\Handlers\RemoveContentContext.cs" />
<Compile Include="ContentManagement\Handlers\VersionContentContext.cs" />
<Compile Include="Data\Conventions\RecordTableNameConvention.cs" />
<Compile Include="Environment\Configuration\ShellSettingsLoader.cs" />
<Compile Include="Environment\ExtensibleInterceptionModule.cs" />
<Compile Include="Environment\ShellBuilders\DefaultShellContainerFactory.cs" />
<Compile Include="Environment\ShellBuilders\IShellContainerFactory.cs" />
<Compile Include="Environment\ShellBuilders\SetupShellContainerFactory.cs" />
<Compile Include="Environment\ShellBuilders\ShellSettings.cs" />
<Compile Include="Environment\Configuration\ShellSettings.cs" />
<Compile Include="Extensions\AreaFolders.cs" />
<Compile Include="Extensions\ExtensionFolders.cs" />
<Compile Include="Extensions\Loaders\AreaExtensionLoader.cs" />