2016-07-28 19:29:30 +02:00
|
|
|
|
using System;
|
2016-08-13 16:02:35 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2016-08-24 19:30:33 +02:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2016-08-24 12:34:28 +02:00
|
|
|
|
using Umbraco.Core.Dictionary;
|
2016-11-03 10:31:44 +01:00
|
|
|
|
using Umbraco.Core.IO;
|
2016-08-25 10:23:41 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2016-08-23 11:06:48 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2016-12-16 17:56:10 +01:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2016-08-07 17:08:57 +02:00
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
2016-07-28 19:29:30 +02:00
|
|
|
|
using Umbraco.Core.Strings;
|
2016-08-23 11:17:08 +02:00
|
|
|
|
using Umbraco.Core.Sync;
|
2016-08-19 11:13:19 +02:00
|
|
|
|
using Umbraco.Core._Legacy.PackageActions;
|
2016-07-28 19:29:30 +02:00
|
|
|
|
|
2017-05-30 15:46:25 +02:00
|
|
|
|
namespace Umbraco.Core.Composing
|
2016-07-28 19:29:30 +02:00
|
|
|
|
{
|
2017-05-30 15:33:13 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides a static service locator for most singletons.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2018-07-20 15:45:01 +02:00
|
|
|
|
/// <para>This class is initialized with the container in UmbracoApplicationBase,
|
2017-05-30 15:33:13 +02:00
|
|
|
|
/// right after the container is created in UmbracoApplicationBase.HandleApplicationStart.</para>
|
|
|
|
|
|
/// <para>Obviously, this is a service locator, which some may consider an anti-pattern. And yet,
|
|
|
|
|
|
/// practically, it works.</para>
|
|
|
|
|
|
/// </remarks>
|
2016-07-28 19:29:30 +02:00
|
|
|
|
public static class Current
|
|
|
|
|
|
{
|
2018-06-16 13:13:29 +02:00
|
|
|
|
private static IContainer _container;
|
2016-07-28 19:29:30 +02:00
|
|
|
|
|
2017-05-30 15:33:13 +02:00
|
|
|
|
private static IShortStringHelper _shortStringHelper;
|
|
|
|
|
|
private static ILogger _logger;
|
|
|
|
|
|
private static IProfiler _profiler;
|
|
|
|
|
|
private static ProfilingLogger _profilingLogger;
|
2018-05-02 13:38:45 +02:00
|
|
|
|
private static IPublishedValueFallback _publishedValueFallback;
|
2017-05-30 15:33:13 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the DI container.
|
|
|
|
|
|
/// </summary>
|
2018-06-16 13:13:29 +02:00
|
|
|
|
internal static IContainer Container
|
2016-07-28 19:29:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2016-07-29 10:58:57 +02:00
|
|
|
|
if (_container == null) throw new Exception("No container has been set.");
|
|
|
|
|
|
return _container;
|
|
|
|
|
|
}
|
2016-09-01 19:06:08 +02:00
|
|
|
|
set
|
2016-07-29 10:58:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (_container != null) throw new Exception("A container has already been set.");
|
|
|
|
|
|
_container = value;
|
2016-07-28 19:29:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-29 10:58:57 +02:00
|
|
|
|
internal static bool HasContainer => _container != null;
|
|
|
|
|
|
|
|
|
|
|
|
// for UNIT TESTS exclusively!
|
2016-09-01 19:06:08 +02:00
|
|
|
|
// resets *everything* that is 'current'
|
2016-07-29 10:58:57 +02:00
|
|
|
|
internal static void Reset()
|
|
|
|
|
|
{
|
2018-08-30 19:08:55 +02:00
|
|
|
|
_container?.Dispose();
|
2016-07-29 10:58:57 +02:00
|
|
|
|
_container = null;
|
2016-08-25 10:23:41 +02:00
|
|
|
|
|
2016-08-24 19:30:33 +02:00
|
|
|
|
_shortStringHelper = null;
|
2016-08-25 10:23:41 +02:00
|
|
|
|
_logger = null;
|
|
|
|
|
|
_profiler = null;
|
2016-08-25 15:09:51 +02:00
|
|
|
|
_profilingLogger = null;
|
2018-05-02 13:38:45 +02:00
|
|
|
|
_publishedValueFallback = null;
|
2016-08-25 10:23:41 +02:00
|
|
|
|
|
2016-07-29 10:58:57 +02:00
|
|
|
|
Resetted?.Invoke(null, EventArgs.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static event EventHandler Resetted;
|
|
|
|
|
|
|
|
|
|
|
|
#region Getters
|
|
|
|
|
|
|
2016-09-01 11:25:00 +02:00
|
|
|
|
// fixme - refactor
|
2017-05-30 15:33:13 +02:00
|
|
|
|
// we don't want Umbraco to die because the container has not been properly initialized,
|
|
|
|
|
|
// for some too-important things such as IShortStringHelper or loggers, so if it's not
|
|
|
|
|
|
// registered we setup a default one. We should really refactor our tests so that it does
|
|
|
|
|
|
// not happen. Will do when we get rid of IShortStringHelper.
|
2016-09-01 11:25:00 +02:00
|
|
|
|
|
2017-05-30 15:33:13 +02:00
|
|
|
|
public static IShortStringHelper ShortStringHelper
|
2018-08-29 18:50:08 +02:00
|
|
|
|
=> _shortStringHelper ?? (_shortStringHelper = _container?.TryGetInstance<IShortStringHelper>()
|
2017-05-30 15:33:13 +02:00
|
|
|
|
?? new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(UmbracoConfig.For.UmbracoSettings())));
|
2016-09-01 11:25:00 +02:00
|
|
|
|
|
2017-05-30 15:33:13 +02:00
|
|
|
|
public static ILogger Logger
|
2018-08-29 18:50:08 +02:00
|
|
|
|
=> _logger ?? (_logger = _container?.TryGetInstance<ILogger>()
|
2017-05-30 15:33:13 +02:00
|
|
|
|
?? new DebugDiagnosticsLogger());
|
|
|
|
|
|
|
|
|
|
|
|
public static IProfiler Profiler
|
2018-08-29 18:50:08 +02:00
|
|
|
|
=> _profiler ?? (_profiler = _container?.TryGetInstance<IProfiler>()
|
2017-05-30 15:33:13 +02:00
|
|
|
|
?? new LogProfiler(Logger));
|
|
|
|
|
|
|
|
|
|
|
|
public static ProfilingLogger ProfilingLogger
|
2018-08-29 18:50:08 +02:00
|
|
|
|
=> _profilingLogger ?? (_profilingLogger = _container?.TryGetInstance<ProfilingLogger>())
|
2017-05-30 15:33:13 +02:00
|
|
|
|
?? new ProfilingLogger(Logger, Profiler);
|
|
|
|
|
|
|
|
|
|
|
|
public static IRuntimeState RuntimeState
|
|
|
|
|
|
=> Container.GetInstance<IRuntimeState>();
|
|
|
|
|
|
|
2017-05-30 15:56:27 +02:00
|
|
|
|
public static TypeLoader TypeLoader
|
2017-05-30 15:33:13 +02:00
|
|
|
|
=> Container.GetInstance<TypeLoader>();
|
2016-08-25 15:09:51 +02:00
|
|
|
|
|
2018-07-20 09:49:05 +02:00
|
|
|
|
public static IFileSystems FileSystems
|
|
|
|
|
|
=> Container.GetInstance<IFileSystems>();
|
2016-11-03 10:31:44 +01:00
|
|
|
|
|
2016-07-29 10:58:57 +02:00
|
|
|
|
public static UrlSegmentProviderCollection UrlSegmentProviders
|
2016-07-28 19:29:30 +02:00
|
|
|
|
=> Container.GetInstance<UrlSegmentProviderCollection>();
|
2016-07-29 10:58:57 +02:00
|
|
|
|
|
2016-08-13 16:02:35 +02:00
|
|
|
|
public static CacheRefresherCollection CacheRefreshers
|
|
|
|
|
|
=> Container.GetInstance<CacheRefresherCollection>();
|
|
|
|
|
|
|
2018-02-16 12:00:45 +01:00
|
|
|
|
public static DataEditorCollection DataEditors
|
|
|
|
|
|
=> Container.GetInstance<DataEditorCollection>();
|
|
|
|
|
|
|
2016-08-07 17:08:57 +02:00
|
|
|
|
public static PropertyEditorCollection PropertyEditors
|
|
|
|
|
|
=> Container.GetInstance<PropertyEditorCollection>();
|
|
|
|
|
|
|
2016-08-18 10:02:46 +02:00
|
|
|
|
public static ParameterEditorCollection ParameterEditors
|
|
|
|
|
|
=> Container.GetInstance<ParameterEditorCollection>();
|
|
|
|
|
|
|
2018-03-16 09:06:44 +01:00
|
|
|
|
internal static ManifestValueValidatorCollection ManifestValidators
|
|
|
|
|
|
=> Container.GetInstance<ManifestValueValidatorCollection>();
|
2016-08-18 10:19:33 +02:00
|
|
|
|
|
2016-08-19 11:13:19 +02:00
|
|
|
|
internal static PackageActionCollection PackageActions
|
|
|
|
|
|
=> Container.GetInstance<PackageActionCollection>();
|
|
|
|
|
|
|
2016-08-19 11:42:23 +02:00
|
|
|
|
internal static PropertyValueConverterCollection PropertyValueConverters
|
|
|
|
|
|
=> Container.GetInstance<PropertyValueConverterCollection>();
|
|
|
|
|
|
|
2017-09-26 14:57:50 +02:00
|
|
|
|
internal static IPublishedModelFactory PublishedModelFactory
|
|
|
|
|
|
=> Container.GetInstance<IPublishedModelFactory>();
|
2016-08-23 11:06:48 +02:00
|
|
|
|
|
2016-08-23 11:17:08 +02:00
|
|
|
|
public static IServerMessenger ServerMessenger
|
|
|
|
|
|
=> Container.GetInstance<IServerMessenger>();
|
|
|
|
|
|
|
2016-08-24 12:28:31 +02:00
|
|
|
|
public static IServerRegistrar ServerRegistrar
|
|
|
|
|
|
=> Container.GetInstance<IServerRegistrar>();
|
|
|
|
|
|
|
2016-08-24 12:34:28 +02:00
|
|
|
|
public static ICultureDictionaryFactory CultureDictionaryFactory
|
|
|
|
|
|
=> Container.GetInstance<ICultureDictionaryFactory>();
|
|
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
public static CacheHelper ApplicationCache
|
|
|
|
|
|
=> Container.GetInstance<CacheHelper>();
|
|
|
|
|
|
|
|
|
|
|
|
public static ServiceContext Services
|
|
|
|
|
|
=> Container.GetInstance<ServiceContext>();
|
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
public static IScopeProvider ScopeProvider
|
|
|
|
|
|
=> Container.GetInstance<IScopeProvider>();
|
|
|
|
|
|
|
2017-09-22 18:28:21 +02:00
|
|
|
|
public static ISqlContext SqlContext
|
|
|
|
|
|
=> Container.GetInstance<ISqlContext>();
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
2018-01-10 12:48:51 +01:00
|
|
|
|
public static IPublishedContentTypeFactory PublishedContentTypeFactory
|
|
|
|
|
|
=> Container.GetInstance<IPublishedContentTypeFactory>();
|
|
|
|
|
|
|
2018-05-02 13:38:45 +02:00
|
|
|
|
public static IPublishedValueFallback PublishedValueFallback
|
|
|
|
|
|
=> _publishedValueFallback ?? Container.GetInstance<IPublishedValueFallback>() ?? new NoopPublishedValueFallback();
|
|
|
|
|
|
|
2016-07-29 10:58:57 +02:00
|
|
|
|
#endregion
|
2016-07-28 19:29:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|