Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f1cdc6f

Browse files
committed
feature #17532 [Asset] Version as service (ewgRa)
This PR was squashed before being merged into the 3.1-dev branch (closes #17532). Discussion ---------- [Asset] Version as service | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | While I working on #14832 I realize that all this problems and hidden magic can be avoided, if we will have ability to set asset version strategy as service. This PR implementation of this idea. Now it is possible to do something like this: ```yaml framework: assets: version_strategy: assets.custom_version_strategy base_urls: http://cdn.example.com packages: foo: base_urls: ["https://example.com"] version_strategy: assets.custom_version_strategy ``` There is can be some conflicts with #16511 when it will be in master Commits ------- 52d116b [Asset] Version as service
2 parents 37f5264 + 52d116b commit f1cdc6f

File tree

11 files changed

+123
-3
lines changed

11 files changed

+123
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
364364
->canBeUnset()
365365
->fixXmlConfig('base_url')
366366
->children()
367+
->scalarNode('version_strategy')->defaultNull()->end()
367368
->scalarNode('version')->defaultNull()->end()
368369
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
369370
->scalarNode('base_path')->defaultValue('')->end()
@@ -376,13 +377,20 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
376377
->prototype('scalar')->end()
377378
->end()
378379
->end()
380+
->validate()
381+
->ifTrue(function ($v) {
382+
return (null !== $v['version_strategy'] && null !== $v['version']);
383+
})
384+
->thenInvalid('You cannot use both "version_strategy" and "version" at the same time under "assets".')
385+
->end()
379386
->fixXmlConfig('package')
380387
->children()
381388
->arrayNode('packages')
382389
->useAttributeAsKey('name')
383390
->prototype('array')
384391
->fixXmlConfig('base_url')
385392
->children()
393+
->scalarNode('version_strategy')->defaultNull()->end()
386394
->scalarNode('version')->defaultNull()->end()
387395
->scalarNode('version_format')->defaultNull()->end()
388396
->scalarNode('base_path')->defaultValue('')->end()
@@ -395,6 +403,12 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
395403
->prototype('scalar')->end()
396404
->end()
397405
->end()
406+
->validate()
407+
->ifTrue(function ($v) {
408+
return (null !== $v['version_strategy'] && null !== $v['version']);
409+
})
410+
->thenInvalid('You cannot use both "version_strategy" and "version" at the same time under "assets" packages.')
411+
->end()
398412
->end()
399413
->end()
400414
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,22 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
561561
{
562562
$loader->load('assets.xml');
563563

564-
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], '_default');
564+
$defaultVersion = null;
565+
566+
if ($config['version_strategy']) {
567+
$defaultVersion = new Reference($config['version_strategy']);
568+
} else {
569+
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], '_default');
570+
}
565571

566572
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
567573
$container->setDefinition('assets._default_package', $defaultPackage);
568574

