/** * Check if filters are properly assigned to resource. * * @param ContainerInterface $container Current container. * @param LibraryDefinitionInterface $definition Current definition. * @param ResourceInterface $resource Current resource. * @throws ConfigurationException */ private function checkFiltersAssignment(ContainerInterface $container, LibraryDefinitionInterface $definition, ResourceInterface $resource) { if ($this->options['check_filter_assignment']) { if (isset($resource->getOptions()['filters'])) { $filters = $resource->getOptions()['filters']; if (!is_array($filters)) { throw new ConfigurationException(sprintf('Expected array of filters for assigned for resource "%s" in definition "%s", "%s" given.', $resource->getSource(), $definition->getName(), gettype($filters))); } if ($resource instanceof ReferenceResource) { throw new ConfigurationException(sprintf('You can not assign filters for reference resource "%s" in definition "%s".', $resource->getSource(), $definition->getName())); } $filters = array_map(function ($filter) { return ltrim($filter, '?'); }, $filters); if (count($duplicates = array_unique(array_diff_assoc($filters, array_unique($filters)))) > 0) { throw new ConfigurationException(sprintf('Filters "%s" for resource "%s" in definition "%s" are used more than once.', implode('", "', $duplicates), $resource->getSource(), $definition->getName())); } } } }
/** * Process given remote resource, and if it is eligible for dynamic load, it is replaced with javascript * function for inclusion on client side. * * @param ResourceInterface $resource Resource to process. * @return ResourceInterface New resource with dynamic js code, or same resource if it is blacklisted. */ private function processResource(ResourceInterface $resource) { $url = $resource->getSource(); if (count($this->options['whitelist']) && !in_array($url, $this->options['whitelist']) || count($this->options['blacklist']) && in_array($url, $this->options['blacklist'])) { return $resource; } switch (AssetType::guessAssetType($url)) { case AssetType::JAVASCRIPT: return new JavascriptStringResource(sprintf(';(function(r){ r.DynamicAssetInclusion.loadJavascript(\'%s\'); })(RunOpenCode);', addslashes($url))); break; case AssetType::STYLESHEET: return new JavascriptStringResource(sprintf(';(function(r){ r.DynamicAssetInclusion.loadStylesheet(\'%s\', %s); })(RunOpenCode);', addslashes($url), isset($resource->getOptions()['media']) ? sprintf('\'%s\'', $resource->getOptions()['media']) : 'null')); break; default: throw new RuntimeException(sprintf('Unknown remote resource type with source "%s".', $resource->getSource())); break; } }
/** * Calculates new file name for resource that will be dumped onto new location. * * @param ResourceInterface $resource Resource which will be dumped in new location. * @return string New file name. */ private function calculateTargetFilename(ResourceInterface $resource) { if ($resource instanceof StringResource) { $filename = isset($resource->getOptions()['filename']) ? $resource->getOptions()['filename'] : $resource->getKey(); if ($resource instanceof JavascriptStringResource) { $extension = AssetType::JAVASCRIPT; } elseif ($resource instanceof StylesheetStringResource) { $extension = AssetType::STYLESHEET; } else { throw new InvalidArgumentException(sprintf('Unable to determine resource type, instance of "%s" expected, "%s" given.', implode('", "', array('RunOpenCode\\AssetsInjection\\Resource\\JavascriptStringResource', 'RunOpenCode\\AssetsInjection\\Resource\\StylesheetStringResource')), get_class($resource))); } } else { $filename = pathinfo($resource->getSource(), PATHINFO_FILENAME); $extension = AssetType::guessAssetType($resource->getSource()); } $environment = $this->options['development'] ? $this->options['development_environment_extension_suffix'] : $this->options['production_environment_extension_suffix']; return sprintf('%s%s%s%s', $resource->getKey() . '.', $filename . '.', $environment ? $environment . '.' : '', $extension); }