diff --git a/src/Umbraco.Web/ContextualPublishedCacheExtensions.cs b/src/Umbraco.Web/ContextualPublishedCacheExtensions.cs new file mode 100644 index 0000000000..2a75c79c14 --- /dev/null +++ b/src/Umbraco.Web/ContextualPublishedCacheExtensions.cs @@ -0,0 +1,63 @@ +using System.Linq; +using Umbraco.Core.Dynamics; +using Umbraco.Core.Xml; +using Umbraco.Web.Models; +using Umbraco.Web.PublishedCache; + +namespace Umbraco.Web +{ + /// + /// Provides extension methods to ContextualPublishedCache. + /// + internal static class ContextualPublishedCacheExtensions + { + /// + /// Gets a dynamic content identified by its unique identifier. + /// + /// The contextual cache. + /// The content unique identifier. + /// The dynamic content, or null. + public static dynamic GetDynamicById(this ContextualPublishedContentCache cache, int contentId) + { + var content = cache.GetById(contentId); + return content == null ? new DynamicNull() : new DynamicPublishedContent(content).AsDynamic(); + } + + /// + /// Gets a dynamic content resulting from an XPath query. + /// + /// The contextual cache. + /// The XPath query. + /// Optional XPath variables + /// The dynamic content, or null. + public static dynamic GetDynamicSingleByXPath(this ContextualPublishedContentCache cache, string xpath, params XPathVariable[] vars) + { + var content = cache.GetSingleByXPath(xpath, vars); + return content == null ? new DynamicNull() : new DynamicPublishedContent(content).AsDynamic(); + } + + /// + /// Gets dynamic contents resulting from an XPath query. + /// + /// The contextual cache. + /// The XPath query. + /// Optional XPath variables + /// The dynamic contents. + public static dynamic GetDynamicByXPath(this ContextualPublishedContentCache cache, string xpath, params XPathVariable[] vars) + { + var content = cache.GetByXPath(xpath, vars); + return new DynamicPublishedContentList(content.Select(c => new DynamicPublishedContent(c))); + } + + /// + /// Gets dynamic contents at root. + /// + /// The contextual cache. + /// The dynamic contents. + public static dynamic GetDynamicAtRoot(this ContextualPublishedContentCache cache) + { + var content = cache.GetAtRoot(); + return new DynamicPublishedContentList(content.Select(c => new DynamicPublishedContent(c))); + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 2c04005a20..ebfc2d7aed 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -320,6 +320,7 @@ +