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>
This commit is contained in:
Elitsa Marinovska
2022-05-09 11:42:10 +02:00
committed by GitHub
parent 22d11d23df
commit 9326cc5fc6
10 changed files with 325 additions and 19 deletions

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