Files
Umbraco-CMS/src/Umbraco.Web.BackOffice/Trees/TreeCollectionBuilder.cs
Mole bf41c2eeaa Netcore: Align namespaces (#9801)
* Rename Umbraco.Core namespace to Umbraco.Cms.Core

* Move extension methods in core project to Umbraco.Extensions

* Move extension methods in core project to Umbraco.Extensions

* Rename Umbraco.Examine namespace to Umbraco.Cms.Examine

* Move examine extensions to Umbraco.Extensions namespace

* Reflect changed namespaces in Builder and fix unit tests

* Adjust namespace in Umbraco.ModelsBuilder.Embedded

* Adjust namespace in Umbraco.Persistence.SqlCe

* Adjust namespace in Umbraco.PublishedCache.NuCache

* Align namespaces in Umbraco.Web.BackOffice

* Align namespaces in Umbraco.Web.Common

* Ensure that SqlCeSupport is still enabled after changing the namespace

* Align namespaces in Umbraco.Web.Website

* Align namespaces in Umbraco.Web.UI.NetCore

* Align namespaces in Umbraco.Tests.Common

* Align namespaces in Umbraco.Tests.UnitTests

* Align namespaces in Umbraco.Tests.Integration

* Fix errors caused by changed namespaces

* Fix integration tests

* Undo the Umbraco.Examine.Lucene namespace change

This breaks integration tests on linux, since the namespace wont exists there because it's only used on windows.

* Fix merge

* Fix Merge
2021-02-18 11:06:02 +01:00

61 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Trees;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.BackOffice.Trees
{
/// <summary>
/// Builds a <see cref="TreeCollection"/>.
/// </summary>
public class TreeCollectionBuilder : ICollectionBuilder<TreeCollection, Tree>
{
private readonly List<Tree> _trees = new List<Tree>();
public TreeCollection CreateCollection(IServiceProvider factory) => new TreeCollection(_trees);
public void RegisterWith(IServiceCollection services) => services.Add(new ServiceDescriptor(typeof(TreeCollection), CreateCollection, ServiceLifetime.Singleton));
/// <summary>
/// Registers a custom tree definition
/// </summary>
/// <param name="treeDefinition"></param>
/// <remarks>
/// This is useful if a developer wishes to have a single tree controller for different tree aliases. In this case the tree controller
/// cannot be decorated with the TreeAttribute (since then it will be auto-registered).
/// </remarks>
public void AddTree(Tree treeDefinition)
{
if (treeDefinition == null) throw new ArgumentNullException(nameof(treeDefinition));
_trees.Add(treeDefinition);
}
public void AddTreeController<TController>()
where TController : TreeControllerBase
=> AddTreeController(typeof(TController));
public void AddTreeController(Type controllerType)
{
if (!typeof(TreeControllerBase).IsAssignableFrom(controllerType))
throw new ArgumentException($"Type {controllerType} does not inherit from {typeof(TreeControllerBase).FullName}.");
// not all TreeControllerBase are meant to be used here,
// ignore those that don't have the attribute
var attribute = controllerType.GetCustomAttribute<TreeAttribute>(false);
if (attribute == null) return;
var tree = new Tree(attribute.SortOrder, attribute.SectionAlias, attribute.TreeGroup, attribute.TreeAlias, attribute.TreeTitle, attribute.TreeUse, controllerType, attribute.IsSingleNodeTree);
_trees.Add(tree);
}
public void AddTreeControllers(IEnumerable<Type> controllerTypes)
{
foreach (var controllerType in controllerTypes)
AddTreeController(controllerType);
}
}
}