569575
$namedPackages = array();
570576
foreach ($config['packages'] as $name => $package) {
571-
if (null === $package['version']) {
577+
if (null !== $package['version_strategy']) {
578+
$version = new Reference($package['version_strategy']);
579+
} elseif (null === $package['version']) {
572580
$version = $defaultVersion;
573581
} else {
574582
$format = $package['version_format'] ?: $config['version_format'];

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
</xsd:sequence>
127127

128128
<xsd:attribute name="base-path" type="xsd:string" />
129+
<xsd:attribute name="version-strategy" type="xsd:string" />
129130
<xsd:attribute name="version" type="xsd:string" />
130131
<xsd:attribute name="version-format" type="xsd:string" />
131132
</xsd:complexType>
@@ -137,6 +138,7 @@
137138

138139
<xsd:attribute name="name" type="xsd:string" use="required" />
139140
<xsd:attribute name="base-path" type="xsd:string" />
141+
<xsd:attribute name="version-strategy" type="xsd:string" />
140142
<xsd:attribute name="version" type="xsd:string" />
141143
<xsd:attribute name="version-format" type="xsd:string" />
142144
</xsd:complexType>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function testInvalidValueTrustedProxies()
9191
{
9292
$processor = new Processor();
9393
$configuration = new Configuration(true);
94+
9495
$processor->processConfiguration($configuration, array(
9596
array(
9697
'secret' => 's3cr3t',
@@ -106,6 +107,7 @@ public function testAssetsCanBeEnabled()
106107
$config = $processor->processConfiguration($configuration, array(array('assets' => null)));
107108

108109
$defaultConfig = array(
110+
'version_strategy' => null,
109111
'version' => null,
110112
'version_format' => '%%s?%%s',
111113
'base_path' => '',
@@ -116,6 +118,51 @@ public function testAssetsCanBeEnabled()
116118
$this->assertEquals($defaultConfig, $config['assets']);
117119
}
118120

121+
/**
122+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
123+
* @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets".
124+
*/
125+
public function testInvalidVersionStrategy()
126+
{
127+
$processor = new Processor();
128+
$configuration = new Configuration(true);
129+
$processor->processConfiguration($configuration, array(
130+
array(
131+
'assets' => array(
132+
'base_urls' => '//example.com',
133+
'version' => 1,
134+
'version_strategy' => 'foo',
135+
),
136+
),
137+
));
138+
}
139+
140+
/**
141+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
142+
* @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets" packages.
143+
*/
144+
public function testInvalidPackageVersionStrategy()
145+
{
146+
$processor = new Processor();
147+
$configuration = new Configuration(true);
148+
149+
$processor->processConfiguration($configuration, array(
150+
array(
151+
'assets' => array(
152+
'base_urls' => '//example.com',
153+
'version' => 1,
154+
'packages' => array(
155+
'foo' => array(
156+
'base_urls' => '//example.com',
157+
'version' => 1,
158+
'version_strategy' => 'foo',
159+
),
160+
),
161+
),
162+
),
163+
));
164+
}
165+
119166
protected static function getBundleDefaultConfig()
120167
{
121168
return array(

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
'bar' => array(
2121
'base_urls' => array('https://bar2.example.com'),
2222
),
23+
'bar_version_strategy' => array(
24+
'base_urls' => array('https://bar2.example.com'),
25+
'version_strategy' => 'assets.custom_version_strategy',
26+
),
2327
),
2428
),
2529
));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'assets' => array(
5+
'version_strategy' => 'assets.custom_version_strategy',
6+
'base_urls' => 'http://cdn.example.com',
7+
),
8+
));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<framework:package name="bar">
1919
<framework:base-url>https://bar2.example.com</framework:base-url>
2020
</framework:package>
21+
<framework:package name="bar_version_strategy" version-strategy="assets.custom_version_strategy">
22+
<framework:base-url>https://bar_version_strategy.example.com</framework:base-url>
23+
</framework:package>
2124
</framework:assets>
2225
</framework:config>
2326
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:assets version-strategy="assets.custom_version_strategy">
11+
<framework:base-url>http://cdn.example.com</framework:base-url>
12+
</framework:assets>
13+
</framework:config>
14+
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ framework:
1414
version_format: %%s-%%s
1515
bar:
1616
base_urls: ["https://bar2.example.com"]
17+
bar_version_strategy:
18+
base_urls: ["https://bar_version_strategy.example.com"]
19+
version_strategy: assets.custom_version_strategy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
assets:
3+
version_strategy: assets.custom_version_strategy
4+
base_urls: http://cdn.example.com

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function testAssets()
209209

210210
// packages
211211
$packages = $packages->getArgument(1);
212-
$this->assertCount(4, $packages);
212+
$this->assertCount(5, $packages);
213213

214214
$package = $container->getDefinition($packages['images_path']);
215215
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
@@ -222,6 +222,19 @@ public function testAssets()
222222

223223
$package = $container->getDefinition($packages['bar']);
224224
$this->assertUrlPackage($container, $package, array('https://bar2.example.com'), 'SomeVersionScheme', '%%s?version=%%s');
225+
226+
$package = $container->getDefinition($packages['bar_version_strategy']);
227+
$this->assertEquals('assets.custom_version_strategy', (string) $package->getArgument(1));
228+
}
229+
230+
public function testAssetsDefaultVersionStrategyAsService()
231+
{
232+
$container = $this->createContainerFromFile('assets_version_strategy_as_service');
233+
$packages = $container->getDefinition('assets.packages');
234+
235+
// default package
236+
$defaultPackage = $container->getDefinition($packages->getArgument(0));
237+
$this->assertEquals('assets.custom_version_strategy', (string) $defaultPackage->getArgument(1));
225238
}
226239

227240
public function testTranslator()

0 commit comments

Comments
 (0)