/** * 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; } }
/** * @expectedException \RuntimeException */ public function testExtensionTypeCanNotBeOverwritten() { AssetType::registerAssetType('new', AssetType::STYLESHEET); AssetType::registerAssetType('new', AssetType::JAVASCRIPT); }
/** * {@inheritdoc} */ public function eject($type, $position = null) { if (!in_array($type = strtolower($type), [AssetType::JAVASCRIPT, AssetType::STYLESHEET])) { throw new InvalidArgumentException(sprintf('You can not eject resources of unknown "%s" asset type.', $type)); } $ejected = []; foreach ($this->injected['resources'] as $key => $value) { /** * @var ResourceInterface $value */ if (!is_null($value) && ($position === false || is_null($position) && !isset($value->getOptions()['position']) || $value->getOptions()['position'] == $position) && ($type == AssetType::JAVASCRIPT && $value instanceof JavascriptStringResource || $type == AssetType::STYLESHEET && $value instanceof StylesheetStringResource || $type == AssetType::guessAssetType($value))) { $ejected[$key] = $value; $this->injected['resources'][$key] = null; } } return $ejected; }
/** * 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); }
?> _<?php echo $position; ?> () { return [ <?php foreach ($injectedResources as $injectedResource) { $resourcePosition = isset($injectedResource->getOptions()['position']) ? $injectedResource->getOptions()['position'] : ''; switch ($resourceClass = ltrim(get_class($injectedResource), '\\')) { case 'RunOpenCode\\AssetsInjection\\Resource\\FileResource': // Fall trough // Fall trough case 'RunOpenCode\\AssetsInjection\\Resource\\HttpResource': $resourceType = \RunOpenCode\AssetsInjection\Utils\AssetType::guessAssetType($injectedResource->getSource()); break; case 'RunOpenCode\\AssetsInjection\\Resource\\JavascriptStringResource': $resourceType = \RunOpenCode\AssetsInjection\Utils\AssetType::JAVASCRIPT; break; case 'RunOpenCode\\AssetsInjection\\Resource\\StylesheetStringResource': $resourceType = \RunOpenCode\AssetsInjection\Utils\AssetType::STYLESHEET; break; default: throw new \RunOpenCode\AssetsInjection\Exception\RuntimeException(sprintf('Unknown resource type "%s" given for generating container.', $resourceClass)); break; } if ($type == $resourceType && $position == $resourcePosition) { ?> new \<?php echo $resourceClass;
public function render(array $resources, array $options = []) { $result = []; /** * @var ResourceInterface $resource */ foreach ($resources as $resource) { $attributes = array_merge([], isset($options['attributes']) ? $options['attributes'] : [], isset($resource->getOptions()['attributes']) ? $resource->getOptions()['attributes'] : []); switch ($resourceClass = ltrim(get_class($resource), '\\')) { case 'RunOpenCode\\AssetsInjection\\Resource\\JavascriptStringResource': $result[] = $this->getJavascriptCodeHtml($resource->getSource(), $attributes); break; case 'RunOpenCode\\AssetsInjection\\Resource\\StylesheetStringResource': $result[] = $this->getStylesheetCodeHtml($resource->getSource(), $attributes); break; case 'RunOpenCode\\AssetsInjection\\Resource\\HttpResource': switch ($type = AssetType::guessAssetType($resource)) { case AssetType::JAVASCRIPT: $result[] = $this->getJavascriptIncludeHtml($resource->getSource(), $attributes); break; case AssetType::STYLESHEET: $result[] = $this->getStylesheetIncludeHtml($resource->getSource(), $attributes); break; default: throw new RuntimeException(sprintf('Unsupported type "%s".', $type)); break; } break; case 'RunOpenCode\\AssetsInjection\\Resource\\FileResource': switch ($options['path_type']) { case PathType::RELATIVE: $path = str_replace($options['web_root'], '', $resource->getSource()); break; case PathType::ABSOLUTE: $path = $options['http_root'] . str_replace($options['web_root'], '', $resource->getSource()); break; case PathType::RAW: $path = $resource->getSource(); break; default: throw new InvalidArgumentException(sprintf('Unknown path type "%s".', $options['path_type'])); break; } switch ($type = AssetType::guessAssetType($resource)) { case AssetType::JAVASCRIPT: $result[] = $this->getJavascriptIncludeHtml($path, $attributes); break; case AssetType::STYLESHEET: $result[] = $this->getStylesheetIncludeHtml($path, $attributes); break; default: throw new RuntimeException(sprintf('Unsupported type "%s".', $type)); break; } break; default: throw new RuntimeException(sprintf('Unsupported resource "%s".', $resourceClass)); break; } } return implode("\n", $result); }
public function testProcessAssetWithExcludedOptionalFilter() { $this->emptyOutput(); $sourceFile = realpath(__DIR__ . '/../Data/web/js/myjavascript.js'); $container = new Container(new LibraryCollection(array(new LibraryDefinition('mylib', array(new FileResource($sourceFile, array('filters' => array('?mockup')))))))); $result = $this->getCompilerPass(true)->process($container); $this->assertInstanceOf(CompilerPassResult::class, $result); $this->assertFalse($result->isProcessingStopped()); $files = glob(rtrim($this->outputDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '/*'); $this->assertSame(1, count($files), 'Only one file should be dumped into output dir.'); $this->assertSame(file_get_contents($sourceFile), file_get_contents($files[0]), 'Source and dumped files should be same.'); $this->assertSame('.dev.', substr($files[0], -7, 5), 'File name should contain ".dev.".'); $this->assertSame(AssetType::guessExtension($sourceFile), AssetType::guessExtension($files[0]), 'File should have proper extension.'); $this->assertFalse(array_key_exists('filters', $result->getContainer()->getLibraries()->getDefinition('mylib')->getResources()[0]->getOptions())); }