From b52f234c0295cd665b3e55331fa04e103fc7387a Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 12 Mar 2015 15:01:25 +0100 Subject: [PATCH] U4-3753 - refactor --- src/Umbraco.Web/Models/ContentExtensions.cs | 29 ++++++++++++++++--- src/Umbraco.Web/PublishedContentExtensions.cs | 26 ++++------------- src/Umbraco.Web/Umbraco.Web.csproj | 1 + 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/Umbraco.Web/Models/ContentExtensions.cs b/src/Umbraco.Web/Models/ContentExtensions.cs index 85f1a3033d..dc12a90250 100644 --- a/src/Umbraco.Web/Models/ContentExtensions.cs +++ b/src/Umbraco.Web/Models/ContentExtensions.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Linq; using Umbraco.Core; using Umbraco.Core.Models; +using Umbraco.Core.Services; using Umbraco.Web.Routing; namespace Umbraco.Web.Models @@ -18,10 +19,30 @@ namespace Umbraco.Web.Models /// The culture that would be selected to render the content. public static CultureInfo GetCulture(this IContent content, Uri current = null) { - var route = UmbracoContext.Current.ContentCache.GetRouteById(content.Id); // cached + return GetCulture(UmbracoContext.Current, + ApplicationContext.Current.Services.DomainService, ApplicationContext.Current.Services.LocalizationService, + content.Id, content.Path, + current); + } + + /// + /// Gets the culture that would be selected to render a specified content, + /// within the context of a specified current request. + /// + /// An instance. + /// An implementation. + /// An implementation. + /// The content identifier. + /// The content path. + /// The request Uri. + /// The culture that would be selected to render the content. + public static CultureInfo GetCulture(UmbracoContext umbracoContext, IDomainService domainService, ILocalizationService localizationService, + int contentId, string contentPath, Uri current) + { + var route = umbracoContext.ContentCache.GetRouteById(contentId); // cached var pos = route.IndexOf('/'); - var domainHelper = new DomainHelper(ApplicationContext.Current.Services.DomainService); + var domainHelper = new DomainHelper(domainService); var domain = pos == 0 ? null @@ -29,11 +50,11 @@ namespace Umbraco.Web.Models if (domain == null) { - var defaultLanguage = ApplicationContext.Current.Services.LocalizationService.GetAllLanguages().FirstOrDefault(); + var defaultLanguage = localizationService.GetAllLanguages().FirstOrDefault(); return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode); } - var wcDomain = DomainHelper.FindWildcardDomainInPath(ApplicationContext.Current.Services.DomainService.GetAll(true), content.Path, domain.RootContent.Id); + var wcDomain = DomainHelper.FindWildcardDomainInPath(domainService.GetAll(true), contentPath, domain.RootContent.Id); return wcDomain == null ? new CultureInfo(domain.Language.IsoCode) : new CultureInfo(wcDomain.Language.IsoCode); diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index 923edf50c2..7302913d34 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -7,6 +7,7 @@ using System.Web; using Examine.LuceneEngine.SearchCriteria; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Services; using Umbraco.Web.Models; using Umbraco.Core; using Umbraco.Web.Routing; @@ -1896,27 +1897,12 @@ namespace Umbraco.Web /// The culture that would be selected to render the content. public static CultureInfo GetCulture(this IPublishedContent content, Uri current = null) { - var route = UmbracoContext.Current.ContentCache.GetRouteById(content.Id); // cached - var pos = route.IndexOf('/'); - - var domainHelper = new DomainHelper(ApplicationContext.Current.Services.DomainService); - - var domain = pos == 0 - ? null - : domainHelper.DomainForNode(int.Parse(route.Substring(0, pos)), current).UmbracoDomain; - - if (domain == null) - { - var defaultLanguage = ApplicationContext.Current.Services.LocalizationService.GetAllLanguages().FirstOrDefault(); - return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode); - } - - var wcDomain = DomainHelper.FindWildcardDomainInPath(ApplicationContext.Current.Services.DomainService.GetAll(true), content.Path, domain.RootContent.Id); - return wcDomain == null - ? new CultureInfo(domain.Language.IsoCode) - : new CultureInfo(wcDomain.Language.IsoCode); + return Models.ContentExtensions.GetCulture(UmbracoContext.Current, + ApplicationContext.Current.Services.DomainService, ApplicationContext.Current.Services.LocalizationService, + content.Id, content.Path, + current); } - + #endregion } } diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 1ce693b27a..a5084b6a31 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -281,6 +281,7 @@ +