using System; using System.Collections.Generic; using System.Configuration; using AutoMapper; using Umbraco.Core.Cache; using Umbraco.Core.Components; using Umbraco.Core.Composing; using Umbraco.Core.Composing.Composers; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.IO; using Umbraco.Core.IO.MediaPathSchemes; using Umbraco.Core.Logging; using Umbraco.Core.Manifest; using Umbraco.Core.Migrations; using Umbraco.Core.Migrations.Install; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.PropertyEditors; using Umbraco.Core.PropertyEditors.Validators; using Umbraco.Core.Scoping; using Umbraco.Core.Services; using Umbraco.Core.Strings; using Umbraco.Core.Sync; using Umbraco.Core._Legacy.PackageActions; using IntegerValidator = Umbraco.Core.PropertyEditors.Validators.IntegerValidator; namespace Umbraco.Core.Runtime { public class CoreRuntimeComponent : UmbracoComponentBase, IRuntimeComponent { public override void Compose(Composition composition) { base.Compose(composition); // composers composition .ComposeConfiguration() .ComposeRepositories() .ComposeServices() .ComposeCoreMappingProfiles() .ComposeFileSystems(); // register persistence mappers - required by database factory so needs to be done here // means the only place the collection can be modified is in a runtime - afterwards it // has been frozen and it is too late composition.GetCollectionBuilder().AddCoreMappers(); // register the scope provider composition.RegisterSingleton(); // implements both IScopeProvider and IScopeAccessor composition.RegisterSingleton(f => f.GetInstance()); composition.RegisterSingleton(f => f.GetInstance()); // register database builder // *not* a singleton, don't want to keep it around composition.Register(); // register manifest parser, will be injected in collection builders where needed composition.RegisterSingleton(); // register our predefined validators composition.GetCollectionBuilder() .Add() .Add() .Add() .Add() .Add() .Add(); // properties and parameters derive from data editors composition.GetCollectionBuilder() .Add(() => composition.TypeLoader.GetDataEditors()); composition.RegisterSingleton(); composition.RegisterSingleton(); // register a server registrar, by default it's the db registrar composition.RegisterSingleton(f => { if ("true".InvariantEquals(ConfigurationManager.AppSettings["umbracoDisableElectionForSingleServer"])) return new SingleServerRegistrar(f.GetInstance()); return new DatabaseServerRegistrar( new Lazy(f.GetInstance), new DatabaseServerRegistrarOptions()); }); // by default we'll use the database server messenger with default options (no callbacks), // this will be overridden by either the legacy thing or the db thing in the corresponding // components in the web project - fixme - should obsolete the legacy thing composition.RegisterSingleton(factory => new DatabaseServerMessenger( factory.GetInstance(), factory.GetInstance(), factory.GetInstance(), factory.GetInstance(), factory.GetInstance(), true, new DatabaseServerMessengerOptions())); composition.GetCollectionBuilder() .Add(() => composition.TypeLoader.GetCacheRefreshers()); composition.GetCollectionBuilder() .Add(() => composition.TypeLoader.GetPackageActions()); composition.GetCollectionBuilder() .Append(composition.TypeLoader.GetTypes()); composition.RegisterSingleton(); composition.RegisterSingleton(factory => new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(factory.GetInstance()))); composition.GetCollectionBuilder() .Append(); composition.GetCollectionBuilder() .Add(() => composition.TypeLoader.GetTypes()); composition.RegisterSingleton(factory => new MigrationBuilder(factory)); // by default, register a noop factory composition.RegisterSingleton(); } internal void Initialize(IEnumerable mapperProfiles) { // mapper profiles have been registered & are created by the container Mapper.Initialize(configuration => { foreach (var profile in mapperProfiles) configuration.AddProfile(profile); }); // ensure we have some essential directories // every other component can then initialize safely IOHelper.EnsurePathExists("~/App_Data"); IOHelper.EnsurePathExists(SystemDirectories.Media); IOHelper.EnsurePathExists(SystemDirectories.MvcViews); IOHelper.EnsurePathExists(SystemDirectories.MvcViews + "/Partials"); IOHelper.EnsurePathExists(SystemDirectories.MvcViews + "/MacroPartials"); } } }