public void Discover(ShapeTableBuilder builder)
 {
     builder.Describe("Foo")
     .OnDisplaying(displaying =>
                   displaying.ChildContent = new HtmlString("<h1>Hi</h1>")
                   );
 }
Exemplo n.º 2
0
        public ShapeTable GetShapeTable(string themeId)
        {
            var cacheKey = $"ShapeTable:{themeId}";

            ShapeTable shapeTable;

            if (!_memoryCache.TryGetValue(cacheKey, out shapeTable))
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Start building shape table");
                }

                var excludedFeatures = _shapeDescriptors.Count == 0 ? new List <string>() :
                                       _shapeDescriptors.Select(kv => kv.Value.Feature.Id).Distinct().ToList();

                foreach (var bindingStrategy in _bindingStrategies)
                {
                    var strategyFeature = _typeFeatureProvider.GetFeatureForDependency(bindingStrategy.GetType());

                    if (!(bindingStrategy is IShapeTableHarvester) && excludedFeatures.Contains(strategyFeature.Id))
                    {
                        continue;
                    }

                    var builder = new ShapeTableBuilder(strategyFeature, excludedFeatures);
                    bindingStrategy.Discover(builder);
                    var builtAlterations = builder.BuildAlterations();

                    BuildDescriptors(bindingStrategy, builtAlterations);
                }

                var enabledAndOrderedFeatureIds = _shellFeaturesManager
                                                  .GetEnabledFeaturesAsync()
                                                  .GetAwaiter()
                                                  .GetResult()
                                                  .Select(f => f.Id)
                                                  .ToList();

                // let the application acting as a super theme for shapes rendering.
                if (enabledAndOrderedFeatureIds.Remove(_hostingEnvironment.ApplicationName))
                {
                    enabledAndOrderedFeatureIds.Add(_hostingEnvironment.ApplicationName);
                }

                var descriptors = _shapeDescriptors
                                  .Where(sd => enabledAndOrderedFeatureIds.Contains(sd.Value.Feature.Id))
                                  .Where(sd => IsModuleOrRequestedTheme(sd.Value.Feature, themeId))
                                  .OrderBy(sd => enabledAndOrderedFeatureIds.IndexOf(sd.Value.Feature.Id))
                                  .GroupBy(sd => sd.Value.ShapeType, StringComparer.OrdinalIgnoreCase)
                                  .Select(group => new ShapeDescriptorIndex
                                          (
                                              shapeType: group.Key,
                                              alterationKeys: group.Select(kv => kv.Key),
                                              descriptors: _shapeDescriptors
                                          ));

                shapeTable = new ShapeTable
                {
                    Descriptors = descriptors.Cast <ShapeDescriptor>().ToDictionary(sd => sd.ShapeType, StringComparer.OrdinalIgnoreCase),
                    Bindings    = descriptors.SelectMany(sd => sd.Bindings).ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase)
                };

                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Done building shape table");
                }

                _memoryCache.Set(cacheKey, shapeTable, new MemoryCacheEntryOptions {
                    Priority = CacheItemPriority.NeverRemove
                });
            }

            return(shapeTable);
        }