-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPagesAppConfigurationExtensions.cs
More file actions
122 lines (106 loc) · 5.1 KB
/
Copy pathPagesAppConfigurationExtensions.cs
File metadata and controls
122 lines (106 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Interfaces;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Request;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Serialization;
using Sitecore.AspNetCore.SDK.Pages.Configuration;
using Sitecore.AspNetCore.SDK.Pages.GraphQL;
using Sitecore.AspNetCore.SDK.Pages.Middleware;
using Sitecore.AspNetCore.SDK.Pages.Request.Handlers.GraphQL;
using Sitecore.AspNetCore.SDK.RenderingEngine.Configuration;
using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions;
using Sitecore.AspNetCore.SDK.RenderingEngine.Interfaces;
namespace Sitecore.AspNetCore.SDK.Pages.Extensions;
/// <summary>
/// Configuration helpers for Pages functionality.
/// </summary>
public static class PagesAppConfigurationExtensions
{
/// <summary>
/// Registers the Sitecore Experience Editor middleware into the <see cref="IApplicationBuilder"/>.
/// </summary>
/// <param name="app">The instance of the <see cref="IApplicationBuilder"/> to extend.</param>
/// <returns>The <see cref="IApplicationBuilder"/> so that additional calls can be chained.</returns>
public static IApplicationBuilder UseSitecorePages(this IApplicationBuilder app)
{
ArgumentNullException.ThrowIfNull(app);
object? experienceEditorMarker = app.ApplicationServices.GetService(typeof(PagesMarkerService));
if (experienceEditorMarker != null)
{
app.UseMiddleware<PageSetupMiddleware>();
app.UseMiddleware<PagesRenderMiddleware>();
}
return app;
}
/// <summary>
/// Adds the Sitecore Experience Editor support services to the <see cref="IServiceCollection" />.
/// </summary>
/// <param name="serviceBuilder">The <see cref="ISitecoreRenderingEngineBuilder" /> to add services to.</param>
/// <param name="contextId">The ContextId for the environment being used.</param>
/// <param name="options">Configures the <see cref="PagesOptions" /> options.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static ISitecoreRenderingEngineBuilder WithSitecorePages(this ISitecoreRenderingEngineBuilder serviceBuilder, string contextId, Action<PagesOptions>? options = null)
{
ArgumentNullException.ThrowIfNull(serviceBuilder);
IServiceCollection services = serviceBuilder.Services;
if (services.Any(s => s.ServiceType == typeof(PagesMarkerService)))
{
return serviceBuilder;
}
services.AddSingleton<PagesMarkerService>();
services.AddSingleton<IGraphQLClientFactory>(new GraphQLClientFactory(contextId));
if (options != null)
{
services.Configure(options);
}
services.Configure((Action<RenderingEngineOptions>)(renderingOptions =>
{
renderingOptions.MapToRequest((httpRequest, layoutRequest) =>
{
MapRequest(httpRequest, layoutRequest, "mode");
MapRequest(httpRequest, layoutRequest, "sc_itemid");
MapRequest(httpRequest, layoutRequest, "sc_version");
MapRequest(httpRequest, layoutRequest, "sc_lang");
MapRequest(httpRequest, layoutRequest, "sc_site");
MapRequest(httpRequest, layoutRequest, "sc_layoutKind");
MapRequest(httpRequest, layoutRequest, "secret");
MapRequest(httpRequest, layoutRequest, "tenant_id");
MapRequest(httpRequest, layoutRequest, "route");
});
}));
return serviceBuilder;
}
/// <summary>
/// Registers an HTTP request handler for the Sitecore layout service client.
/// </summary>
/// <param name="builder">The <see cref="ISitecoreLayoutClientBuilder"/> to configure.</param>
/// <returns>The <see cref="ILayoutRequestHandlerBuilder{HttpLayoutRequestHandler}"/> so that additional calls can be chained.</returns>
public static ISitecoreLayoutClientBuilder AddSitecorePagesHandler(
this ISitecoreLayoutClientBuilder builder)
{
string name = Constants.LayoutClients.Pages;
builder.AddHandler(name, sp
=> ActivatorUtilities.CreateInstance<GraphQLEditingServiceHandler>(
sp,
sp.GetRequiredService<IGraphQLClientFactory>(),
sp.GetRequiredService<ISitecoreLayoutSerializer>(),
sp.GetRequiredService<ILogger<GraphQLEditingServiceHandler>>()));
return builder;
}
private static void MapRequest(HttpRequest httpRequest, SitecoreLayoutRequest layoutRequest, string paramName)
{
if (httpRequest.Query == null || !httpRequest.Query.ContainsKey(paramName))
{
return;
}
string[]? modeQueryValue = httpRequest.Query[paramName];
if (modeQueryValue == null)
{
return;
}
layoutRequest.AddHeader(paramName, modeQueryValue);
}
}