Skip to content

Commit

Permalink
feature #17532 [Asset] Version as service (ewgRa)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jan 26, 2016
2 parents 37f5264 + 52d116b commit f1cdc6f
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 3 deletions.
Expand Up @@ -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()
Expand All @@ -376,13 +377,20 @@ 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 both "version_strategy" and "version" at the same time under "assets".')
->end()
->fixXmlConfig('package')
->children()
->arrayNode('packages')
->useAttributeAsKey('name')
->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()
Expand All @@ -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 both "version_strategy" and "version" at the same time under "assets" packages.')
->end()
->end()
->end()
->end()
Expand Down
Expand Up @@ -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']);
} elseif (null === $package['version']) {
$version = $defaultVersion;
} else {
$format = $package['version_format'] ?: $config['version_format'];
Expand Down
Expand Up @@ -126,6 +126,7 @@
</xsd:sequence>

<xsd:attribute name="base-path" type="xsd:string" />
<xsd:attribute name="version-strategy" type="xsd:string" />
<xsd:attribute name="version" type="xsd:string" />
<xsd:attribute name="version-format" type="xsd:string" />
</xsd:complexType>
Expand All @@ -137,6 +138,7 @@

<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="base-path" type="xsd:string" />
<xsd:attribute name="version-strategy" type="xsd:string" />
<xsd:attribute name="version" type="xsd:string" />
<xsd:attribute name="version-format" type="xsd:string" />
</xsd:complexType>
Expand Down
Expand Up @@ -91,6 +91,7 @@ public function testInvalidValueTrustedProxies()
{
$processor = new Processor();
$configuration = new Configuration(true);

$processor->processConfiguration($configuration, array(
array(
'secret' => 's3cr3t',
Expand All @@ -106,6 +107,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' => '',
Expand All @@ -116,6 +118,51 @@ public function testAssetsCanBeEnabled()
$this->assertEquals($defaultConfig, $config['assets']);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @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',
),
),
));
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage You cannot use both "version_strategy" and "version" at the same time under "assets" packages.
*/
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(
Expand Down
Expand Up @@ -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',
),
),
),
));
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', array(
'assets' => array(
'version_strategy' => 'assets.custom_version_strategy',
'base_urls' => 'http://cdn.example.com',
),
));
Expand Up @@ -18,6 +18,9 @@
<framework:package name="bar">
<framework:base-url>https://bar2.example.com</framework:base-url>
</framework:package>
<framework:package name="bar_version_strategy" version-strategy="assets.custom_version_strategy">
<framework:base-url>https://bar_version_strategy.example.com</framework:base-url>
</framework:package>
</framework:assets>
</framework:config>
</container>
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:assets version-strategy="assets.custom_version_strategy">
<framework:base-url>http://cdn.example.com</framework:base-url>
</framework:assets>
</framework:config>
</container>
Expand Up @@ -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
@@ -0,0 +1,4 @@
framework:
assets:
version_strategy: assets.custom_version_strategy
base_urls: http://cdn.example.com
Expand Up @@ -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');
Expand All @@ -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()
Expand Down

0 comments on commit f1cdc6f

Please sign in to comment.