Skip to content

Commit 525b462

Browse files
ewgRafabpot
authored andcommitted
[Asset] Version as service
1 parent 6a65181 commit 525b462

File tree

11 files changed

+123
-3
lines changed

11 files changed

+123
-3
lines changed

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()

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'];

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>

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(

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
));
Lines changed: 8 additions & 0 deletions
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+
));

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>
Lines changed: 14 additions & 0 deletions
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>

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
Lines changed: 4 additions & 0 deletions
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

0 commit comments

Comments
 (0)