Introduced interface on UrlProvider (Can't be named IUrlProvider)

This commit is contained in:
Bjarke Berg
2020-02-09 20:20:13 +01:00
parent 2658dae649
commit 794e2ccdac
5 changed files with 120 additions and 9 deletions

View File

@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Web.Routing
{
public interface IPublishedUrlProvider
{
/// <summary>
/// Gets or sets the provider url mode.
/// </summary>
UrlMode Mode { get; set; }
UrlMode GetMode(bool absolute);
IPublishedContent GetDocument(int id);
IPublishedContent GetDocument(Guid id);
IPublishedContent GetMedia(Guid id);
/// <summary>
/// Gets the url of a published content.
/// </summary>
/// <param name="id">The published content identifier.</param>
/// <param name="mode">The url mode.</param>
/// <param name="culture">A culture.</param>
/// <param name="current">The current absolute url.</param>
/// <returns>The url for the published content.</returns>
string GetUrl(Guid id, UrlMode mode = UrlMode.Default, string culture = null, Uri current = null);
/// <summary>
/// Gets the url of a published content.
/// </summary>
/// <param name="id">The published content identifier.</param>
/// <param name="mode">The url mode.</param>
/// <param name="culture">A culture.</param>
/// <param name="current">The current absolute url.</param>
/// <returns>The url for the published content.</returns>
string GetUrl(int id, UrlMode mode = UrlMode.Default, string culture = null, Uri current = null);
/// <summary>
/// Gets the url of a published content.
/// </summary>
/// <param name="content">The published content.</param>
/// <param name="mode">The url mode.</param>
/// <param name="culture">A culture.</param>
/// <param name="current">The current absolute url.</param>
/// <returns>The url for the published content.</returns>
/// <remarks>
/// <para>The url is absolute or relative depending on <c>mode</c> and on <c>current</c>.</para>
/// <para>If the published content is multi-lingual, gets the url for the specified culture or,
/// when no culture is specified, the current culture.</para>
/// <para>If the provider is unable to provide a url, it returns "#".</para>
/// </remarks>
string GetUrl(IPublishedContent content, UrlMode mode = UrlMode.Default, string culture = null, Uri current = null);
string GetUrlFromRoute(int id, string route, string culture);
/// <summary>
/// Gets the other urls of a published content.
/// </summary>
/// <param name="id">The published content id.</param>
/// <returns>The other urls for the published content.</returns>
/// <remarks>
/// <para>Other urls are those that <c>GetUrl</c> would not return in the current context, but would be valid
/// urls for the node in other contexts (different domain for current request, umbracoUrlAlias...).</para>
/// <para>The results depend on the current url.</para>
/// </remarks>
IEnumerable<UrlInfo> GetOtherUrls(int id);
/// <summary>
/// Gets the other urls of a published content.
/// </summary>
/// <param name="id">The published content id.</param>
/// <param name="current">The current absolute url.</param>
/// <returns>The other urls for the published content.</returns>
/// <remarks>
/// <para>Other urls are those that <c>GetUrl</c> would not return in the current context, but would be valid
/// urls for the node in other contexts (different domain for current request, umbracoUrlAlias...).</para>
/// </remarks>
IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current);
/// <summary>
/// Gets the url of a media item.
/// </summary>
/// <param name="id"></param>
/// <param name="mode"></param>
/// <param name="culture"></param>
/// <param name="propertyAlias"></param>
/// <param name="current"></param>
/// <returns></returns>
string GetMediaUrl(Guid id, UrlMode mode = UrlMode.Default, string culture = null, string propertyAlias = Constants.Conventions.Media.File, Uri current = null);
/// <summary>
/// Gets the url of a media item.
/// </summary>
/// <param name="content">The published content.</param>
/// <param name="propertyAlias">The property alias to resolve the url from.</param>
/// <param name="mode">The url mode.</param>
/// <param name="culture">The variation language.</param>
/// <param name="current">The current absolute url.</param>
/// <returns>The url for the media.</returns>
/// <remarks>
/// <para>The url is absolute or relative depending on <c>mode</c> and on <c>current</c>.</para>
/// <para>If the media is multi-lingual, gets the url for the specified culture or,
/// when no culture is specified, the current culture.</para>
/// <para>If the provider is unable to provide a url, it returns <see cref="String.Empty"/>.</para>
/// </remarks>
string GetMediaUrl(IPublishedContent content, UrlMode mode = UrlMode.Default, string culture = null, string propertyAlias = Constants.Conventions.Media.File, Uri current = null);
}
}

View File

@@ -65,7 +65,7 @@ namespace Umbraco.Web
/// <summary>
/// Gets the url provider.
/// </summary>
UrlProvider UrlProvider { get; }
IPublishedUrlProvider UrlProvider { get; }
/// <summary>
/// Gets/sets the PublishedRequest object

View File

@@ -8,15 +8,16 @@ using Umbraco.Web.Composing;
namespace Umbraco.Web.Routing
{
/// <summary>
/// Provides urls.
/// </summary>
public class UrlProvider
public class UrlProvider : IPublishedUrlProvider
{
#region Ctor and configuration
/// <summary>
/// Initializes a new instance of the <see cref="UrlProvider"/> class with an Umbraco context and a list of url providers.
/// InitialiIUrlProviderzes a new instance of the <see cref="UrlProvider"/> class with an Umbraco context and a list of url providers.
/// </summary>
/// <param name="umbracoContext">The Umbraco context.</param>
/// <param name="routingSettings">Routing settings.</param>
@@ -72,10 +73,10 @@ namespace Umbraco.Web.Routing
#region GetUrl
private UrlMode GetMode(bool absolute) => absolute ? UrlMode.Absolute : Mode;
private IPublishedContent GetDocument(int id) => _umbracoContext.Content.GetById(id);
private IPublishedContent GetDocument(Guid id) => _umbracoContext.Content.GetById(id);
private IPublishedContent GetMedia(Guid id) => _umbracoContext.Media.GetById(id);
public UrlMode GetMode(bool absolute) => absolute ? UrlMode.Absolute : Mode;
public IPublishedContent GetDocument(int id) => _umbracoContext.Content.GetById(id);
public IPublishedContent GetDocument(Guid id) => _umbracoContext.Content.GetById(id);
public IPublishedContent GetMedia(Guid id) => _umbracoContext.Media.GetById(id);
/// <summary>
/// Gets the url of a published content.
@@ -138,7 +139,7 @@ namespace Umbraco.Web.Routing
return url?.Text ?? "#"; // legacy wants this
}
internal string GetUrlFromRoute(int id, string route, string culture)
public string GetUrlFromRoute(int id, string route, string culture)
{
var provider = _urlProviders.OfType<DefaultUrlProvider>().FirstOrDefault();
var url = provider == null

View File

@@ -134,7 +134,7 @@ namespace Umbraco.Web
/// <summary>
/// Gets the url provider.
/// </summary>
public UrlProvider UrlProvider { get; }
public IPublishedUrlProvider UrlProvider { get; }
/// <summary>
/// Gets/sets the PublishedRequest object