2021-02-18 11:06:02 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2021-01-14 23:14:35 +11:00
|
|
|
using System;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
2019-02-22 19:13:25 +01:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Extensions
|
2019-02-22 19:13:25 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides extension methods for <see cref="IPublishedModelFactory"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class PublishedModelFactoryExtensions
|
|
|
|
|
{
|
2020-04-20 23:22:03 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if the current <see cref="IPublishedModelFactory"/> is an implementation of <see cref="ILivePublishedModelFactory2"/> and is enabled
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsLiveFactoryEnabled(this IPublishedModelFactory factory)
|
|
|
|
|
{
|
2021-05-26 07:36:21 +01:00
|
|
|
if (factory is IAutoPublishedModelFactory liveFactory)
|
2021-01-14 23:14:35 +11:00
|
|
|
{
|
2020-05-01 14:08:29 +02:00
|
|
|
return liveFactory.Enabled;
|
2021-01-14 23:14:35 +11:00
|
|
|
}
|
2020-04-20 23:22:03 +10:00
|
|
|
|
2020-05-01 14:08:29 +02:00
|
|
|
// if it's not ILivePublishedModelFactory we can't determine if it's enabled or not so return true
|
|
|
|
|
return true;
|
2020-04-20 23:22:03 +10:00
|
|
|
}
|
|
|
|
|
|
2019-09-16 14:50:05 +10:00
|
|
|
/// <summary>
|
2021-05-26 07:36:21 +01:00
|
|
|
/// Sets a flag to reset the ModelsBuilder models if the <see cref="IPublishedModelFactory"/> is <see cref="IAutoPublishedModelFactory"/>
|
2019-09-16 14:50:05 +10:00
|
|
|
/// </summary>
|
2019-09-16 17:23:00 +10:00
|
|
|
/// <remarks>
|
2021-05-26 07:36:21 +01:00
|
|
|
/// This does not recompile the InMemory models, only sets a flag to tell models builder to recompile when they are requested.
|
2019-09-16 17:23:00 +10:00
|
|
|
/// </remarks>
|
|
|
|
|
internal static void WithSafeLiveFactoryReset(this IPublishedModelFactory factory, Action action)
|
2019-09-16 14:50:05 +10:00
|
|
|
{
|
2021-05-26 07:36:21 +01:00
|
|
|
if (factory is IAutoPublishedModelFactory liveFactory)
|
2019-09-16 14:50:05 +10:00
|
|
|
{
|
|
|
|
|
lock (liveFactory.SyncRoot)
|
|
|
|
|
{
|
2020-05-01 14:08:29 +02:00
|
|
|
liveFactory.Reset();
|
2019-09-16 17:23:00 +10:00
|
|
|
|
2019-09-16 14:50:05 +10:00
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 19:13:25 +01:00
|
|
|
}
|
|
|
|
|
}
|