diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 949fbb9adb590..fb217ee5f2201 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -57,12 +57,14 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['assets']); }) ->then(function ($v) { if (!isset($v['templating']) - || !$v['templating']['assets_version'] + || (!$v['templating']['assets_version'] + && !$v['templating']['assets_version_strategy']) && !count($v['templating']['assets_base_urls']['http']) && !count($v['templating']['assets_base_urls']['ssl']) && !count($v['templating']['packages']) ) { $v['assets'] = array( + 'version_strategy' => null, 'version' => null, 'version_format' => '%%s?%%s', 'base_path' => '', @@ -78,6 +80,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return isset($v['templating']); }) ->then(function ($v) { if ($v['templating']['assets_version'] + || $v['templating']['assets_version_strategy'] || count($v['templating']['assets_base_urls']['http']) || count($v['templating']['assets_base_urls']['ssl']) || count($v['templating']['packages']) @@ -89,7 +92,12 @@ public function getConfigTreeBuilder() throw new \LogicException('You cannot use assets settings under "framework.templating" and "assets" configurations in the same project.'); } + if (null !== $v['templating']['assets_version_strategy'] && null !== $v['templating']['assets_version']) { + throw new \LogicException('You cannot use version_strategy and version settings in assets configuration.'); + } + $v['assets'] = array( + 'version_strategy' => $v['templating']['assets_version_strategy'], 'version' => $v['templating']['assets_version'], 'version_format' => $v['templating']['assets_version_format'], 'base_path' => '', @@ -98,7 +106,12 @@ public function getConfigTreeBuilder() ); foreach ($v['templating']['packages'] as $name => $config) { + if (null !== $config['version_strategy'] && null !== $config['version']) { + throw new \LogicException('You cannot use version_strategy and version settings in same package.'); + } + $v['assets']['packages'][$name] = array( + 'version_strategy' => $config['version_strategy'], 'version' => null === $config['version'] ? null : (string) $config['version'], 'version_format' => $config['version_format'], 'base_path' => '', @@ -107,7 +120,25 @@ public function getConfigTreeBuilder() } } - unset($v['templating']['assets_version'], $v['templating']['assets_version_format'], $v['templating']['assets_base_urls'], $v['templating']['packages']); + unset($v['templating']['assets_version'], $v['templating']['assets_version_strategy'], $v['templating']['assets_version_format'], $v['templating']['assets_base_urls'], $v['templating']['packages']); + + return $v; + }) + ->end() + ->validate() + ->ifTrue(function ($v) { return isset($v['assets']); }) + ->then(function ($v) { + if (null !== $v['assets']['version_strategy'] && null !== $v['assets']['version']) { + throw new \LogicException('You cannot use version_strategy and version settings in assets configuration.'); + } + + if (count($v['assets']['packages'])) { + foreach ($v['assets']['packages'] as $config) { + if (null !== $config['version_strategy'] && null !== $config['version'] && '' !== $config['version']) { + throw new \LogicException('You cannot use version_strategy and version settings in same package.'); + } + } + } return $v; }) @@ -454,6 +485,7 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode) ->info('templating configuration') ->canBeUnset() ->children() + ->scalarNode('assets_version_strategy')->defaultNull()->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end() ->scalarNode('assets_version')->defaultNull()->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end() ->scalarNode('assets_version_format')->defaultValue('%%s?%%s')->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end() ->scalarNode('hinclude_default_template')->defaultNull()->end() @@ -530,6 +562,7 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode) ->prototype('array') ->fixXmlConfig('base_url') ->children() + ->scalarNode('version_strategy')->defaultNull()->end() ->scalarNode('version') ->defaultNull() ->beforeNormalization() @@ -576,6 +609,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->canBeUnset() ->fixXmlConfig('base_url') ->children() + ->scalarNode('version_strategy')->defaultNull()->end() ->scalarNode('version')->defaultNull()->end() ->scalarNode('version_format')->defaultValue('%%s?%%s')->end() ->scalarNode('base_path')->defaultValue('')->end() @@ -595,6 +629,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->prototype('array') ->fixXmlConfig('base_url') ->children() + ->scalarNode('version_strategy')->defaultNull()->end() ->scalarNode('version') ->defaultNull() ->beforeNormalization() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 6a810b39e02eb..e501f233a682e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -591,14 +591,22 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co { $loader->load('assets.xml'); - $defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], '_default'); + $defaultVersion = null; + + if ($config['version_strategy']) { + $defaultVersion = new Reference($config['version_strategy']); + } else { + $defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], '_default'); + } $defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion); $container->setDefinition('assets._default_package', $defaultPackage); $namedPackages = array(); foreach ($config['packages'] as $name => $package) { - if (!array_key_exists('version', $package)) { + if (null !== $package['version_strategy']) { + $version = new Reference($package['version_strategy']); + } else if (!array_key_exists('version', $package)) { $version = $defaultVersion; } else { $format = $package['version_format'] ?: $config['version_format']; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 4e382f677f71b..685ce2c25efc4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -136,6 +136,7 @@ + @@ -147,6 +148,7 @@ + @@ -160,6 +162,7 @@ + @@ -178,6 +181,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 48e9d9a0c57dd..dae3cb064282e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -119,6 +119,98 @@ public function testLegacyInvalidValueAssets() )); } + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + * @expectedExceptionMessage You cannot use version_strategy and version settings in assets configuration. + * @group legacy + */ + public function testLegacyInvalidVersionStrategy() + { + $processor = new Processor(); + $configuration = new Configuration(true); + $processor->processConfiguration($configuration, array( + array( + 'templating' => array( + 'engines' => null, + 'assets_base_urls' => '//example.com', + 'assets_version' => 1, + 'assets_version_strategy' => 'foo', + ), + ), + )); + } + + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + * @expectedExceptionMessage You cannot use version_strategy and version settings in same package. + * @group legacy + */ + public function testLegacyInvalidPackageVersionStrategy() + { + $processor = new Processor(); + $configuration = new Configuration(true); + $processor->processConfiguration($configuration, array( + array( + 'templating' => array( + 'engines' => null, + 'assets_base_urls' => '//example.com', + 'assets_version' => 1, + 'packages' => array( + 'foo' => array( + 'base_urls' => '//example.com', + 'version' => 1, + 'version_strategy' => 'foo', + ), + ), + ), + ), + )); + } + + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + * @expectedExceptionMessage You cannot use version_strategy and version settings in assets configuration. + */ + public function testInvalidVersionStrategy() + { + $processor = new Processor(); + $configuration = new Configuration(true); + $processor->processConfiguration($configuration, array( + array( + 'assets' => array( + 'base_urls' => '//example.com', + 'version' => 1, + 'version_strategy' => 'foo', + ), + ), + )); + } + + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + * @expectedExceptionMessage You cannot use version_strategy and version settings in same package. + */ + public function testInvalidPackageVersionStrategy() + { + $processor = new Processor(); + $configuration = new Configuration(true); + $processor->processConfiguration($configuration, array( + array( + 'assets' => array( + 'base_urls' => '//example.com', + 'version' => 1, + 'packages' => array( + 'foo' => array( + 'base_urls' => '//example.com', + 'version' => 1, + 'version_strategy' => 'foo', + ), + ), + ), + ), + )); + } + protected static function getBundleDefaultConfig() { return array( @@ -183,6 +275,7 @@ protected static function getBundleDefaultConfig() 'enabled' => false, ), 'assets' => array( + 'version_strategy' => null, 'version' => null, 'version_format' => '%%s?%%s', 'base_path' => '', diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php index 1055251004bd9..c4265ff174fb1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php @@ -24,6 +24,10 @@ 'version' => null, 'base_urls' => array('https://bar3.example.com'), ), + 'bar_version_strategy' => array( + 'base_urls' => array('https://bar2.example.com'), + 'version_strategy' => 'assets.custom_version_strategy', + ), ), ), )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets_version_strategy_as_service.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets_version_strategy_as_service.php new file mode 100644 index 0000000000000..4f9123aefb0f5 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets_version_strategy_as_service.php @@ -0,0 +1,8 @@ +loadFromExtension('framework', array( + 'assets' => array( + 'version_strategy' => 'assets.custom_version_strategy', + 'base_urls' => 'http://cdn.example.com', + ), +)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets.php index 15df1015be15d..a4d06b9fac80e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets.php @@ -21,7 +21,10 @@ 'bar_null_version' => array( 'version' => null, 'base_urls' => array('https://bar3.example.com'), + 'bar_version_strategy' => array( + 'base_urls' => array('https://bar2.example.com'), + 'version_strategy' => 'assets.custom_version_strategy', ), ), ), -)); +), ); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets_version_strategy_as_service.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets_version_strategy_as_service.php new file mode 100644 index 0000000000000..9188ab9a6f42c --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/legacy_templating_assets_version_strategy_as_service.php @@ -0,0 +1,9 @@ +loadFromExtension('framework', array( + 'templating' => array( + 'engines' => array('php'), + 'assets_version_strategy' => 'assets.custom_version_strategy', + 'assets_base_urls' => 'http://cdn.example.com', + ), + )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml index 3e057d5b90254..e0896a5368727 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml @@ -21,6 +21,9 @@ https://bar3.example.com + + https://bar_version_strategy.example.com + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets_version_strategy_as_service.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets_version_strategy_as_service.xml new file mode 100644 index 0000000000000..353ac487e4044 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets_version_strategy_as_service.xml @@ -0,0 +1,14 @@ + + + + + + + http://cdn.example.com + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets.xml index 3b21e2cb4ed49..966f070d42273 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets.xml @@ -21,6 +21,9 @@ https://bar3.example.com + + https://bar_version_strategy.example.com + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets_version_strategy_as_service.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets_version_strategy_as_service.xml new file mode 100644 index 0000000000000..3b381120a728c --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/legacy_templating_assets_version_strategy_as_service.xml @@ -0,0 +1,15 @@ + + + + + + + php + http://cdn.example.com + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml index d3285ff58102c..c0e930deb9548 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml @@ -17,3 +17,6 @@ framework: bar_null_version: version: null base_urls: "https://bar3.example.com" + bar_version_strategy: + base_urls: ["https://bar_version_strategy.example.com"] + version_strategy: assets.custom_version_strategy diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets_version_strategy_as_service.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets_version_strategy_as_service.yml new file mode 100644 index 0000000000000..2528462f83cb5 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets_version_strategy_as_service.yml @@ -0,0 +1,4 @@ +framework: + assets: + version_strategy: assets.custom_version_strategy + base_urls: http://cdn.example.com diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets.yml index f2c8ecfec6701..33629a05dac27 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets.yml @@ -16,3 +16,6 @@ framework: bar_null_version: version: null base_urls: "https://bar3.example.com" + bar_version_strategy: + base_urls: ["https://bar_version_strategy.example.com"] + version_strategy: assets.custom_version_strategy diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets_version_strategy_as_service.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets_version_strategy_as_service.yml new file mode 100644 index 0000000000000..64b91ec0def92 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/legacy_templating_assets_version_strategy_as_service.yml @@ -0,0 +1,5 @@ +framework: + templating: + engines: [php] + assets_version_strategy: assets.custom_version_strategy + assets_base_urls: http://cdn.example.com diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index eb2e7cfea9e22..b3bdf0ecfdc12 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -215,11 +215,34 @@ public function testLegacyTemplatingAssets() $this->checkAssetsPackages($this->createContainerFromFile('legacy_templating_assets'), true); } + /** + * @group legacy + */ + public function testLegacyAssetsDefaultVersionStrategyAsService() + { + $container = $this->createContainerFromFile('legacy_templating_assets_version_strategy_as_service'); + $packages = $container->getDefinition('assets.packages'); + + // default package + $defaultPackage = $container->getDefinition($packages->getArgument(0)); + $this->assertEquals('assets.custom_version_strategy', (string) $defaultPackage->getArgument(1)); + } + public function testAssets() { $this->checkAssetsPackages($this->createContainerFromFile('assets')); } + public function testAssetsDefaultVersionStrategyAsService() + { + $container = $this->createContainerFromFile('assets_version_strategy_as_service'); + $packages = $container->getDefinition('assets.packages'); + + // default package + $defaultPackage = $container->getDefinition($packages->getArgument(0)); + $this->assertEquals('assets.custom_version_strategy', (string) $defaultPackage->getArgument(1)); + } + public function testTranslator() { $container = $this->createContainerFromFile('full'); @@ -552,7 +575,7 @@ private function checkAssetsPackages(ContainerBuilder $container, $legacy = fals // packages $packages = $packages->getArgument(1); - $this->assertCount($legacy ? 4 : 5, $packages); + $this->assertCount($legacy ? 5 : 6, $packages); if (!$legacy) { $package = $container->getDefinition($packages['images_path']); @@ -570,6 +593,8 @@ private function checkAssetsPackages(ContainerBuilder $container, $legacy = fals $this->assertEquals($legacy ? 'assets.empty_version_strategy' : 'assets._version__default', (string) $container->getDefinition('assets._package_bar')->getArgument(1)); $this->assertEquals('assets.empty_version_strategy', (string) $container->getDefinition('assets._package_bar_null_version')->getArgument(1)); + + $this->assertEquals('assets.custom_version_strategy', (string) $container->getDefinition('assets._package_bar_version_strategy')->getArgument(1)); } private function assertPathPackage(ContainerBuilder $container, DefinitionDecorator $package, $basePath, $version, $format)