From 726a94c5bfe417925fa194db396ad68c55b5c945 Mon Sep 17 00:00:00 2001 From: Evgeniy Sokolov Date: Mon, 25 Jan 2016 22:22:29 +0100 Subject: [PATCH 1/3] assets version as service --- .../DependencyInjection/Configuration.php | 14 ++++++ .../FrameworkExtension.php | 12 ++++- .../Resources/config/schema/symfony-1.0.xsd | 2 + .../DependencyInjection/ConfigurationTest.php | 45 +++++++++++++++++++ .../Fixtures/php/assets.php | 4 ++ .../assets_version_strategy_as_service.php | 8 ++++ .../Fixtures/xml/assets.xml | 3 ++ .../assets_version_strategy_as_service.xml | 14 ++++++ .../Fixtures/yml/assets.yml | 3 ++ .../assets_version_strategy_as_service.yml | 4 ++ .../FrameworkExtensionTest.php | 15 ++++++- 11 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets_version_strategy_as_service.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets_version_strategy_as_service.xml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets_version_strategy_as_service.yml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index a638daeb8a731..dfed633fda640 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -364,6 +364,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() @@ -376,6 +377,12 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->prototype('scalar')->end() ->end() ->end() + ->validate() + ->ifTrue(function ($v) { + return (null !== $v['version_strategy'] && null !== $v['version']); + }) + ->thenInvalid('You cannot use version_strategy and version settings in assets configuration.') + ->end() ->fixXmlConfig('package') ->children() ->arrayNode('packages') @@ -383,6 +390,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->prototype('array') ->fixXmlConfig('base_url') ->children() + ->scalarNode('version_strategy')->defaultNull()->end() ->scalarNode('version')->defaultNull()->end() ->scalarNode('version_format')->defaultNull()->end() ->scalarNode('base_path')->defaultValue('')->end() @@ -395,6 +403,12 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->prototype('scalar')->end() ->end() ->end() + ->validate() + ->ifTrue(function ($v) { + return (null !== $v['version_strategy'] && null !== $v['version']); + }) + ->thenInvalid('You cannot use version_strategy and version settings in same package.') + ->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 7d8afee62b043..806be8e8ebf90 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -561,14 +561,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 (null === $package['version']) { + if (null !== $package['version_strategy']) { + $version = new Reference($package['version_strategy']); + } else if (null === $package['version']) { $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 5e26bd11d3520..cead2295ed1ac 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 @@ -126,6 +126,7 @@ + @@ -137,6 +138,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index d0dba2badf4f7..12f4a664dba97 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -106,6 +106,7 @@ public function testAssetsCanBeEnabled() $config = $processor->processConfiguration($configuration, array(array('assets' => null))); $defaultConfig = array( + 'version_strategy' => null, 'version' => null, 'version_format' => '%%s?%%s', 'base_path' => '', @@ -116,6 +117,50 @@ public function testAssetsCanBeEnabled() $this->assertEquals($defaultConfig, $config['assets']); } + /** + * @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( 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 e3532a7747cc6..3cfc5c1bc0cc4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php @@ -20,6 +20,10 @@ 'bar' => array( 'base_urls' => array('https://bar2.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/xml/assets.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml index e39e15f900528..b99952b2923d3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml @@ -18,6 +18,9 @@ https://bar2.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/yml/assets.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml index 193dc59dfba3a..ec74f1beb8015 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml @@ -14,3 +14,6 @@ framework: version_format: %%s-%%s bar: base_urls: ["https://bar2.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/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index f6be36dae8e50..34889a13a17ce 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -209,7 +209,7 @@ public function testAssets() // packages $packages = $packages->getArgument(1); - $this->assertCount(4, $packages); + $this->assertCount(5, $packages); $package = $container->getDefinition($packages['images_path']); $this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s'); @@ -222,6 +222,19 @@ public function testAssets() $package = $container->getDefinition($packages['bar']); $this->assertUrlPackage($container, $package, array('https://bar2.example.com'), 'SomeVersionScheme', '%%s?version=%%s'); + + $package = $container->getDefinition($packages['bar_version_strategy']); + $this->assertEquals('assets.custom_version_strategy', (string) $package->getArgument(1)); + } + + 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() From b270a798ddc868e7515adf4e3eaff39c945b1ae4 Mon Sep 17 00:00:00 2001 From: Evgeniy Sokolov Date: Tue, 26 Jan 2016 08:45:15 +0100 Subject: [PATCH 2/3] cs and texts --- .../DependencyInjection/Configuration.php | 4 +- .../FrameworkExtension.php | 2 +- .../DependencyInjection/ConfigurationTest.php | 42 ++++++++++--------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index dfed633fda640..a1af4ef70f4fd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -381,7 +381,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->ifTrue(function ($v) { return (null !== $v['version_strategy'] && null !== $v['version']); }) - ->thenInvalid('You cannot use version_strategy and version settings in assets configuration.') + ->thenInvalid('You cannot use both "version_strategy" and "version" at the same time under "assets".') ->end() ->fixXmlConfig('package') ->children() @@ -407,7 +407,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->ifTrue(function ($v) { return (null !== $v['version_strategy'] && null !== $v['version']); }) - ->thenInvalid('You cannot use version_strategy and version settings in same package.') + ->thenInvalid('You cannot use both "version_strategy" and "version" at the same time under "assets" for the "%s" package.') ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 806be8e8ebf90..326c0083a498d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -576,7 +576,7 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co foreach ($config['packages'] as $name => $package) { if (null !== $package['version_strategy']) { $version = new Reference($package['version_strategy']); - } else if (null === $package['version']) { + } elseif (null === $package['version']) { $version = $defaultVersion; } else { $format = $package['version_format'] ?: $config['version_format']; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 12f4a664dba97..5bd2e114a1f1b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -91,6 +91,7 @@ public function testInvalidValueTrustedProxies() { $processor = new Processor(); $configuration = new Configuration(true); + $processor->processConfiguration($configuration, array( array( 'secret' => 's3cr3t', @@ -119,46 +120,47 @@ public function testAssetsCanBeEnabled() /** * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException - * @expectedExceptionMessage You cannot use version_strategy and version settings in assets configuration. + * @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets". */ 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', - ), + 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. + * @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets" for the "{"base_urls":["\/\/example.com"],"version":1,"version_strategy":"foo","version_format":null,"base_path":""}" 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', - ), + 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() From 214de9b5b66cc5130332b63bc22f0ede897658ec Mon Sep 17 00:00:00 2001 From: Evgeniy Sokolov Date: Tue, 26 Jan 2016 09:30:27 +0100 Subject: [PATCH 3/3] text cosmetics --- .../FrameworkBundle/DependencyInjection/Configuration.php | 2 +- .../Tests/DependencyInjection/ConfigurationTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index a1af4ef70f4fd..2276b26db58c0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -407,7 +407,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode) ->ifTrue(function ($v) { return (null !== $v['version_strategy'] && null !== $v['version']); }) - ->thenInvalid('You cannot use both "version_strategy" and "version" at the same time under "assets" for the "%s" package.') + ->thenInvalid('You cannot use both "version_strategy" and "version" at the same time under "assets" packages.') ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 5bd2e114a1f1b..a4d086117567d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -139,7 +139,7 @@ public function testInvalidVersionStrategy() /** * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException - * @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets" for the "{"base_urls":["\/\/example.com"],"version":1,"version_strategy":"foo","version_format":null,"base_path":""}" package. + * @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets" packages. */ public function testInvalidPackageVersionStrategy() { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy