Use SnippetCollection to when working with snippets (#12355)

* Introducing a new Snippet type

* Adding a SnippetCollection and SnippetCollectionBuilder

* Using snippetCollection to get the snippets instead of fileService

* Fixed fetching the correct content

* Make ISnippet non-discoverable

* Split the SnippetCollection into PartialViewSnippetCollection and PartialViewMacroSnippetCollection

* Update CodeFileController to use the 2 snippet collections

* Display the names with Empty.cshtml on top

* Remove merging embedded snippets with custom snippets from ~\Umbraco.Web.UI\umbraco\PartialViewMacros\Templates folder for the Partial View Collection

* Fix naming

* Fix another naming

* Cleanup + Use base items

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
(cherry picked from commit 9326cc5fc6)
This commit is contained in:
Elitsa Marinovska
2022-05-09 11:42:10 +02:00
committed by Sebastiaan Janssen
parent 4f48a4937b
commit a3692b887a
10 changed files with 325 additions and 17 deletions

View File

@@ -13,6 +13,7 @@ using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.Validators;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Sections;
using Umbraco.Cms.Core.Snippets;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Tour;
using Umbraco.Cms.Core.Trees;
@@ -90,6 +91,8 @@ namespace Umbraco.Cms.Core.DependencyInjection
.Add<RedirectUrlDashboard>()
.Add<SettingsDashboard>()
.Add(builder.TypeLoader.GetTypes<IDashboard>());
builder.PartialViewSnippets();
builder.PartialViewMacroSnippets();
builder.DataValueReferenceFactories();
builder.PropertyValueConverters().Append(builder.TypeLoader.GetTypes<IPropertyValueConverter>());
builder.UrlSegmentProviders().Append<DefaultUrlSegmentProvider>();
@@ -200,6 +203,20 @@ namespace Umbraco.Cms.Core.DependencyInjection
public static DashboardCollectionBuilder Dashboards(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DashboardCollectionBuilder>();
/// <summary>
/// Gets the partial view snippets collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static PartialViewSnippetCollectionBuilder? PartialViewSnippets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PartialViewSnippetCollectionBuilder>();
/// <summary>
/// Gets the partial view macro snippets collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static PartialViewMacroSnippetCollectionBuilder? PartialViewMacroSnippets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PartialViewMacroSnippetCollectionBuilder>();
/// <summary>
/// Gets the cache refreshers collection builder.
/// </summary>

View File

@@ -35,6 +35,7 @@ using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Snippets;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Core.Telemetry;
using Umbraco.Cms.Core.Templates;

View File

@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Services
@@ -10,6 +7,7 @@ namespace Umbraco.Cms.Core.Services
/// </summary>
public interface IFileService : IService
{
[Obsolete("Please use SnippetCollection.GetPartialViewSnippetNames() or SnippetCollection.GetPartialViewMacroSnippetNames() instead. Scheduled for removal in V12.")]
IEnumerable<string> GetPartialViewSnippetNames(params string[] filterNames);
void CreatePartialViewFolder(string folderPath);
void CreatePartialViewMacroFolder(string folderPath);
@@ -295,6 +293,7 @@ namespace Umbraco.Cms.Core.Services
/// </summary>
/// <param name="snippetName">The name of the snippet</param>
/// <returns></returns>
[Obsolete("Please use SnippetCollection.GetPartialViewMacroSnippetContent instead. Scheduled for removal in V12.")]
string GetPartialViewMacroSnippetContent(string snippetName);
/// <summary>
@@ -302,6 +301,7 @@ namespace Umbraco.Cms.Core.Services
/// </summary>
/// <param name="snippetName">The name of the snippet</param>
/// <returns>The content of the partial view.</returns>
[Obsolete("Please use SnippetCollection.GetPartialViewSnippetContent instead. Scheduled for removal in V12.")]
string GetPartialViewSnippetContent(string snippetName);
}
}

View File

@@ -0,0 +1,18 @@
namespace Umbraco.Cms.Core.Snippets
{
/// <summary>
/// Defines a partial view macro snippet.
/// </summary>
public interface ISnippet
{
/// <summary>
/// Gets the name of the snippet.
/// </summary>
string Name { get; }
/// <summary>
/// Gets the content of the snippet.
/// </summary>
string Content { get; }
}
}

View File

@@ -0,0 +1,66 @@
using System.Text.RegularExpressions;
using Umbraco.Cms.Core.Composing;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Snippets
{
/// <summary>
/// The collection of partial view macro snippets.
/// </summary>
public class PartialViewMacroSnippetCollection : BuilderCollectionBase<ISnippet>
{
public PartialViewMacroSnippetCollection(Func<IEnumerable<ISnippet>> items) : base(items)
{
}
/// <summary>
/// Gets the partial view macro snippet names.
/// </summary>
/// <returns>The names of all partial view macro snippets.</returns>
public IEnumerable<string> GetNames()
{
var snippetNames = this.Select(x => Path.GetFileNameWithoutExtension(x.Name)).ToArray();
// Ensure the ones that are called 'Empty' are at the top
var empty = snippetNames.Where(x => Path.GetFileName(x)?.InvariantStartsWith("Empty") ?? false)
.OrderBy(x => x?.Length).ToArray();
return empty.Union(snippetNames.Except(empty)).WhereNotNull();
}
/// <summary>
/// Gets the content of a partial view macro snippet as a string.
/// </summary>
/// <param name="snippetName">The name of the snippet.</param>
/// <returns>The content of the partial view macro.</returns>
public string GetContentFromName(string snippetName)
{
if (snippetName.IsNullOrWhiteSpace())
{
throw new ArgumentNullException(nameof(snippetName));
}
string partialViewMacroHeader = "@inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage";
var snippet = this.Where(x => x.Name.Equals(snippetName + ".cshtml")).FirstOrDefault();
// Try and get the snippet path
if (snippet is null)
{
throw new InvalidOperationException("Could not load snippet with name " + snippetName);
}
// Strip the @inherits if it's there
var snippetContent = StripPartialViewHeader(snippet.Content);
var content = $"{partialViewMacroHeader}{Environment.NewLine}{snippetContent}";
return content;
}
private string StripPartialViewHeader(string contents)
{
var headerMatch = new Regex("^@inherits\\s+?.*$", RegexOptions.Multiline);
return headerMatch.Replace(contents, string.Empty);
}
}
}

View File

@@ -0,0 +1,56 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Extensions;
namespace Umbraco.Cms.Core.Snippets
{
/// <summary>
/// The partial view macro snippet collection builder.
/// </summary>
public class PartialViewMacroSnippetCollectionBuilder : LazyCollectionBuilderBase<PartialViewMacroSnippetCollectionBuilder, PartialViewMacroSnippetCollection, ISnippet>
{
protected override PartialViewMacroSnippetCollectionBuilder This => this;
protected override IEnumerable<ISnippet> CreateItems(IServiceProvider factory)
{
var hostEnvironment = factory.GetRequiredService<IHostEnvironment>();
var embeddedSnippets = new List<ISnippet>(base.CreateItems(factory));
var snippetProvider = new EmbeddedFileProvider(typeof(IAssemblyProvider).Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets");
var embeddedFiles = snippetProvider.GetDirectoryContents(string.Empty)
.Where(x => !x.IsDirectory && x.Name.EndsWith(".cshtml"));
foreach (var file in embeddedFiles)
{
using var stream = new StreamReader(file.CreateReadStream());
embeddedSnippets.Add(new Snippet(file.Name, stream.ReadToEnd().Trim()));
}
var customSnippetsDir = new DirectoryInfo(hostEnvironment.MapPathContentRoot($"{Constants.SystemDirectories.Umbraco}/PartialViewMacros/Templates"));
if (!customSnippetsDir.Exists)
{
return embeddedSnippets;
}
var customSnippets = customSnippetsDir.GetFiles().Select(f => new Snippet(f.Name, File.ReadAllText(f.FullName)));
var allSnippets = Merge(embeddedSnippets, customSnippets);
return allSnippets;
}
private IEnumerable<ISnippet> Merge(IEnumerable<ISnippet> embeddedSnippets, IEnumerable<ISnippet> customSnippets)
{
var allSnippets = embeddedSnippets.Concat(customSnippets);
var duplicates = allSnippets.GroupBy(s => s.Name)
.Where(gr => gr.Count() > 1) // Finds the snippets with the same name
.Select(s => s.First()); // Takes the first element from a grouping, which is the embeded snippet with that same name,
// since the physical snippet files are placed after the embedded ones in the all snippets colleciton
// Remove any embedded snippets if a physical file with the same name can be found
return allSnippets.Except(duplicates);
}
}
}

View File

@@ -0,0 +1,70 @@
using System.Text.RegularExpressions;
using Umbraco.Cms.Core.Composing;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Snippets
{
/// <summary>
/// The collection of partial view snippets.
/// </summary>
public class PartialViewSnippetCollection : BuilderCollectionBase<ISnippet>
{
public PartialViewSnippetCollection(Func<IEnumerable<ISnippet>> items) : base(items)
{
}
/// <summary>
/// Gets the partial view snippet names.
/// </summary>
/// <returns>The names of all partial view snippets.</returns>
public IEnumerable<string> GetNames()
{
var snippetNames = this.Select(x => Path.GetFileNameWithoutExtension(x.Name)).ToArray();
// Ensure the ones that are called 'Empty' are at the top
var empty = snippetNames.Where(x => Path.GetFileName(x)?.InvariantStartsWith("Empty") ?? false)
.OrderBy(x => x?.Length).ToArray();
return empty.Union(snippetNames.Except(empty)).WhereNotNull();
}
/// <summary>
/// Gets the content of a partial view snippet as a string.
/// </summary>
/// <param name="snippetName">The name of the snippet.</param>
/// <returns>The content of the partial view.</returns>
public string GetContentFromName(string snippetName)
{
if (snippetName.IsNullOrWhiteSpace())
{
throw new ArgumentNullException(nameof(snippetName));
}
string partialViewHeader = "@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage";
var snippet = this.Where(x => x.Name.Equals(snippetName + ".cshtml")).FirstOrDefault();
// Try and get the snippet path
if (snippet is null)
{
throw new InvalidOperationException("Could not load snippet with name " + snippetName);
}
var snippetContent = CleanUpContents(snippet.Content);
var content = $"{partialViewHeader}{Environment.NewLine}{snippetContent}";
return content;
}
private string CleanUpContents(string content)
{
// Strip the @inherits if it's there
var headerMatch = new Regex("^@inherits\\s+?.*$", RegexOptions.Multiline);
var newContent = headerMatch.Replace(content, string.Empty);
return newContent
.Replace("Model.Content.", "Model.")
.Replace("(Model.Content)", "(Model)");
}
}
}

View File

@@ -0,0 +1,42 @@
using Microsoft.Extensions.FileProviders;
using Umbraco.Cms.Core.Composing;
namespace Umbraco.Cms.Core.Snippets
{
/// <summary>
/// The partial view snippet collection builder.
/// </summary>
public class PartialViewSnippetCollectionBuilder : LazyCollectionBuilderBase<PartialViewSnippetCollectionBuilder, PartialViewSnippetCollection, ISnippet>
{
protected override PartialViewSnippetCollectionBuilder This => this;
protected override IEnumerable<ISnippet> CreateItems(IServiceProvider factory)
{
var embeddedSnippets = new List<ISnippet>(base.CreateItems(factory));
// Ignore these
var filterNames = new List<string>
{
"Gallery",
"ListChildPagesFromChangeableSource",
"ListChildPagesOrderedByProperty",
"ListImagesFromMediaFolder"
};
var snippetProvider = new EmbeddedFileProvider(typeof(IAssemblyProvider).Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets");
var embeddedFiles = snippetProvider.GetDirectoryContents(string.Empty)
.Where(x => !x.IsDirectory && x.Name.EndsWith(".cshtml"));
foreach (var file in embeddedFiles)
{
if (!filterNames.Contains(Path.GetFileNameWithoutExtension(file.Name)))
{
using var stream = new StreamReader(file.CreateReadStream());
embeddedSnippets.Add(new Snippet(file.Name, stream.ReadToEnd().Trim()));
}
}
return embeddedSnippets;
}
}
}

View File

@@ -0,0 +1,14 @@
namespace Umbraco.Cms.Core.Snippets
{
public class Snippet : ISnippet
{
public string Name { get; }
public string Content { get; }
public Snippet(string name, string content)
{
Name = name;
Content = content;
}
}
}

View File

@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
@@ -15,6 +12,7 @@ using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Snippets;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Strings.Css;
using Umbraco.Cms.Web.BackOffice.Filters;
@@ -22,6 +20,7 @@ using Umbraco.Cms.Web.BackOffice.Trees;
using Umbraco.Cms.Web.Common.ActionsResults;
using Umbraco.Cms.Web.Common.Attributes;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Cms.Web.Common.DependencyInjection;
using Umbraco.Extensions;
using Constants = Umbraco.Cms.Core.Constants;
using Stylesheet = Umbraco.Cms.Core.Models.Stylesheet;
@@ -45,7 +44,10 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
private readonly IUmbracoMapper _umbracoMapper;
private readonly IShortStringHelper _shortStringHelper;
private readonly GlobalSettings _globalSettings;
private readonly PartialViewSnippetCollection _partialViewSnippetCollection;
private readonly PartialViewMacroSnippetCollection _partialViewMacroSnippetCollection;
[ActivatorUtilitiesConstructor]
public CodeFileController(
IHostingEnvironment hostingEnvironment,
FileSystems fileSystems,
@@ -54,7 +56,9 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
ILocalizedTextService localizedTextService,
IUmbracoMapper umbracoMapper,
IShortStringHelper shortStringHelper,
IOptionsSnapshot<GlobalSettings> globalSettings)
IOptionsSnapshot<GlobalSettings> globalSettings,
PartialViewSnippetCollection partialViewSnippetCollection,
PartialViewMacroSnippetCollection partialViewMacroSnippetCollection)
{
_hostingEnvironment = hostingEnvironment;
_fileSystems = fileSystems;
@@ -64,6 +68,31 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
_umbracoMapper = umbracoMapper;
_shortStringHelper = shortStringHelper;
_globalSettings = globalSettings.Value;
_partialViewSnippetCollection = partialViewSnippetCollection;
_partialViewMacroSnippetCollection = partialViewMacroSnippetCollection;
}
[Obsolete("Use ctor will all params. Scheduled for removal in V12.")]
public CodeFileController(
IHostingEnvironment hostingEnvironment,
FileSystems fileSystems,
IFileService fileService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
ILocalizedTextService localizedTextService,
IUmbracoMapper umbracoMapper,
IShortStringHelper shortStringHelper,
IOptionsSnapshot<GlobalSettings> globalSettings) : this(
hostingEnvironment,
fileSystems,
fileService,
backOfficeSecurityAccessor,
localizedTextService,
umbracoMapper,
shortStringHelper,
globalSettings,
StaticServiceProvider.Instance.GetRequiredService<PartialViewSnippetCollection>(),
StaticServiceProvider.Instance.GetRequiredService<PartialViewMacroSnippetCollection>())
{
}
/// <summary>
@@ -272,15 +301,10 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
switch (type)
{
case Constants.Trees.PartialViews:
snippets = _fileService.GetPartialViewSnippetNames(
//ignore these - (this is taken from the logic in "PartialView.ascx.cs")
"Gallery",
"ListChildPagesFromChangeableSource",
"ListChildPagesOrderedByProperty",
"ListImagesFromMediaFolder");
snippets = _partialViewSnippetCollection.GetNames();
break;
case Constants.Trees.PartialViewMacros:
snippets = _fileService.GetPartialViewSnippetNames();
snippets = _partialViewMacroSnippetCollection.GetNames();
break;
default:
return NotFound();
@@ -312,7 +336,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
codeFileDisplay.VirtualPath = Constants.SystemDirectories.PartialViews;
if (snippetName.IsNullOrWhiteSpace() == false)
{
codeFileDisplay.Content = _fileService.GetPartialViewSnippetContent(snippetName!);
codeFileDisplay.Content = _partialViewSnippetCollection.GetContentFromName(snippetName!);
}
}
@@ -324,7 +348,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
codeFileDisplay.VirtualPath = Constants.SystemDirectories.MacroPartials;
if (snippetName.IsNullOrWhiteSpace() == false)
{
codeFileDisplay.Content = _fileService.GetPartialViewMacroSnippetContent(snippetName!);
codeFileDisplay.Content = _partialViewMacroSnippetCollection.GetContentFromName(snippetName!);
}
}