Files
Umbraco-CMS/src/Umbraco.Core/Snippets/PartialViewSnippetCollectionBuilder.cs
Elitsa Marinovska 9326cc5fc6 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>
2022-05-09 11:42:10 +02:00

43 lines
1.6 KiB
C#

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