8000 [Asset] ability to set assets_version_strategy as service by ewgRa · Pull Request #16522 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Asset] ability to set assets_version_strategy as service #16522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand All @@ -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'])
Expand All @@ -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' => '',
Expand All @@ -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' => '',
Expand All @@ -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;
})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -530,6 +562,7 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
->prototype('array')
->fixXmlConfig('base_url')
->children()
->scalarNode('version_strategy')->defaultNull()->end()
->scalarNode('version')
->defaultNull()
->beforeNormalization()
Expand Down Expand Up @@ -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()
Expand All @@ -595,6 +629,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
->prototype('array')
->fixXmlConfig('base_url')
->children()
->scalarNode('version_strategy')->defaultNull()->end()
->scalarNode('version')
->defaultNull()
->beforeNormalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,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 @@ -147,6 +148,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 All @@ -160,6 +162,7 @@
<xsd:element name="form" type="form-resources" minOccurs="0" maxOccurs="1" />
</xsd:sequence>

<xsd:attribute name="assets-version-strategy" type="xsd:string" />
<xsd:attribute name="assets-version" type="xsd:string" />
<xsd:attribute name="assets-version-format" type="xsd:string" />
<xsd:attribute name="cache" type="xsd:string" />
Expand All @@ -178,6 +181,7 @@
</xsd:sequence>

<xsd:attribute name="name" type="xsd:string" use="required" />
<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
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -183,6 +275,7 @@ protected static function getBundleDefaultConfig()
'enabled' => false,
),
'assets' => array(
'version_strategy' => null,
'version' => null,
'version_format' => '%%s?%%s',
'base_path' => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', array(
'assets' => array(
'version_strategy' => 'assets.custom_version_strategy',
'base_urls' => 'http://cdn.example.com',
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
));
), );
F987
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$container->loadFromExtension('framework', array(
'templating' => array(
'engines' => array('php'),
'assets_version_strategy' => 'assets.custom_version_strategy',
'assets_base_urls' => 'http://cdn.example.com',
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<framework:package name="bar_null_version" version="">
<framework:base-url>https://bar3.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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<framework:package name="bar_null_version" version="">
<framework:base-url>https://bar3.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:templating>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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:templating assets-version-strategy="assets.custom_version_strategy">
<framework:engine>php</framework:engine>
<framework:assets-base-url>http://cdn.example.com</framework:assets-base-url>
</framework:templating>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework:
assets:
version_strategy: assets.custom_version_strategy
base_urls: http://cdn.example.com
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
framework:
templating:
engines: [php]
assets_version_strategy: assets.custom_version_strategy
assets_base_urls: http://cdn.example.com
Loading
0