Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae46adb

Browse filesBrowse files
committed
Makes asset version strategy cachable on config
1 parent ed509e0 commit ae46adb
Copy full SHA for ae46adb

File tree

9 files changed

+53
-10
lines changed
Filter options

9 files changed

+53
-10
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
628628
->end()
629629
->scalarNode('version_format')->defaultNull()->end()
630630
->scalarNode('json_manifest_path')->defaultNull()->end()
631+
->scalarNode('cache_version')->defaultFalse()->end()
631632
->scalarNode('base_path')->defaultValue('')->end()
632633
->arrayNode('base_urls')
633634
->requiresAtLeastOneElement()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+12-6Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,11 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
996996
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], $config['json_manifest_path'], '_default');
997997
}
998998

999-
if ($config['cache_version']) {
1000-
$defaultVersion = $this->cacheVersion($container, $defaultVersion, 'default_');
999+
if (false !== $config['cache_version']) {
1000+
$defaultVersion = $this->cacheVersion($container, $defaultVersion, [
1001+
'prefix' => 'default_',
1002+
'expiresAfter' => $config['cache_version'] === true ? null : $config['cache_version'],
1003+
]);
10011004
}
10021005

10031006
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
@@ -1015,8 +1018,11 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
10151018
$format = $package['version_format'] ?: $config['version_format'];
10161019
$version = isset($package['version']) ? $package['version'] : null;
10171020
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name);
1018-
if ($package['cache_version']) {
1019-
$version = $this->cacheVersion($container, $version, $name.'_');
1021+
if (false !== $package['cache_version']) {
1022+
$version = $this->cacheVersion($container, $version, [
1023+
'prefix' => $name.'_',
1024+
'expiresAfter' => $package['cache_version'] === true ? null : $package['cache_version'],
1025+
]);
10201026
}
10211027
}
10221028

@@ -1080,12 +1086,12 @@ private function createVersion(ContainerBuilder $container, ?string $version, ?s
10801086
return new Reference('assets.empty_version_strategy');
10811087
}
10821088

1083-
private function cacheVersion(ContainerBuilder $container, Reference $reference, string $prefix): Reference
1089+
private function cacheVersion(ContainerBuilder $container, Reference $reference, array $options): Reference
10841090
{
10851091
$cachedReference = new Reference($reference.'.cached');
10861092
$cached = new ChildDefinition('assets.cached_version_strategy');
10871093
$cached->replaceArgument(0, $reference);
1088-
$cached->replaceArgument(2, ['prefix' => $prefix]);
1094+
$cached->replaceArgument(2, $options);
10891095
$container->setDefinition($cachedReference, $cached);
10901096

10911097
return $cachedReference;

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
<xsd:attribute name="version" type="xsd:string" />
145145
<xsd:attribute name="version-format" type="xsd:string" />
146146
<xsd:attribute name="json-manifest-path" type="xsd:string" />
147+
<xsd:attribute name="cache-version" type="xsd:string" />
147148
</xsd:complexType>
148149

149150
<xsd:complexType name="package">
@@ -157,6 +158,7 @@
157158
<xsd:attribute name="version" type="xsd:string" />
158159
<xsd:attribute name="version-format" type="xsd:string" />
159160
<xsd:attribute name="json-manifest-path" type="xsd:string" />
161+
<xsd:attribute name="cache-version" type="xsd:string" />
160162
</xsd:complexType>
161163

162164
<xsd:complexType name="form-resources">

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function testAssetsCanBeEnabled()
8686
'base_urls' => [],
8787
'packages' => [],
8888
'json_manifest_path' => null,
89+
'cache_version' => false,
8990
];
9091

9192
$this->assertEquals($defaultConfig, $config['assets']);
@@ -443,6 +444,7 @@ protected static function getBundleDefaultConfig()
443444
'base_urls' => [],
444445
'packages' => [],
445446
'json_manifest_path' => null,
447+
'cache_version' => false,
446448
],
447449
'cache' => [
448450
'pools' => [],

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
'remote_manifest' => [
3131
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
3232
],
33+
'cached' => [
34+
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
35+
'cache_version' => true,
36+
],
3337
],
3438
],
3539
]);

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</framework:package>
2424
<framework:package name="json_manifest_strategy" json-manifest-path="/path/to/manifest.json" />
2525
<framework:package name="remote_manifest" json-manifest-path="https://cdn.example.com/manifest.json" />
26+
<framework:package name="cached" json-manifest-path="https://cdn.example.com/manifest.json" cache-version="60" />
2627
</framework:assets>
2728
</framework:config>
2829
</container>

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ framework:
2121
json_manifest_path: '/path/to/manifest.json'
2222
remote_manifest:
2323
json_manifest_path: 'https://cdn.example.com/manifest.json'
24+
cached:
25+
json_manifest_path: 'https://cdn.example.com/manifest.json'
26+
cache_version: 60

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public function testAssets()
535535

