/** * {@inheritDoc} */ public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); $container->setParameter('hearsay_require_js.require_js_src', $config['require_js_src']); $container->setParameter('hearsay_require_js.initialize_template', $config['initialize_template']); $container->setParameter('hearsay_require_js.base_url', $config['base_url']); $container->setParameter('hearsay_require_js.base_dir', $this->getRealPath($config['base_dir'], $container)); // Add the base directory as an empty namespace $config['paths'][''] = array('location' => $config['base_dir'], 'external' => false); $hideUnoptimizedAssets = !isset($config['optimizer']['hide_unoptimized_assets']) ? false : $config['optimizer']['hide_unoptimized_assets']; // By using this flag as true, defines will be declared as their module names $declareModuleName = !isset($config['optimizer']['declare_module_name']) ? false : $config['optimizer']['declare_module_name']; $container->setParameter('hearsay_require_js.declare_module_name', $declareModuleName); foreach ($config['paths'] as $path => $settings) { $location = $settings['location']; if ($settings['external']) { $this->addExternalNamespaceMapping($location, $path, $container); } else { is_array($location) && ($location = array_shift($location)); $this->addNamespaceMapping($location, $path, $container, !$hideUnoptimizedAssets); } } $container->setParameter('hearsay_require_js.shim', $config['shim']); $configurationBuilder = $container->getDefinition('hearsay_require_js.configuration_builder'); foreach ($config['options'] as $option => $settings) { $configurationBuilder->addMethodCall('addOption', array($option, $settings['value'])); } if (!isset($config['optimizer'])) { // If the r.js optimizer config isn't provided, don't provide the Assetic filter $container->removeDefinition('hearsay_require_js.optimizer_filter'); } else { $container->setParameter('hearsay_require_js.r.path', $this->getRealPath($config['optimizer']['path'], $container)); $filter = $container->getDefinition('hearsay_require_js.optimizer_filter'); if ($config['optimizer']['almond_path']) { // Makes almond path relative to base directory for r.js optimization $almondPath = UrlGenerator::getRelativePath($container->getParameter('hearsay_require_js.base_dir'), $this->getRealPath($config['optimizer']['almond_path'], $container)); // Removes file extension if it exists $almondPath = preg_replace('/\\.js$/', '', $almondPath); $filter->addMethodCall('setAlmondPath', array($almondPath)); $configurationBuilder->addMethodCall('setUseAlmond', array(true)); } $filter->addMethodCall('setShim', array($config['shim'])); $filter->addMethodCall('setTimeout', array($config['optimizer']['timeout'])); foreach ($config['optimizer']['exclude'] as $exclude) { $filter->addMethodCall('addExclude', array($exclude)); } foreach ($config['optimizer']['modules'] as $name => $module) { $filter->addMethodCall('addModule', array($name, $module)); } foreach ($config['optimizer']['options'] as $name => $settings) { $filter->addMethodCall('addOption', array($name, $settings['value'])); } } }
/** * @dataProvider provideRelativePaths */ public function testGetRelativePath($sourcePath, $targetPath, $expectedPath) { $this->assertSame($expectedPath, UrlGenerator::getRelativePath($sourcePath, $targetPath)); }
/** * Returns the target path as relative reference from the base path. * * @param string $basePath The base path * @param string $targetPath The target path * * @return string The relative target path */ protected function getRelativePath($basePath, $targetPath) { return UrlGenerator::getRelativePath($basePath, $targetPath); }
/** * @param string $pathInfo * @param bool|string $referenceType * * @return string */ protected function getUrlOrPathForType($pathInfo, $referenceType) { $url = $pathInfo; $scheme = $this->context->getScheme(); if (self::NETWORK_PATH !== $referenceType && ($scheme === 'http' && $this->sslEnabled === true || $scheme === 'https' && $this->sslEnabled === false)) { $referenceType = self::ABSOLUTE_URL; } switch ($referenceType) { case self::ABSOLUTE_URL: case self::NETWORK_PATH: $url = $this->buildUrl($pathInfo, $referenceType); break; case self::ABSOLUTE_PATH: $url = $pathInfo; break; case self::RELATIVE_PATH: $url = UrlGenerator::getRelativePath($this->context->getPathInfo(), $pathInfo); break; } return $url; }