1<?php 2 3/* 4 * This file is part of the Silex framework. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Silex\Provider; 13 14use Pimple\Container; 15use Pimple\ServiceProviderInterface; 16use Symfony\Component\Asset\Packages; 17use Symfony\Component\Asset\Package; 18use Symfony\Component\Asset\PathPackage; 19use Symfony\Component\Asset\UrlPackage; 20use Symfony\Component\Asset\Context\RequestStackContext; 21use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy; 22use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy; 23use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy; 24 25/** 26 * Symfony Asset component Provider. 27 * 28 * @author Fabien Potencier <fabien@symfony.com> 29 */ 30class AssetServiceProvider implements ServiceProviderInterface 31{ 32 public function register(Container $app) 33 { 34 $app['assets.packages'] = function ($app) { 35 $packages = array(); 36 foreach ($app['assets.named_packages'] as $name => $package) { 37 $version = $app['assets.strategy_factory'](isset($package['version']) ? $package['version'] : null, isset($package['version_format']) ? $package['version_format'] : null, isset($package['json_manifest_path']) ? $package['json_manifest_path'] : null, $name); 38 39 $packages[$name] = $app['assets.package_factory'](isset($package['base_path']) ? $package['base_path'] : '', isset($package['base_urls']) ? $package['base_urls'] : array(), $version, $name); 40 } 41 42 return new Packages($app['assets.default_package'], $packages); 43 }; 44 45 $app['assets.default_package'] = function ($app) { 46 $version = $app['assets.strategy_factory']($app['assets.version'], $app['assets.version_format'], $app['assets.json_manifest_path'], 'default'); 47 48 return $app['assets.package_factory']($app['assets.base_path'], $app['assets.base_urls'], $version, 'default'); 49 }; 50 51 $app['assets.context'] = function ($app) { 52 return new RequestStackContext($app['request_stack']); 53 }; 54 55 $app['assets.base_path'] = ''; 56 $app['assets.base_urls'] = array(); 57 $app['assets.version'] = null; 58 $app['assets.version_format'] = null; 59 $app['assets.json_manifest_path'] = null; 60 61 $app['assets.named_packages'] = array(); 62 63 // prototypes 64 65 $app['assets.strategy_factory'] = $app->protect(function ($version, $format, $jsonManifestPath, $name) use ($app) { 66 if ($version && $jsonManifestPath) { 67 throw new \LogicException(sprintf('Asset package "%s" cannot have version and manifest.', $name)); 68 } 69 70 if ($version) { 71 return new StaticVersionStrategy($version, $format); 72 } 73 74 if ($jsonManifestPath) { 75 if (!class_exists('Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy')) { 76 throw new \RuntimeException('You must require symfony/asset >= 3.3 to use JSON manifest version strategy.'); 77 } 78 79 return new JsonManifestVersionStrategy($jsonManifestPath); 80 } 81 82 return new EmptyVersionStrategy(); 83 }); 84 85 $app['assets.package_factory'] = $app->protect(function ($basePath, $baseUrls, $version, $name) use ($app) { 86 if ($basePath && $baseUrls) { 87 throw new \LogicException(sprintf('Asset package "%s" cannot have base URLs and base paths.', $name)); 88 } 89 90 if (!$baseUrls) { 91 return new PathPackage($basePath, $version, $app['assets.context']); 92 } 93 94 return new UrlPackage($baseUrls, $version, $app['assets.context']); 95 }); 96 } 97} 98