2019-01-23 21:22:49 +01:00
using System ;
using System.Collections.Generic ;
2021-01-05 16:27:42 +11:00
using System.ComponentModel ;
2019-01-23 21:22:49 +01:00
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
{
/// <summary>
/// Provides static options for the runtime.
/// </summary>
/// <remarks>
/// These options can be configured in PreApplicationStart or via appSettings.
/// </remarks>
public static class RuntimeOptions
{
private static List < Action < IProfilingLogger > > _onBoot ;
private static List < Action < Composition , AppCaches , TypeLoader , IUmbracoDatabaseFactory > > _onEssentials ;
private static bool? _installMissingDatabase ;
private static bool? _installEmptyDatabase ;
2020-12-08 10:38:32 +01:00
private static bool? _installUnattended ;
2020-12-08 09:06:28 +01:00
private static bool? _upgradeUnattended ;
2019-01-23 21:22:49 +01:00
// reads a boolean appSetting
private static bool BoolSetting ( string key , bool missing ) = > ConfigurationManager . AppSettings [ key ] ? . InvariantEquals ( "true" ) ? ? missing ;
/// <summary>
/// Gets a value indicating whether the runtime should enter Install level when the database is missing.
/// </summary>
/// <remarks>
/// <para>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.</para>
/// <para>It is then up to the implementor, that is setting this value, to take over the installation
/// sequence.</para>
/// </remarks>
public static bool InstallMissingDatabase
{
2020-12-08 10:38:32 +01:00
get = > _installMissingDatabase ? ? BoolSetting ( "Umbraco.Core.RuntimeState.InstallMissingDatabase" , false ) ;
set = > _installMissingDatabase = value ;
2019-01-23 21:22:49 +01:00
}
2021-01-05 16:27:42 +11:00
[Obsolete("This setting is no longer used and will be removed in future versions. If a database connection string is configured and the database is empty Umbraco will be installed during the installation sequence.")]
[EditorBrowsable(EditorBrowsableState.Never)]
2019-01-23 21:22:49 +01:00
public static bool InstallEmptyDatabase
{
2020-12-16 11:03:23 +01:00
get = > _installEmptyDatabase ? ? BoolSetting ( "Umbraco.Core.RuntimeState.InstallEmptyDatabase" , true ) ;
2020-12-08 10:38:32 +01:00
set = > _installEmptyDatabase = value ;
}
/// <summary>
/// Gets a value indicating whether unattended installs are enabled.
/// </summary>
/// <remarks>
/// <para>By default, when a database connection string is configured and it is possible to connect to
2020-12-16 11:03:23 +01:00
/// the database, but the database is empty, the runtime enters the <c>Install</c> level.
/// If this option is set to <c>true</c> an unattended install will be performed and the runtime enters
/// the <c>Run</c> level.</para>
2020-12-08 10:38:32 +01:00
/// </remarks>
public static bool InstallUnattended
{
2020-12-16 11:03:23 +01:00
get = > _installUnattended ? ? BoolSetting ( "Umbraco.Core.RuntimeState.InstallUnattended" , false ) ;
2020-12-08 10:38:32 +01:00
set = > _installUnattended = value ;
2019-01-23 21:22:49 +01:00
}
2020-12-08 09:06:28 +01:00
/// <summary>
/// Gets a value indicating whether unattended upgrade is enabled.
/// </summary>
public static bool UpgradeUnattended
{
get = > _upgradeUnattended ? ? BoolSetting ( "Umbraco.Core.RuntimeState.UpgradeUnattended" , false ) ;
set = > _upgradeUnattended = value ;
}
2019-01-23 21:22:49 +01:00
/// <summary>
/// Executes the RuntimeBoot handlers.
/// </summary>
internal static void DoRuntimeBoot ( IProfilingLogger logger )
{
if ( _onBoot = = null )
return ;
foreach ( var action in _onBoot )
action ( logger ) ;
}
/// <summary>
/// Executes the RuntimeEssentials handlers.
/// </summary>
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 ) ;
}
/// <summary>
/// Registers a RuntimeBoot handler.
/// </summary>
/// <remarks>
/// <para>A RuntimeBoot handler runs when the runtime boots, right after the
/// loggers have been created, but before anything else.</para>
/// </remarks>
public static void OnRuntimeBoot ( Action < IProfilingLogger > action )
{
if ( _onBoot = = null )
_onBoot = new List < Action < IProfilingLogger > > ( ) ;
_onBoot . Add ( action ) ;
}
/// <summary>
/// Registers a RuntimeEssentials handler.
/// </summary>
/// <remarks>
/// <para>A RuntimeEssentials handler runs after the runtime has created a few
/// essential things (AppCaches, a TypeLoader, and a database factory) but
/// before anything else.</para>
/// </remarks>
public static void OnRuntimeEssentials ( Action < Composition , AppCaches , TypeLoader , IUmbracoDatabaseFactory > action )
{
if ( _onEssentials = = null )
_onEssentials = new List < Action < Composition , AppCaches , TypeLoader , IUmbracoDatabaseFactory > > ( ) ;
_onEssentials . Add ( action ) ;
}
}
}