-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathUmbracoServices.cs
231 lines (194 loc) · 10.7 KB
/
UmbracoServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
namespace Umbraco.ModelsBuilder.Umbraco
{
public class UmbracoServices
{
private readonly IContentTypeService _contentTypeService;
private readonly IMediaTypeService _mediaTypeService;
private readonly IMemberTypeService _memberTypeService;
private readonly IPublishedContentTypeFactory _publishedContentTypeFactory;
public UmbracoServices(IContentTypeService contentTypeService, IMediaTypeService mediaTypeService, IMemberTypeService memberTypeService, IPublishedContentTypeFactory publishedContentTypeFactory)
{
_contentTypeService = contentTypeService;
_mediaTypeService = mediaTypeService;
_memberTypeService = memberTypeService;
_publishedContentTypeFactory = publishedContentTypeFactory;
}
private static Config Config => Current.Configs.ModelsBuilder();
#region Services
public IList<TypeModel> GetAllTypes()
{
var types = new List<TypeModel>();
types.AddRange(GetTypes(PublishedItemType.Content, _contentTypeService.GetAll().Cast<IContentTypeComposition>().ToArray()));
types.AddRange(GetTypes(PublishedItemType.Media, _mediaTypeService.GetAll().Cast<IContentTypeComposition>().ToArray()));
types.AddRange(GetTypes(PublishedItemType.Member, _memberTypeService.GetAll().Cast<IContentTypeComposition>().ToArray()));
return EnsureDistinctAliases(types);
}
public IList<TypeModel> GetContentTypes()
{
var contentTypes = _contentTypeService.GetAll().Cast<IContentTypeComposition>().ToArray();
return GetTypes(PublishedItemType.Content, contentTypes); // aliases have to be unique here
}
public IList<TypeModel> GetMediaTypes()
{
var contentTypes = _mediaTypeService.GetAll().Cast<IContentTypeComposition>().ToArray();
return GetTypes(PublishedItemType.Media, contentTypes); // aliases have to be unique here
}
public IList<TypeModel> GetMemberTypes()
{
var memberTypes = _memberTypeService.GetAll().Cast<IContentTypeComposition>().ToArray();
return GetTypes(PublishedItemType.Member, memberTypes); // aliases have to be unique here
}
public static string GetClrName(string name, string alias)
{
// ideally we should just be able to re-use Umbraco's alias,
// just upper-casing the first letter, however in v7 for backward
// compatibility reasons aliases derive from names via ToSafeAlias which is
// PreFilter = ApplyUrlReplaceCharacters,
// IsTerm = (c, leading) => leading
// ? char.IsLetter(c) // only letters
// : (char.IsLetterOrDigit(c) || c == '_'), // letter, digit or underscore
// StringType = CleanStringType.Ascii | CleanStringType.UmbracoCase,
// BreakTermsOnUpper = false
//
// but that is not ideal with acronyms and casing
// however we CANNOT change Umbraco
// so, adding a way to "do it right" deriving from name, here
switch (Config.ClrNameSource)
{
case ClrNameSource.RawAlias:
// use Umbraco's alias
return alias;
case ClrNameSource.Alias:
// ModelsBuilder's legacy - but not ideal
return alias.ToCleanString(CleanStringType.ConvertCase | CleanStringType.PascalCase);
case ClrNameSource.Name:
// derive from name
var source = name.TrimStart('_'); // because CleanStringType.ConvertCase accepts them
return source.ToCleanString(CleanStringType.ConvertCase | CleanStringType.PascalCase | CleanStringType.Ascii);
default:
throw new Exception("Invalid ClrNameSource.");
}
}
private IList<TypeModel> GetTypes(PublishedItemType itemType, IContentTypeComposition[] contentTypes)
{
var typeModels = new List<TypeModel>();
var uniqueTypes = new HashSet<string>();
// get the types and the properties
foreach (var contentType in contentTypes)
{
var typeModel = new TypeModel
{
Id = contentType.Id,
Alias = contentType.Alias,
ClrName = GetClrName(contentType.Name, contentType.Alias),
ParentId = contentType.ParentId,
Name = contentType.Name,
Description = contentType.Description
};
// of course this should never happen, but when it happens, better detect it
// else we end up with weird nullrefs everywhere
if (uniqueTypes.Contains(typeModel.ClrName))
throw new Exception($"Panic: duplicate type ClrName \"{typeModel.ClrName}\".");
uniqueTypes.Add(typeModel.ClrName);
var publishedContentType = _publishedContentTypeFactory.CreateContentType(contentType);
switch (itemType)
{
case PublishedItemType.Content:
typeModel.ItemType = publishedContentType.ItemType == PublishedItemType.Element
? TypeModel.ItemTypes.Element
: TypeModel.ItemTypes.Content;
break;
case PublishedItemType.Media:
typeModel.ItemType = publishedContentType.ItemType == PublishedItemType.Element
? TypeModel.ItemTypes.Element
: TypeModel.ItemTypes.Media;
break;
case PublishedItemType.Member:
typeModel.ItemType = publishedContentType.ItemType == PublishedItemType.Element
? TypeModel.ItemTypes.Element
: TypeModel.ItemTypes.Member;
break;
default:
throw new InvalidOperationException(string.Format("Unsupported PublishedItemType \"{0}\".", itemType));
}
typeModels.Add(typeModel);
foreach (var propertyType in contentType.PropertyTypes)
{
var propertyModel = new PropertyModel
{
Alias = propertyType.Alias,
ClrName = GetClrName(propertyType.Name, propertyType.Alias),
Name = propertyType.Name,
Description = propertyType.Description,
IsCultureVariant = propertyType.Variations == ContentVariation.Culture
};
var publishedPropertyType = publishedContentType.GetPropertyType(propertyType.Alias);
if (publishedPropertyType == null)
throw new Exception($"Panic: could not get published property type {contentType.Alias}.{propertyType.Alias}.");
propertyModel.ModelClrType = publishedPropertyType.ModelClrType;
typeModel.Properties.Add(propertyModel);
}
}
// wire the base types
foreach (var typeModel in typeModels.Where(x => x.ParentId > 0))
{
typeModel.BaseType = typeModels.SingleOrDefault(x => x.Id == typeModel.ParentId);
// Umbraco 7.4 introduces content types containers, so even though ParentId > 0, the parent might
// not be a content type - here we assume that BaseType being null while ParentId > 0 means that
// the parent is a container (and we don't check).
typeModel.IsParent = typeModel.BaseType != null;
}
// discover mixins
foreach (var contentType in contentTypes)
{
var typeModel = typeModels.SingleOrDefault(x => x.Id == contentType.Id);
if (typeModel == null) throw new Exception("Panic: no type model matching content type.");
IEnumerable<IContentTypeComposition> compositionTypes;
var contentTypeAsMedia = contentType as IMediaType;
var contentTypeAsContent = contentType as IContentType;
var contentTypeAsMember = contentType as IMemberType;
if (contentTypeAsMedia != null) compositionTypes = contentTypeAsMedia.ContentTypeComposition;
else if (contentTypeAsContent != null) compositionTypes = contentTypeAsContent.ContentTypeComposition;
else if (contentTypeAsMember != null) compositionTypes = contentTypeAsMember.ContentTypeComposition;
else throw new Exception(string.Format("Panic: unsupported type \"{0}\".", contentType.GetType().FullName));
foreach (var compositionType in compositionTypes)
{
var compositionModel = typeModels.SingleOrDefault(x => x.Id == compositionType.Id);
if (compositionModel == null) throw new Exception("Panic: composition type does not exist.");
if (compositionType.Id == contentType.ParentId) continue;
// add to mixins
typeModel.MixinTypes.Add(compositionModel);
// mark as mixin - as well as parents
compositionModel.IsMixin = true;
while ((compositionModel = compositionModel.BaseType) != null)
compositionModel.IsMixin = true;
}
}
return typeModels;
}
internal static IList<TypeModel> EnsureDistinctAliases(IList<TypeModel> typeModels)
{
var groups = typeModels.GroupBy(x => x.Alias.ToLowerInvariant());
foreach (var group in groups.Where(x => x.Count() > 1))
{
throw new NotSupportedException($"Alias \"{group.Key}\" is used by types"
+ $" {string.Join(", ", group.Select(x => x.ItemType + ":\"" + x.Alias + "\""))}. Aliases have to be unique."
+ " One of the aliases must be modified in order to use the ModelsBuilder.");
}
return typeModels;
}
#endregion
}
}