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))); } } }