536536
// packages
537537
$packages = $packages->getArgument(1);
538-
$this->assertCount(7, $packages);
538+
$this->assertCount(8, $packages);
539539

540540
$package = $container->getDefinition((string) $packages['images_path']);
541541
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
@@ -561,6 +561,10 @@ public function testAssets()
561561
$versionStrategy = $container->getDefinition($package->getArgument(1));
562562
$this->assertSame('assets.remote_json_manifest_version_strategy', $versionStrategy->getParent());
563563
$this->assertSame('https://cdn.example.com/manifest.json', $versionStrategy->getArgument(0));
564+
565+
$package = $container->getDefinition($packages['cached']);
566+
$versionStrategy = $container->getDefinition($package->getArgument(1));
567+
$this->assertSame('assets.cached_version_strategy', $versionStrategy->getParent());
564568
}
565569

566570
public function testAssetsDefaultVersionStrategyAsService()

‎src/Symfony/Component/Asset/VersionStrategy/CachedVersionStrategy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/VersionStrategy/CachedVersionStrategy.php
+23-3Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Component\Asset\VersionStrategy;
1313

1414
use Symfony\Contracts\Cache\CacheInterface;
15+
use Symfony\Contracts\Cache\ItemInterface;
1516

1617
/**
17-
* Cache generated versions of any version strategy.
18+
* Cache generated versions and path of any version strategy.
1819
*/
1920
class CachedVersionStrategy implements VersionStrategyInterface
2021
{
@@ -24,13 +25,28 @@ class CachedVersionStrategy implements VersionStrategyInterface
2425

2526
private $options;
2627

28+
/**
29+
* @param VersionStrategyInterface $strategy
30+
* @param CacheInterface $cache
31+
* @param array $options
32+
*
33+
* List of available options:
34+
* * prefix: Begin the cache key
35+
* * beta: A float that, as it grows, controls the likeliness of triggering
36+
* early expiration. 0 disables it, INF forces immediate expiration.
37+
* @see CacheInterface::get()
38+
* * expiresAfter: The period of time from the present after which the item MUST
39+
* be considered expired. An integer parameter is understood to be the time in
40+
* seconds until expiration. @see ItemInterface::expiresAfter()
41+
*/
2742
public function __construct(VersionStrategyInterface $strategy, CacheInterface $cache, array $options = [])
2843
{
2944
$this->strategy = $strategy;
3045
$this->cache = $cache;
3146
$this->options = array_replace([
3247
'prefix' => 'asset_',
3348
'beta' => null,
49+
'expiresAfter' => null,
3450
], $options);
3551
}
3652

@@ -39,7 +55,9 @@ public function __construct(VersionStrategyInterface $strategy, CacheInterface $
3955
*/
4056
public function getVersion(string $path)
4157
{
42-
return $this->cache->get($this->getCacheKey('version_'.$path), function () use ($path) {
58+
return $this->cache->get($this->getCacheKey('version_'.$path), function (ItemInterface $item) use ($path) {
59+
$item->expiresAfter($this->options['expiresAfter']);
60+
4361
return $this->strategy->getVersion($path);
4462
}, $this->options['beta']);
4563
}
@@ -49,7 +67,9 @@ public function getVersion(string $path)
4967
*/
5068
public function applyVersion(string $path)
5169
{
52-
return $this->cache->get($this->getCacheKey('path_'.$path), function () use ($path) {
70+
return $this->cache->get($this->getCacheKey('path_'.$path),function (ItemInterface $item) use ($path) {
71+
$item->expiresAfter($this->options['expiresAfter']);
72+
5373
return $this->strategy->applyVersion($path);
5474
}, $this->options['beta']);
5575
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.