├── .gitignore ├── .travis.yml ├── Asset └── GulpRevVersionStrategy.php ├── DependencyInjection ├── Compiler │ └── SetVersionStrategyCompiler.php ├── Configuration.php └── IrozgarGulpRevVersionsExtension.php ├── IrozgarGulpRevVersionsBundle.php ├── LICENSE ├── README.md ├── Resources └── config │ └── services.xml ├── Tests ├── Asset │ └── GulpRevVersionStrategyTest.php ├── DependencyInjection │ ├── Compiler │ │ └── SetVersionStrategyCompilerTest.php │ ├── ConfigurationTest.php │ └── IrozgarGulpRevVersionsExtensionTest.php ├── Fixtures │ └── rev-manifest.json ├── Functional │ ├── ControllerResponseTest.php │ ├── GulpRevVersionStrategyTest.php │ ├── KernelTestCase.php │ ├── TestBundle │ │ ├── Controller │ │ │ └── TestController.php │ │ ├── Resources │ │ │ └── config │ │ │ │ └── routing.yml │ │ └── TestBundle.php │ ├── app │ │ ├── AppKernel.php │ │ ├── Resources │ │ │ ├── assets │ │ │ │ └── rev-manifest.json │ │ │ └── views │ │ │ │ └── test.html.twig │ │ ├── config │ │ │ ├── config.yml │ │ │ └── config_previous_3-1.yml │ │ └── routing.yml │ └── web │ │ └── script.js └── IrozgarGulpRevVersionsBundleTest.php ├── composer.json └── phpunit.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /composer.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | cache: 4 | directories: 5 | - $HOME/.composer/cache 6 | 7 | php: 8 | - '7.3' 9 | - '7.2' 10 | - '7.1' 11 | 12 | env: 13 | - SYMFONY_VERSION="2.8.*" 14 | - SYMFONY_VERSION="3.4.*" 15 | 16 | before_install: 17 | - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi; 18 | - if [ "$SYMFONY_VERSION" == "3.0.*" ]; then composer require "twig/twig:~1.28" --no-update; fi; 19 | 20 | install: 21 | - composer update --prefer-source --no-interaction $COMPOSER_FLAGS 22 | 23 | script: 24 | - vendor/bin/phpunit 25 | -------------------------------------------------------------------------------- /Asset/GulpRevVersionStrategy.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class GulpRevVersionStrategy implements VersionStrategyInterface 20 | { 21 | private $manifestPath; 22 | 23 | private $paths = array(); 24 | 25 | private $kernelRootDir; 26 | 27 | /** 28 | * VersionStrategy constructor. 29 | * 30 | * @param string $kernelRootDir 31 | * @param string $manifestPath 32 | */ 33 | public function __construct($kernelRootDir, $manifestPath) 34 | { 35 | $this->manifestPath = $manifestPath; 36 | $this->kernelRootDir = $kernelRootDir; 37 | } 38 | 39 | public function getVersion($path) 40 | { 41 | if (file_exists($path)) { 42 | return null; 43 | } 44 | 45 | $path = pathinfo($this->getAssetVersion($path)); 46 | $filenameParts = explode('-', $path['filename']); 47 | 48 | // With gulp rev, the version is at the end of the filename so it will be the last item of the array 49 | return $filenameParts[count($filenameParts) - 1]; 50 | } 51 | 52 | public function applyVersion($path) 53 | { 54 | return $this->getAssetVersion($path); 55 | } 56 | 57 | private function getAssetVersion($path) 58 | { 59 | // The twig extension is a singleton so we store the loaded content into a property to read it only once 60 | // @see https://knpuniversity.com/screencast/gulp/version-cache-busting#comment-2884388919 61 | if (count($this->paths) === 0) { 62 | $this->loadManifestFile(); 63 | } 64 | 65 | if (isset($this->paths[$path])) { 66 | return $this->paths[$path]; 67 | } 68 | 69 | // If a file exists, it doesn't have a version so we ignore it 70 | if (!file_exists($this->kernelRootDir.'/../web/'.$path)) { 71 | throw new Exception(sprintf('The file "%s" does not exist and there is no version file for it', $path)); 72 | } 73 | 74 | return $path; 75 | } 76 | 77 | private function loadManifestFile() 78 | { 79 | $manifestPath = $this->kernelRootDir.'/'.$this->manifestPath; 80 | $manifestFilename = basename($manifestPath); 81 | 82 | if (!is_file($manifestPath)) { 83 | throw new Exception( 84 | sprintf( 85 | 'Manifest file "%s" not found in path "%s". You can generate this file running gulp', 86 | $manifestFilename, 87 | $this->manifestPath 88 | ) 89 | ); 90 | } 91 | 92 | $this->paths = json_decode(file_get_contents($manifestPath), true); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /DependencyInjection/Compiler/SetVersionStrategyCompiler.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class SetVersionStrategyCompiler implements CompilerPassInterface 24 | { 25 | private $map = array( 26 | 'Symfony\\Component\\Asset\\Package' => 0, 27 | 'Symfony\\Component\\Asset\\PathPackage' => 1, 28 | 'Symfony\\Component\\Asset\\UrlPackage' => 1, 29 | ); 30 | 31 | public function process(ContainerBuilder $container) 32 | { 33 | if (!$container->has('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy')) { 34 | return; 35 | } 36 | 37 | // Replace the default strategy 38 | if ($container->getParameter('gulp_rev_replace_strategy')) { 39 | $this->replaceDefaultStrategy($container); 40 | } 41 | 42 | $this->replaceStrategy($container); 43 | } 44 | 45 | private function getIndexForVersionStrategyArgument($class) 46 | { 47 | if (!isset($this->map[$class])) { 48 | throw new Exception(sprintf('Packages of type "%s" are not supported by IrozgarGulpRevBundle', $class)); 49 | } 50 | 51 | return $this->map[$class]; 52 | } 53 | 54 | private function getPackageClass(Definition $package, ContainerBuilder $container) 55 | { 56 | if ($package->getClass() !== null) { 57 | return $package->getClass(); 58 | } 59 | 60 | if ($package instanceof DefinitionDecorator) { 61 | $parent = $container->getDefinition($package->getParent()); 62 | $class = $this->getPackageClass($parent, $container); 63 | if ($class === null) { 64 | throw new Exception( 65 | sprintf('Unable to resolve the class of the service "%s"', $package->getParent()) 66 | ); 67 | } 68 | 69 | return $class; 70 | } 71 | 72 | return null; 73 | } 74 | 75 | /** 76 | * @param ContainerBuilder $container 77 | * 78 | * @throws Exception 79 | */ 80 | private function replaceDefaultStrategy(ContainerBuilder $container) 81 | { 82 | if (!$container->has('assets._default_package')) { 83 | throw new Exception('Default package does not exist.'); 84 | } 85 | 86 | $defaultPackage = $container->getDefinition('assets._default_package'); 87 | 88 | $class = $this->getPackageClass($defaultPackage, $container); 89 | 90 | if ($class === null) { 91 | throw new Exception( 92 | sprintf('Unable to resolve the class of the service "%s"', 'assets._default_package') 93 | ); 94 | } 95 | 96 | $index = $this->getIndexForVersionStrategyArgument($class); 97 | 98 | $defaultPackage->replaceArgument( 99 | $index, 100 | new Reference('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy') 101 | ); 102 | } 103 | 104 | private function replaceStrategy(ContainerBuilder $container) 105 | { 106 | $packages = $container->getDefinition('assets.packages')->getArgument(1); 107 | $packagesToReplace = $this->createNamedPackageIdsArray($container->getParameter('irozgar_gulp_rev.packages')); 108 | 109 | foreach ($packages as $package) { 110 | if (!in_array($package, $packagesToReplace)) { 111 | continue; 112 | } 113 | 114 | $definition = $container->getDefinition((string) $package); 115 | $definition->replaceArgument(1, new Reference('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy')); 116 | } 117 | } 118 | 119 | private function createNamedPackageIdsArray(array $names) 120 | { 121 | return array_map(function ($name) { 122 | return 'assets._package_'.$name; 123 | }, $names); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | root('irozgar_gulp_rev_versions'); 24 | 25 | $rootNode 26 | ->children() 27 | ->scalarNode('manifest_path') 28 | ->treatNullLike(self::DEFAULT_MANIFEST_PATH) 29 | ->defaultValue(self::DEFAULT_MANIFEST_PATH) 30 | ->end() 31 | ->booleanNode('replace_default_version_strategy') 32 | ->treatNullLike(true) 33 | ->defaultFalse() 34 | ->end() 35 | ->arrayNode('packages') 36 | ->prototype('scalar') 37 | ->end() 38 | ->end() 39 | ->end() 40 | ; 41 | 42 | return $treeBuilder; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /DependencyInjection/IrozgarGulpRevVersionsExtension.php: -------------------------------------------------------------------------------- 1 | processConfiguration($configuration, $configs); 24 | 25 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 26 | $loader->load('services.xml'); 27 | 28 | $container->setParameter('gulp_rev_manifest_path', $config['manifest_path']); 29 | $container->setParameter('gulp_rev_replace_strategy', $config['replace_default_version_strategy']); 30 | $container->setParameter('irozgar_gulp_rev.packages', $config['packages']); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /IrozgarGulpRevVersionsBundle.php: -------------------------------------------------------------------------------- 1 | = 30300) { 18 | $deprecationMessage .= 'Since version 3.3, symfony includes the option "json_manifest_path" that does the '. 19 | 'same as this bundle, I recommend using that instead of this bundle.'; 20 | } 21 | if (Kernel::VERSION_ID < 30300) { 22 | $deprecationMessage .= 'I recommend updating your symfony version to the last stable version and use '. 23 | 'the option "json_manifest_path" included in symfony since version 3.3.'; 24 | } 25 | trigger_error($deprecationMessage, E_USER_DEPRECATED); 26 | } 27 | 28 | public function build(ContainerBuilder $container) 29 | { 30 | parent::build($container); 31 | 32 | // Register only for versions lower than Symfony 3.1 because the don't have the option for 33 | // setting the version_strategy 34 | $this->addCompilerPassWhenVersionIsLowerThan($container, new SetVersionStrategyCompiler(), 30100); 35 | } 36 | 37 | public function addCompilerPassWhenVersionIsLowerThan( 38 | ContainerBuilder $container, 39 | CompilerPassInterface $compilerPass, 40 | $maxVersion, 41 | $currentVersion = Kernel::VERSION_ID 42 | ) { 43 | if ($currentVersion < $maxVersion) { 44 | $container->addCompilerPass($compilerPass); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Isaac Rozas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GulpRevVersionsBundle 2 | ===================== 3 | [![Build Status](https://travis-ci.org/irozgar/gulp-rev-versions-bundle.svg?branch=master)](https://travis-ci.org/irozgar/gulp-rev-versions-bundle) 4 | [![SensioLabsInsight](https://insight.sensiolabs.com/projects/f85b2945-53fe-4511-b3f0-ab93111b62a2/mini.png)](https://insight.sensiolabs.com/projects/f85b2945-53fe-4511-b3f0-ab93111b62a2) 5 | 6 | This bundle helps you using your assets versioned with gulp-rev in a symfony project by 7 | making the twig function `asset` return the files mapped in your gulp-rev manifest. 8 | 9 | **DEPRECATED** This bundle is deprecated and will be abandoned when symfony 2.8 support finishes on November 2019. Since 10 | version 3.3, symfony includes the option [_json_manifest_path_](http://symfony.com/doc/3.3/reference/configuration/framework.html#reference-assets-json-manifest-path) that does the same as this bundle, I recommend using that 11 | instead of this bundle. For previous versions the recommendation is to update symfony to a stable version and start 12 | using the option _json_manifest_path_. 13 | 14 | Installation 15 | ------------ 16 | 17 | ### Step 1. Download with composer 18 | 19 | ```bash 20 | composer require irozgar/gulp-rev-versions-bundle 21 | ``` 22 | 23 | ### Step 2. Add the bundle to AppKernel 24 | 25 | ```php 26 | = 3.1 && < 4.0 67 | 68 | This symfony version introduced a new option to configure the version strategy. 69 | 70 | Add this to your config.yml to tell symfony what version strategy it should use 71 | ```yaml 72 | # app/config/config.yml 73 | 74 | framework: 75 | # ... 76 | assets: 77 | version_strategy: irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy 78 | 79 | # ... 80 | 81 | # This is only needed if using a custom path for the manifest file 82 | irozgar_gulp_rev_versions: 83 | manifest_path: "your/custom/path/rev-manifest.json" 84 | ``` 85 | 86 | **NOTE** Since symfony 3.3 the framework includes a version strategy to load assets using a manifest file. 87 | [more info](http://symfony.com/doc/current/reference/configuration/framework.html#reference-assets-json-manifest-path) 88 | 89 | #### Configuring the manifest file path 90 | 91 | The default location of the rev-manifest.json file is _app/Resources/assets/rev-manifest.json_. 92 | You can customize it by adding the following lines to your config.yml 93 | 94 | ```yaml 95 | # app/config/config.yml 96 | 97 | irozgar_gulp_rev_versions: 98 | manifest_path: "your/custom/path/rev-manifest.json" 99 | ``` 100 | 101 | **NOTE** All paths will be relative to `%kernel.root_dir%` 102 | -------------------------------------------------------------------------------- /Resources/config/services.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | %kernel.root_dir% 10 | %gulp_rev_manifest_path% 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Tests/Asset/GulpRevVersionStrategyTest.php: -------------------------------------------------------------------------------- 1 | assertEquals('07fb3d8168', $strategy->getVersion('js/scripts.js')); 21 | } 22 | 23 | public function testGetApplyVersion() 24 | { 25 | $strategy = new GulpRevVersionStrategy(__DIR__.'/../', 'Fixtures/rev-manifest.json'); 26 | 27 | $this->assertEquals('js/scripts-07fb3d8168.js', $strategy->applyVersion('js/scripts.js')); 28 | } 29 | 30 | public function testPathsAreLoadedAfterFirstUse() 31 | { 32 | $strategy = new GulpRevVersionStrategy(__DIR__.'/../', 'Fixtures/rev-manifest.json'); 33 | 34 | $strategy->applyVersion('js/scripts.js'); 35 | 36 | $this->assertAttributeNotEmpty('paths', $strategy); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Tests/DependencyInjection/Compiler/SetVersionStrategyCompilerTest.php: -------------------------------------------------------------------------------- 1 | getMockBuilder('Irozgar\GulpRevVersionsBundle\Asset\GulpRevVersionStrategy') 25 | ->disableOriginalConstructor() 26 | ->getMock(); 27 | $package = $this->createDefaultPackageDefinition($container); 28 | $this->createPackagesDefinition($container, array()); 29 | 30 | $container->setParameter('gulp_rev_replace_strategy', true); 31 | $container->setParameter('irozgar_gulp_rev.packages', array('mycdn')); 32 | $container->register('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', $strategy); 33 | 34 | $compiler = new SetVersionStrategyCompiler(); 35 | $compiler->process($container); 36 | 37 | $this->assertEquals( 38 | 'irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', 39 | (string) $package->getArgument(1) 40 | ); 41 | } 42 | 43 | public function testDoNotAddTheVersionStrategyWhenOptionIsNotEnabled() 44 | { 45 | $container = new ContainerBuilder(); 46 | 47 | $strategy = $this->getMockBuilder('Irozgar\GulpRevVersionsBundle\Asset\GulpRevVersionStrategy') 48 | ->disableOriginalConstructor() 49 | ->getMock(); 50 | $package = $this->createDefaultPackageDefinition($container); 51 | $this->createPackagesDefinition($container, array()); 52 | 53 | $container->setParameter('gulp_rev_replace_strategy', false); 54 | $container->setParameter('irozgar_gulp_rev.packages', array('mycdn')); 55 | $container->register('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', $strategy); 56 | 57 | $compiler = new SetVersionStrategyCompiler(); 58 | $compiler->process($container); 59 | 60 | $this->assertEquals( 61 | (string) $this->createVersion($container), 62 | (string) $package->getArgument(1) 63 | ); 64 | } 65 | 66 | public function testReplacesNamedPackages() 67 | { 68 | $container = new ContainerBuilder(); 69 | 70 | $strategy = $this->getMockBuilder('Irozgar\GulpRevVersionsBundle\Asset\GulpRevVersionStrategy') 71 | ->disableOriginalConstructor() 72 | ->getMock(); 73 | 74 | // Create packages name 75 | $packages = array(); 76 | $namedPackageDefinition = array(); 77 | foreach (array('mycdn', 'another') as $name) { 78 | /* @var DefinitionDecorator[] $namedPackageDefinition */ 79 | $namedPackageDefinition[$name] = $this->createNamedPackageDefinition($name, $container); 80 | $packages[$name] = new Reference($this->getNamedPackageId($name)); 81 | } 82 | $this->createDefaultPackageDefinition($container); 83 | $this->createPackagesDefinition($container, $packages); 84 | 85 | $container->setParameter('gulp_rev_replace_strategy', true); 86 | $container->setParameter('irozgar_gulp_rev.packages', array('mycdn', 'another')); 87 | $container->register('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', $strategy); 88 | 89 | $compiler = new SetVersionStrategyCompiler(); 90 | $compiler->process($container); 91 | 92 | $this->assertEquals( 93 | 'irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', 94 | (string) $namedPackageDefinition['mycdn']->getArgument(1) 95 | ); 96 | $this->assertEquals( 97 | 'irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', 98 | (string) $namedPackageDefinition['another']->getArgument(1) 99 | ); 100 | } 101 | 102 | public function testReplacesOnlyConfiguredNamedPackages() 103 | { 104 | $container = new ContainerBuilder(); 105 | 106 | $strategy = $this->getMockBuilder('Irozgar\GulpRevVersionsBundle\Asset\GulpRevVersionStrategy') 107 | ->disableOriginalConstructor() 108 | ->getMock(); 109 | 110 | // Create packages name 111 | $packages = array(); 112 | $namedPackageDefinition = array(); 113 | foreach (array('mycdn', 'another') as $name) { 114 | /* @var DefinitionDecorator[] $namedPackageDefinition */ 115 | $namedPackageDefinition[$name] = $this->createNamedPackageDefinition($name, $container); 116 | $packages[$name] = new Reference($this->getNamedPackageId($name)); 117 | } 118 | $defaultPackageDefinition = $this->createDefaultPackageDefinition($container); 119 | $this->createPackagesDefinition($container, $packages); 120 | 121 | $container->setParameter('gulp_rev_replace_strategy', false); 122 | $container->setParameter('irozgar_gulp_rev.packages', array('mycdn')); 123 | $container->register('irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', $strategy); 124 | 125 | $compiler = new SetVersionStrategyCompiler(); 126 | $compiler->process($container); 127 | 128 | $this->assertEquals( 129 | 'assets.empty_version_strategy', 130 | (string) $defaultPackageDefinition->getArgument(1) 131 | ); 132 | $this->assertEquals( 133 | 'irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy', 134 | (string) $namedPackageDefinition['mycdn']->getArgument(1) 135 | ); 136 | $this->assertEquals( 137 | 'assets.empty_version_strategy', 138 | (string) $namedPackageDefinition['another']->getArgument(1) 139 | ); 140 | } 141 | 142 | private function createDefaultPackageDefinition(ContainerBuilder $container) 143 | { 144 | $package = new Definition('Symfony\Component\Asset\PathPackage'); 145 | $package->setAbstract(true); 146 | $decoratedPackage = new DefinitionDecorator('assets.path_package'); 147 | $decoratedPackage->setArguments(array('/', $this->createVersion($container))); 148 | 149 | $container->setDefinition('assets.path_package', $package); 150 | $container->setDefinition('assets._default_package', $decoratedPackage); 151 | 152 | return $decoratedPackage; 153 | } 154 | 155 | private function createNamedPackageDefinition($name, ContainerBuilder $container) 156 | { 157 | $package = new Definition('Symfony\Component\Asset\PathPackage'); 158 | $package->setAbstract(true); 159 | $decoratedPackage = new DefinitionDecorator('assets.path_package'); 160 | $decoratedPackage->setArguments(array('/', $this->createVersion($container))); 161 | 162 | $container->setDefinition($this->getNamedPackageId($name), $decoratedPackage); 163 | 164 | return $decoratedPackage; 165 | } 166 | 167 | private function createVersion(ContainerBuilder $container) 168 | { 169 | $version = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy') 170 | ->disableOriginalConstructor() 171 | ->getMock(); 172 | $container->register('assets.empty_version_strategy', $version); 173 | 174 | return new Reference('assets.empty_version_strategy'); 175 | } 176 | 177 | /** 178 | * @param $name 179 | * 180 | * @return string 181 | */ 182 | private function getNamedPackageId($name) 183 | { 184 | return 'assets._package_'.$name; 185 | } 186 | 187 | /** 188 | * @param ContainerBuilder $container 189 | * @param array $packages 190 | */ 191 | private function createPackagesDefinition(ContainerBuilder $container, array $packages) 192 | { 193 | $container->setDefinition('assets.packages', new Definition('Symfony\Component\Asset\Packages', array( 194 | new Reference('assets._default_package'), 195 | $packages, 196 | ))); 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /Tests/DependencyInjection/ConfigurationTest.php: -------------------------------------------------------------------------------- 1 | processConfiguration($configuration, array()); 23 | 24 | $this->assertArrayHasKey('manifest_path', $config); 25 | $this->assertContains('Resources/assets/rev-manifest.json', $config); 26 | } 27 | 28 | public function testNullManifestPathUsesDefaultPath() 29 | { 30 | $configuration = new Configuration(); 31 | $processor = new Processor(); 32 | $config = $processor->processConfiguration($configuration, array($this->getEmptyConfig())); 33 | 34 | $this->assertArrayHasKey('manifest_path', $config); 35 | $this->assertContains('Resources/assets/rev-manifest.json', $config); 36 | } 37 | 38 | public function testEmptyReplaceDefaultVersionStrategy() 39 | { 40 | $configuration = new Configuration(); 41 | $processor = new Processor(); 42 | $config = $processor->processConfiguration($configuration, array()); 43 | 44 | $this->assertArrayHasKey('replace_default_version_strategy', $config); 45 | $this->assertFalse($config['replace_default_version_strategy']); 46 | } 47 | 48 | public function testNullReplaceDefaultVersionStrategy() 49 | { 50 | $configuration = new Configuration(); 51 | $processor = new Processor(); 52 | $config = $processor->processConfiguration($configuration, array($this->getEmptyConfig())); 53 | 54 | $this->assertArrayHasKey('replace_default_version_strategy', $config); 55 | $this->assertTrue($config['replace_default_version_strategy']); 56 | } 57 | 58 | public function getEmptyConfig() 59 | { 60 | $yaml = <<<'EOF' 61 | manifest_path: ~ 62 | replace_default_version_strategy: ~ 63 | EOF; 64 | 65 | $parser = new Parser(); 66 | 67 | return $parser->parse($yaml); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Tests/DependencyInjection/IrozgarGulpRevVersionsExtensionTest.php: -------------------------------------------------------------------------------- 1 | configuration = new ContainerBuilder(); 27 | } 28 | 29 | public function testAddsParameterWithManifestPathToContainer() 30 | { 31 | $loader = new IrozgarGulpRevVersionsExtension(); 32 | $config = $this->getEmptyConfig(); 33 | $loader->load(array($config), $this->configuration); 34 | 35 | $this->assertEquals(Configuration::DEFAULT_MANIFEST_PATH, $this->configuration->getParameter('gulp_rev_manifest_path')); 36 | } 37 | 38 | public function testAddsPackagesArrayToContainer() 39 | { 40 | $loader = new IrozgarGulpRevVersionsExtension(); 41 | $config = $this->getConfigWithPackages(); 42 | $loader->load(array($config), $this->configuration); 43 | 44 | $this->assertEquals(array('mycdn', 'another'), $this->configuration->getParameter('irozgar_gulp_rev.packages')); 45 | } 46 | 47 | public function getEmptyConfig() 48 | { 49 | $yaml = <<<'EOF' 50 | manifest_path: ~ 51 | EOF; 52 | 53 | $parser = new Parser(); 54 | 55 | return $parser->parse($yaml); 56 | } 57 | 58 | public function getConfigWithPackages() 59 | { 60 | $yaml = <<<'EOF' 61 | manifest_path: ~ 62 | packages: 63 | - mycdn 64 | - another 65 | EOF; 66 | 67 | $parser = new Parser(); 68 | 69 | return $parser->parse($yaml); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Tests/Fixtures/rev-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "js/scripts.js": "js/scripts-07fb3d8168.js" 3 | } -------------------------------------------------------------------------------- /Tests/Functional/ControllerResponseTest.php: -------------------------------------------------------------------------------- 1 | request('GET', '/'); 15 | 16 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); 17 | $this->assertEquals("/styles-a41d8cd1.css", $crawler->filterXPath('//link/@href')->text()); 18 | } 19 | 20 | public function testIgnoresFiles() 21 | { 22 | $client = static::createClient(); 23 | 24 | $crawler = $client->request('GET', '/'); 25 | 26 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); 27 | $this->assertEquals("/script.js", $crawler->filterXPath('//script/@src')->text()); 28 | } 29 | 30 | protected static function getKernelClass() 31 | { 32 | return AppKernel::class; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Tests/Functional/GulpRevVersionStrategyTest.php: -------------------------------------------------------------------------------- 1 | getContainer()->get('assets.packages'); 17 | 18 | $this->assertAttributeInstanceOf(GulpRevVersionStrategy::class, 'versionStrategy', $service->getPackage()); 19 | } 20 | 21 | public function testVersionStrategyForNamedPackageIsCorrectlySet() 22 | { 23 | static::bootKernel(); 24 | 25 | /** @var Packages $service */ 26 | $service = static::$kernel->getContainer()->get('assets.packages'); 27 | 28 | $this->assertAttributeInstanceOf(GulpRevVersionStrategy::class, 'versionStrategy', $service->getPackage('main')); 29 | $this->assertAttributeInstanceOf(EmptyVersionStrategy::class, 'versionStrategy', $service->getPackage('unversioned')); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Tests/Functional/KernelTestCase.php: -------------------------------------------------------------------------------- 1 | boot(); 29 | } 30 | 31 | /** 32 | * Creates a Kernel. 33 | * 34 | * Available options: 35 | * 36 | * * environment 37 | * * debug 38 | * 39 | * @param array $options An array of options 40 | * 41 | * @return KernelInterface A KernelInterface instance 42 | */ 43 | protected static function createKernel(array $options = array()) 44 | { 45 | return new AppKernel( 46 | isset($options['environment']) ? $options['environment'] : 'test', 47 | isset($options['debug']) ? $options['debug'] : true 48 | ); 49 | } 50 | 51 | /** 52 | * Shuts the kernel down if it was used in the test. 53 | */ 54 | protected static function ensureKernelShutdown() 55 | { 56 | if (null !== static::$kernel) { 57 | $container = static::$kernel->getContainer(); 58 | static::$kernel->shutdown(); 59 | if ($container instanceof ResettableContainerInterface) { 60 | $container->reset(); 61 | } 62 | } 63 | } 64 | 65 | /** 66 | * Clean up Kernel usage in this test. 67 | */ 68 | protected function tearDown() 69 | { 70 | static::ensureKernelShutdown(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Tests/Functional/TestBundle/Controller/TestController.php: -------------------------------------------------------------------------------- 1 | render('test.html.twig'); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Tests/Functional/TestBundle/Resources/config/routing.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irozgar/gulp-rev-versions-bundle/0e47327ac485e433c8ccec06344a49239e6d7079/Tests/Functional/TestBundle/Resources/config/routing.yml -------------------------------------------------------------------------------- /Tests/Functional/TestBundle/TestBundle.php: -------------------------------------------------------------------------------- 1 | environment; 25 | } 26 | 27 | public function getLogDir() 28 | { 29 | return sys_get_temp_dir(). '/'. Kernel::VERSION.'/logs/'; 30 | } 31 | 32 | public function registerBundles() 33 | { 34 | return [ 35 | new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), 36 | new \Symfony\Bundle\TwigBundle\TwigBundle(), 37 | new \Irozgar\GulpRevVersionsBundle\Tests\Functional\TestBundle\TestBundle(), 38 | new \Irozgar\GulpRevVersionsBundle\IrozgarGulpRevVersionsBundle(), 39 | ]; 40 | } 41 | 42 | public function registerContainerConfiguration(LoaderInterface $loader) 43 | { 44 | if (Kernel::VERSION_ID < 30100) { 45 | $loader->load(__DIR__ .'/config/config_previous_3-1.yml'); 46 | } else { 47 | $loader->load(__DIR__ .'/config/config.yml'); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Tests/Functional/app/Resources/assets/rev-manifest.json: -------------------------------------------------------------------------------- 1 | { "styles.css": "styles-a41d8cd1.css", "unicorn.css": "unicorn-d41d8cd98f.css" } 2 | -------------------------------------------------------------------------------- /Tests/Functional/app/Resources/views/test.html.twig: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Test template 6 | 7 | 8 | 9 |

Test template

10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Tests/Functional/app/config/config.yml: -------------------------------------------------------------------------------- 1 | framework: 2 | secret: test 3 | router: { resource: "%kernel.root_dir%/routing.yml" } 4 | test: ~ 5 | assets: 6 | version_strategy: irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy 7 | packages: 8 | main: 9 | version_strategy: irozgar_gulp_rev_versions.asset.gulp_rev_version_strategy 10 | unversioned: 11 | version_strategy: assets.empty_version_strategy 12 | 13 | services: 14 | logger: { class: Psr\Log\NullLogger } 15 | 16 | -------------------------------------------------------------------------------- /Tests/Functional/app/config/config_previous_3-1.yml: -------------------------------------------------------------------------------- 1 | framework: 2 | secret: test 3 | router: { resource: "%kernel.root_dir%/routing.yml" } 4 | test: ~ 5 | assets: 6 | packages: 7 | main: 8 | unversioned: 9 | templating: 10 | engines: ['twig'] 11 | 12 | services: 13 | logger: { class: Psr\Log\NullLogger } 14 | 15 | irozgar_gulp_rev_versions: 16 | replace_default_version_strategy: ~ 17 | 18 | packages: 19 | - main 20 | -------------------------------------------------------------------------------- /Tests/Functional/app/routing.yml: -------------------------------------------------------------------------------- 1 | welcome: 2 | path: / 3 | defaults: { _controller: TestBundle:Test:test } 4 | -------------------------------------------------------------------------------- /Tests/Functional/web/script.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irozgar/gulp-rev-versions-bundle/0e47327ac485e433c8ccec06344a49239e6d7079/Tests/Functional/web/script.js -------------------------------------------------------------------------------- /Tests/IrozgarGulpRevVersionsBundleTest.php: -------------------------------------------------------------------------------- 1 | build($container); 26 | 27 | $this->assertArrayHasInstanceOf( 28 | 'Irozgar\GulpRevVersionsBundle\DependencyInjection\Compiler\SetVersionStrategyCompiler', 29 | $container->getCompilerPassConfig()->getPasses() 30 | ); 31 | } 32 | } 33 | 34 | public function testCompilerPassIsNotAddedIfSymfonyVersionIsGreaterThanOrEquals30100() 35 | { 36 | if (Kernel::VERSION_ID >= 30100) { 37 | $container = new ContainerBuilder(); 38 | $bundle = new IrozgarGulpRevVersionsBundle(); 39 | $bundle->build($container); 40 | 41 | $this->assertArrayHasNoInstanceOf( 42 | 'Irozgar\GulpRevVersionsBundle\DependencyInjection\Compiler\SetVersionStrategyCompiler', 43 | $container->getCompilerPassConfig()->getPasses() 44 | ); 45 | } 46 | } 47 | 48 | public function testTriggersDeprecationErrorForSymfonyVersionsLowerThan30300() 49 | { 50 | if (Kernel::VERSION_ID >= 30300) { 51 | $this->markTestSkipped('Symfony version is greater or equal to 3.3'); 52 | } 53 | 54 | $originalErrorReporting = ini_get('error_reporting'); 55 | // Report deprecations messages during this test 56 | ini_set('error_reporting', '-1'); 57 | 58 | $deprecationMessage = ''; 59 | try { 60 | new IrozgarGulpRevVersionsBundle(); 61 | } catch (Throwable $e) { 62 | if (!$e instanceof PHPUnit_Framework_Error_Deprecated) { 63 | throw $e; 64 | 65 | } 66 | $deprecationMessage = $e->getMessage(); 67 | } finally { 68 | // Restore error_reporting 69 | ini_set('error_reporting', $originalErrorReporting); 70 | } 71 | 72 | $expectedDeprecationMessage = 'The bundle IrozgarGulpRevVersionsBundle is deprecated and will be abandoned '. 73 | 'when symfony 2.8 support finishes on November 2019. I recommend updating your symfony version to the '. 74 | 'last stable version and use the option "json_manifest_path" included in symfony since version 3.3.'; 75 | self::assertSame($expectedDeprecationMessage, $deprecationMessage); 76 | } 77 | 78 | public function testTriggersDeprecationErrorForSymfonyVersionsGreaterOrEqualTo30300() 79 | { 80 | if (Kernel::VERSION_ID < 30300) { 81 | $this->markTestSkipped('Symfony version is lower than 3.3'); 82 | } 83 | 84 | $originalErrorReporting = ini_get('error_reporting'); 85 | // Report deprecations messages during this test 86 | ini_set('error_reporting', '-1'); 87 | 88 | $deprecationMessage = ''; 89 | try { 90 | new IrozgarGulpRevVersionsBundle(); 91 | } catch (Throwable $e) { 92 | if (!$e instanceof PHPUnit_Framework_Error_Deprecated) { 93 | throw $e; 94 | } 95 | 96 | $deprecationMessage = $e->getMessage(); 97 | } finally { 98 | // Restore error_reporting 99 | ini_set('error_reporting', $originalErrorReporting); 100 | } 101 | 102 | 103 | $expectedDeprecationMessage = 'The bundle IrozgarGulpRevVersionsBundle is deprecated and will be abandoned '. 104 | 'when symfony 2.8 support finishes on November 2019. Since version 3.3, symfony includes the option '. 105 | '"json_manifest_path" that does the same as this bundle, I recommend using that instead of this bundle.'; 106 | self::assertSame($expectedDeprecationMessage, $deprecationMessage); 107 | } 108 | 109 | protected function assertArrayHasInstanceOf($class, $haystack) 110 | { 111 | $found = false; 112 | foreach ($haystack as $item) { 113 | if ($item instanceof $class) { 114 | $found = true; 115 | } 116 | } 117 | 118 | $this->assertTrue($found); 119 | } 120 | 121 | protected function assertArrayHasNoInstanceOf($class, $haystack) 122 | { 123 | $found = false; 124 | foreach ($haystack as $item) { 125 | if ($item instanceof $class) { 126 | $found = true; 127 | } 128 | } 129 | 130 | $this->assertFalse($found); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "irozgar/gulp-rev-versions-bundle", 3 | "description": "A bundle that allows symfony to get the version of assets versioned with gulp-rev", 4 | "keywords": [ 5 | "symfony", 6 | "bundle", 7 | "asset", 8 | "assets", 9 | "version", 10 | "gulp-rev", 11 | "rev-manifest", 12 | "twig", 13 | "css", 14 | "js", 15 | "javascript" 16 | ], 17 | "type": "library", 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "Isaac Rozas", 22 | "email": "irozgar@gmail.com" 23 | } 24 | ], 25 | "require": { 26 | "php": ">=5.6", 27 | "symfony/asset": "^2.7|^3.0", 28 | "symfony/config": "^2.7|^3.0", 29 | "symfony/dependency-injection": "^2.7|^3.0", 30 | "symfony/http-kernel": "^2.7|^3.0" 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit": "^4.8|^5.5", 34 | "symfony/yaml": "^2.7|^3.0", 35 | "symfony/framework-bundle": "^2.7|^3.0", 36 | "symfony/browser-kit": "^2.7|^3.0", 37 | "symfony/twig-bundle": "^2.7|^3.0" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "Irozgar\\GulpRevVersionsBundle\\": "" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Tests 17 | 18 | 19 | 20 | 21 | 22 | ./ 23 | 24 | ./Resources 25 | ./Tests 26 | 27 | 28 | 29 | 30 | --------------------------------------------------------------------------------