private void TraverseResouceShapes(IList <ResourceRequiredContext> resources, ResourceType resourceType, Action <ShapeTable, ResourceRequiredContext, string> processor)
        {
            var shapeKeyPrefix = resourceType == ResourceType.Style ? "Style__" : "Script__";

            var currentTheme = _themeManager.GetRequestTheme(_httpContextAccessor.Current().Request.RequestContext);
            var shapeTable   = _shapeTableLocator.Lookup(currentTheme.Id);

            foreach (var resource in resources)
            {
                var fullPath = resource.Resource.GetFullPath();
                if (!string.IsNullOrEmpty(fullPath))
                {
                    var shapeName = StaticFileBindingStrategy.GetAlternateShapeNameFromFileName(fullPath);
                    var shapeKey  = shapeKeyPrefix + shapeName;

                    // Simply included CDN stylesheets are not in the ShapeTable, so we have to check
                    if (shapeTable.Bindings.ContainsKey(shapeKey))
                    {
                        processor(shapeTable, resource, shapeKey);
                    }
                    else
                    {
                        // Maybe the original binding was removed previously and the combined shape binding remains.
                        shapeKey = CombinedShapeKey(shapeKey);
                        if (shapeTable.Bindings.ContainsKey(shapeKey))
                        {
                            processor(shapeTable, resource, shapeKey);
                        }
                    }
                }
            }
        }
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            shapeBinding = null;

            if (!Enabled)
            {
                return(false);
            }

            var currentThemeName = _siteThemeService.GetCurrentThemeName();
            var shapeTable       = _shapeTableLocator.Lookup(currentThemeName);

            return(shapeTable.Bindings.TryGetValue(shapeType, out shapeBinding));
        }
Ejemplo n.º 3
0
        private void BindPlacement(BuildShapeContext context, string displayType, string stereotype)
        {
            context.FindPlacement = (partShapeType, differentiator, defaultLocation) =>
            {
                var theme      = _siteThemeService.GetSiteTheme();
                var shapeTable = _shapeTableLocator.Lookup(theme.Id);

                var request = _requestContext.HttpContext.Request;

                ShapeDescriptor descriptor;
                if (shapeTable.Descriptors.TryGetValue(partShapeType, out descriptor))
                {
                    var placementContext = new ShapePlacementContext
                    {
                        ContentType    = context.ContentItem.ContentType,
                        Stereotype     = stereotype,
                        DisplayType    = displayType,
                        Differentiator = differentiator,
                        Path           = VirtualPathUtility.AppendTrailingSlash(_virtualPathProvider.ToAppRelative(request.Path)) // get the current app-relative path, i.e. ~/my-blog/foo
                    };

                    // define which location should be used if none placement is hit
                    descriptor.DefaultPlacement = defaultLocation;

                    var placement = descriptor.Placement(placementContext);
                    if (placement != null)
                    {
                        placement.Source = placementContext.Source;
                        return(placement);
                    }
                }

                return(new PlacementInfo
                {
                    Location = defaultLocation,
                    Source = String.Empty
                });
            };
        }
Ejemplo n.º 4
0
        public override IList <ResourceRequiredContext> BuildRequiredResources(string stringResourceType)
        {
            // It's necessary to make a copy since making a change to the local variable also changes the private one.
            var resources = new List <ResourceRequiredContext>(base.BuildRequiredResources(stringResourceType));

            var settingsPart = _cacheManager.Get("Piedone.Combinator.CombinatorSettingsPart", ctx =>
            {
                _combinatorEventMonitor.MonitorConfigurationChanged(ctx);

                return(_siteService.GetSiteSettings().As <CombinatorSettingsPart>());
            });

            if (resources.Count == 0 ||
                Orchard.UI.Admin.AdminFilter.IsApplied(_httpContextAccessor.Current().Request.RequestContext) && !settingsPart.EnableForAdmin)
            {
                return(resources);
            }

            var resourceType = ResourceTypeHelper.StringTypeToEnum(stringResourceType);

            try
            {
                var settings = new CombinatorSettings
                {
                    CombineCDNResources     = settingsPart.CombineCDNResources,
                    EmbedCssImages          = settingsPart.EmbedCssImages,
                    EmbeddedImagesMaxSizeKB = settingsPart.EmbeddedImagesMaxSizeKB,
                    MinifyResources         = settingsPart.MinifyResources
                };

                if (!String.IsNullOrEmpty(settingsPart.CombinationExcludeRegex))
                {
                    settings.CombinationExcludeFilter = new Regex(settingsPart.CombinationExcludeRegex);
                }
                if (!String.IsNullOrEmpty(settingsPart.EmbedCssImagesStylesheetExcludeRegex))
                {
                    settings.EmbedCssImagesStylesheetExcludeFilter = new Regex(settingsPart.EmbedCssImagesStylesheetExcludeRegex);
                }
                if (!String.IsNullOrEmpty(settingsPart.MinificationExcludeRegex))
                {
                    settings.MinificationExcludeFilter = new Regex(settingsPart.MinificationExcludeRegex);
                }

                if (!String.IsNullOrEmpty(settingsPart.ResourceSetRegexes))
                {
                    var setRegexes = new List <Regex>();
                    foreach (var regex in settingsPart.ResourceSetRegexes.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (!String.IsNullOrEmpty(regex))
                        {
                            setRegexes.Add(new Regex(regex));
                        }
                    }
                    settings.ResourceSetFilters = setRegexes.ToArray();
                }

                if (resourceType == ResourceType.Style)
                {
                    // Checking for overridden stylesheets
                    var currentTheme = _themeManager.GetRequestTheme(_httpContextAccessor.Current().Request.RequestContext);
                    var shapeTable   = _shapeTableLocator.Lookup(currentTheme.Id);

                    foreach (var resource in resources)
                    {
                        var shapeName = StylesheetBindingStrategy.GetAlternateShapeNameFromFileName(resource.Resource.GetFullPath());

                        // Simply included CDN stylesheets are not in the ShapeTable, so we have to check
                        if (shapeTable.Bindings.ContainsKey("Style__" + shapeName))
                        {
                            var binding = shapeTable.Bindings["Style__" + shapeName].BindingSource;
                            resource.Resource.SetUrl(binding, null);
                        }
                    }

                    return(_combinatorService.CombineStylesheets(resources, settings));
                }
                else if (resourceType == ResourceType.JavaScript)
                {
                    return(_combinatorService.CombineScripts(resources, settings));
                }

                return(base.BuildRequiredResources(stringResourceType));
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }
                Logger.Error(ex, "Error when combining " + resourceType + " files");
                return(base.BuildRequiredResources(stringResourceType));
            }
        }