using System; using System.Collections.Generic; using System.Configuration; using System.Runtime.CompilerServices; using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; namespace Umbraco.Core { /// /// Provides static options for the runtime. /// /// /// These options can be configured in PreApplicationStart or via appSettings. /// public static class RuntimeOptions { private static List> _onBoot; private static List> _onEssentials; private static bool? _installMissingDatabase; private static bool? _installEmptyDatabase; // reads a boolean appSetting private static bool BoolSetting(string key, bool missing) => ConfigurationManager.AppSettings[key]?.InvariantEquals("true") ?? missing; /// /// Gets a value indicating whether the runtime should enter Install level when the database is missing. /// /// /// By default, when a database connection string is configured but it is not possible to /// connect to the database, the runtime enters the BootFailed level. If this options is set to true, /// it enters the Install level instead. /// It is then up to the implementor, that is setting this value, to take over the installation /// sequence. /// public static bool InstallMissingDatabase { get => _installEmptyDatabase ?? BoolSetting("Umbraco.Core.RuntimeState.InstallMissingDatabase", false); set => _installEmptyDatabase = value; } /// /// Gets a value indicating whether the runtime should enter Install level when the database is empty. /// /// /// By default, when a database connection string is configured and it is possible to connect to /// the database, but the database is empty, the runtime enters the BootFailed level. If this options /// is set to true, it enters the Install level instead. /// It is then up to the implementor, that is setting this value, to take over the installation /// sequence. /// public static bool InstallEmptyDatabase { get => _installMissingDatabase ?? BoolSetting("Umbraco.Core.RuntimeState.InstallEmptyDatabase", false); set => _installMissingDatabase = value; } /// /// Executes the RuntimeBoot handlers. /// internal static void DoRuntimeBoot(IProfilingLogger logger) { if (_onBoot == null) return; foreach (var action in _onBoot) action(logger); } /// /// Executes the RuntimeEssentials handlers. /// internal static void DoRuntimeEssentials(Composition composition, AppCaches appCaches, TypeLoader typeLoader, IUmbracoDatabaseFactory databaseFactory) { if (_onEssentials== null) return; foreach (var action in _onEssentials) action(composition, appCaches, typeLoader, databaseFactory); } /// /// Registers a RuntimeBoot handler. /// /// /// A RuntimeBoot handler runs when the runtime boots, right after the /// loggers have been created, but before anything else. /// public static void OnRuntimeBoot(Action action) { if (_onBoot == null) _onBoot = new List>(); _onBoot.Add(action); } /// /// Registers a RuntimeEssentials handler. /// /// /// A RuntimeEssentials handler runs after the runtime has created a few /// essential things (AppCaches, a TypeLoader, and a database factory) but /// before anything else. /// public static void OnRuntimeEssentials(Action action) { if (_onEssentials == null) _onEssentials = new List>(); _onEssentials.Add(action); } } }