From df0f75c98f409ec186a084006d9223884dc2603e Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska Date: Tue, 17 Mar 2020 10:25:55 +0100 Subject: [PATCH] Creating a ClientDependencyComponent and Composer for taking out ConfigureClientDependency() --- .../Runtime/ClientDependencyComponent.cs | 83 +++++++++++++++++++ .../Runtime/ClientDependencyComposer.cs | 21 +++++ .../Runtime/WebInitialComponent.cs | 44 ---------- 3 files changed, 104 insertions(+), 44 deletions(-) create mode 100644 src/Umbraco.Web/Runtime/ClientDependencyComponent.cs create mode 100644 src/Umbraco.Web/Runtime/ClientDependencyComposer.cs diff --git a/src/Umbraco.Web/Runtime/ClientDependencyComponent.cs b/src/Umbraco.Web/Runtime/ClientDependencyComponent.cs new file mode 100644 index 0000000000..833c25e7d3 --- /dev/null +++ b/src/Umbraco.Web/Runtime/ClientDependencyComponent.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ClientDependency.Core.CompositeFiles.Providers; +using ClientDependency.Core.Config; +using Umbraco.Core; +using Umbraco.Core.Composing; +using Umbraco.Core.Configuration; +using Umbraco.Core.Hosting; +using Umbraco.Core.Runtime; +using Umbraco.Web.JavaScript; + +namespace Umbraco.Web.Runtime +{ + [ComposeAfter(typeof(WebInitialComponent))] + public sealed class ClientDependencyComponent : IComponent + { + private readonly IHostingSettings _hostingSettings; + private readonly IHostingEnvironment _hostingEnvironment; + private readonly IRuntimeSettings _settings; + + public ClientDependencyComponent( + IHostingSettings hostingSettings, + IHostingEnvironment hostingEnvironment, + IRuntimeSettings settings, + IRuntimeMinifier runtimeMinifier) + { + _hostingSettings = hostingSettings; + _hostingEnvironment = hostingEnvironment; + _settings = settings; + } + + public void Initialize() + { + // anything else? + + if (_hostingEnvironment.IsHosted) + { + ConfigureClientDependency(); + } + } + + private void ConfigureClientDependency() + { + // Backwards compatibility - set the path and URL type for ClientDependency 1.5.1 [LK] + XmlFileMapper.FileMapDefaultFolder = Core.Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "ClientDependency"; + BaseCompositeFileProcessingProvider.UrlTypeDefault = CompositeUrlType.Base64QueryStrings; + + // Now we need to detect if we are running 'Umbraco.Core.LocalTempStorage' as EnvironmentTemp and in that case we want to change the CDF file + // location to be there + if (_hostingSettings.LocalTempStorageLocation == LocalTempStorage.EnvironmentTemp) + { + var cachePath = _hostingEnvironment.LocalTempPath; + + //set the file map and composite file default location to the %temp% location + BaseCompositeFileProcessingProvider.CompositeFilePathDefaultFolder + = XmlFileMapper.FileMapDefaultFolder + = Path.Combine(cachePath, "ClientDependency"); + } + + if (_settings.MaxQueryStringLength.HasValue || _settings.MaxRequestLength.HasValue) + { + //set the max url length for CDF to be the smallest of the max query length, max request length + ClientDependency.Core.CompositeFiles.CompositeDependencyHandler.MaxHandlerUrlLength = Math.Min(_settings.MaxQueryStringLength.GetValueOrDefault(), _settings.MaxRequestLength.GetValueOrDefault()); + } + + //Register a custom renderer - used to process property editor dependencies + var renderer = new DependencyPathRenderer(); + renderer.Initialize("Umbraco.DependencyPathRenderer", new NameValueCollection + { + { "compositeFileHandlerPath", ClientDependencySettings.Instance.CompositeFileHandlerPath } + }); + + ClientDependencySettings.Instance.MvcRendererCollection.Add(renderer); + } + public void Terminate() + { } + } +} diff --git a/src/Umbraco.Web/Runtime/ClientDependencyComposer.cs b/src/Umbraco.Web/Runtime/ClientDependencyComposer.cs new file mode 100644 index 0000000000..0e52a04cfe --- /dev/null +++ b/src/Umbraco.Web/Runtime/ClientDependencyComposer.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Umbraco.Core; +using Umbraco.Core.Composing; +using Umbraco.Core.Runtime; +using Umbraco.Web.JavaScript; + +namespace Umbraco.Web.Runtime +{ + public sealed class ClientDependencyComposer : ComponentComposer + { + public override void Compose(Composition composition) + { + base.Compose(composition); + composition.RegisterUnique(); + } + } +} diff --git a/src/Umbraco.Web/Runtime/WebInitialComponent.cs b/src/Umbraco.Web/Runtime/WebInitialComponent.cs index 8f07ab4837..c35b1bc783 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComponent.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComponent.cs @@ -7,8 +7,6 @@ using System.Web.Http; using System.Web.Http.Dispatcher; using System.Web.Mvc; using System.Web.Routing; -using ClientDependency.Core.CompositeFiles.Providers; -using ClientDependency.Core.Config; using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Core.Configuration; @@ -16,7 +14,6 @@ using Umbraco.Core.Hosting; using Umbraco.Core.Strings; using Umbraco.Core.IO; using Umbraco.Web.Install; -using Umbraco.Web.JavaScript; using Umbraco.Web.Mvc; using Umbraco.Web.WebApi; @@ -64,13 +61,6 @@ namespace Umbraco.Web.Runtime // setup mvc and webapi services SetupMvcAndWebApi(); - // When using a non-web runtime and this component is loaded ClientDependency explodes because it'll - // want to access HttpContext.Current, which doesn't exist - if (_hostingEnvironment.IsHosted) - { - ConfigureClientDependency(); - } - // Disable the X-AspNetMvc-Version HTTP Header MvcHandler.DisableMvcResponseHeader = true; @@ -129,40 +119,6 @@ namespace Umbraco.Web.Runtime new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration)); } - private void ConfigureClientDependency() - { - // Backwards compatibility - set the path and URL type for ClientDependency 1.5.1 [LK] - XmlFileMapper.FileMapDefaultFolder = Core.Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "ClientDependency"; - BaseCompositeFileProcessingProvider.UrlTypeDefault = CompositeUrlType.Base64QueryStrings; - - // Now we need to detect if we are running 'Umbraco.Core.LocalTempStorage' as EnvironmentTemp and in that case we want to change the CDF file - // location to be there - if (_hostingSettings.LocalTempStorageLocation == LocalTempStorage.EnvironmentTemp) - { - var cachePath = _hostingEnvironment.LocalTempPath; - - //set the file map and composite file default location to the %temp% location - BaseCompositeFileProcessingProvider.CompositeFilePathDefaultFolder - = XmlFileMapper.FileMapDefaultFolder - = Path.Combine(cachePath, "ClientDependency"); - } - - if (_settings.MaxQueryStringLength.HasValue || _settings.MaxRequestLength.HasValue) - { - //set the max url length for CDF to be the smallest of the max query length, max request length - ClientDependency.Core.CompositeFiles.CompositeDependencyHandler.MaxHandlerUrlLength = Math.Min(_settings.MaxQueryStringLength.GetValueOrDefault(), _settings.MaxRequestLength.GetValueOrDefault()); - } - - //Register a custom renderer - used to process property editor dependencies - var renderer = new DependencyPathRenderer(); - renderer.Initialize("Umbraco.DependencyPathRenderer", new NameValueCollection - { - { "compositeFileHandlerPath", ClientDependencySettings.Instance.CompositeFileHandlerPath } - }); - - ClientDependencySettings.Instance.MvcRendererCollection.Add(renderer); - } - // internal for tests internal static void CreateRoutes( IUmbracoContextAccessor umbracoContextAccessor,