2020-11-03 14:14:01 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Logging;
|
2020-11-03 14:14:01 +01:00
|
|
|
|
2022-05-06 15:06:39 +02:00
|
|
|
namespace Umbraco.Cms.Web.Website.ViewEngines;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Wraps all view engines with a <see cref="ProfilingViewEngine" />
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ProfilingViewEngineWrapperMvcViewOptionsSetup : IConfigureOptions<MvcViewOptions>
|
2020-11-03 14:14:01 +01:00
|
|
|
{
|
2022-05-06 15:06:39 +02:00
|
|
|
private readonly IProfiler _profiler;
|
|
|
|
|
|
2021-01-04 15:43:30 +11:00
|
|
|
/// <summary>
|
2022-05-06 15:06:39 +02:00
|
|
|
/// Initializes a new instance of the <see cref="ProfilingViewEngineWrapperMvcViewOptionsSetup" /> class.
|
2021-01-04 15:43:30 +11:00
|
|
|
/// </summary>
|
2022-05-06 15:06:39 +02:00
|
|
|
/// <param name="profiler">The <see cref="IProfiler" /></param>
|
|
|
|
|
public ProfilingViewEngineWrapperMvcViewOptionsSetup(IProfiler profiler) =>
|
|
|
|
|
_profiler = profiler ?? throw new ArgumentNullException(nameof(profiler));
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Configure(MvcViewOptions options)
|
2020-11-03 14:14:01 +01:00
|
|
|
{
|
2022-05-06 15:06:39 +02:00
|
|
|
if (options == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(options));
|
|
|
|
|
}
|
2020-11-03 14:14:01 +01:00
|
|
|
|
2022-05-06 15:06:39 +02:00
|
|
|
WrapViewEngines(options.ViewEngines);
|
|
|
|
|
}
|
2020-11-03 14:14:01 +01:00
|
|
|
|
2022-05-06 15:06:39 +02:00
|
|
|
private void WrapViewEngines(IList<IViewEngine> viewEngines)
|
|
|
|
|
{
|
|
|
|
|
if (viewEngines.Count == 0)
|
2020-11-03 14:14:01 +01:00
|
|
|
{
|
2022-05-06 15:06:39 +02:00
|
|
|
return;
|
2020-11-03 14:14:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-06 15:06:39 +02:00
|
|
|
var originalEngines = viewEngines.ToList();
|
|
|
|
|
viewEngines.Clear();
|
|
|
|
|
foreach (IViewEngine engine in originalEngines)
|
2020-11-03 14:14:01 +01:00
|
|
|
{
|
2022-05-06 15:06:39 +02:00
|
|
|
IViewEngine wrappedEngine =
|
|
|
|
|
engine is ProfilingViewEngine ? engine : new ProfilingViewEngine(engine, _profiler);
|
|
|
|
|
viewEngines.Add(wrappedEngine);
|
2020-11-03 14:14:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|