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 ed509e0

Browse filesBrowse files
committed
Add CachedVersionStrategy to decorate any Asset version strategy
framework: assets: json_manifest_path: 'https://cdn.example.com/manifest.json' cache_version: true
1 parent 6ae2fc3 commit ed509e0
Copy full SHA for ed509e0

File tree

5 files changed

+90
-0
lines changed
Filter options

5 files changed

+90
-0
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
@@ -585,6 +585,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
585585
->scalarNode('version')->defaultNull()->end()
586586
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
587587
->scalarNode('json_manifest_path')->defaultNull()->end()
588+
->booleanNode('cache_version')->defaultFalse()->end()
588589
->scalarNode('base_path')->defaultValue('')->end()
589590
->arrayNode('base_urls')
590591
->requiresAtLeastOneElement()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,10 @@ 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_');
1001+
}
1002+
9991003
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
10001004
$container->setDefinition('assets._default_package', $defaultPackage);
10011005

@@ -1011,6 +1015,9 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
10111015
$format = $package['version_format'] ?: $config['version_format'];
10121016
$version = isset($package['version']) ? $package['version'] : null;
10131017
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name);
1018+
if ($package['cache_version']) {
1019+
$version = $this->cacheVersion($container, $version, $name.'_');
1020+
}
10141021
}
10151022

10161023
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
@@ -1073,6 +1080,17 @@ private function createVersion(ContainerBuilder $container, ?string $version, ?s
10731080
return new Reference('assets.empty_version_strategy');
10741081
}
10751082

1083+
private function cacheVersion(ContainerBuilder $container, Reference $reference, string $prefix): Reference
1084+
{
1085+
$cachedReference = new Reference($reference.'.cached');
1086+
$cached = new ChildDefinition('assets.cached_version_strategy');
1087+
$cached->replaceArgument(0, $reference);
1088+
$cached->replaceArgument(2, ['prefix' => $prefix]);
1089+
$container->setDefinition($cachedReference, $cached);
1090+
1091+
return $cachedReference;
1092+
}
1093+
10761094
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container, LoaderInterface $loader, string $defaultLocale)
10771095
{
10781096
if (!$this->isConfigEnabled($container, $config)) {

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@
5555
<argument /> <!-- manifest url -->
5656
<argument type="service" id="http_client" />
5757
</service>
58+
59+
<service id="assets.cached_version_strategy" class="Symfony\Component\Asset\VersionStrategy\CachedVersionStrategy" abstract="true">
60+
<argument /> <!-- decorated version strategy -->
61+
<argument type="service" id="cache.assets" />
62+
<argument /> <!-- options -->
63+
</service>
5864
</services>
5965
</container>

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<argument type="service" id="cache.app" />
1616
</service>
1717

18+
<service id="cache.assets" parent="cache.app" public="false">
19+
<tag name="cache.pool" />
20+
</service>
21+
1822
<service id="cache.system" parent="cache.adapter.system" public="true">
1923
<tag name="cache.pool" />
2024
</service>
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Asset\VersionStrategy;
13+
14+
use Symfony\Contracts\Cache\CacheInterface;
15+
16+
/**
17+
* Cache generated versions of any version strategy.
18+
*/
19+
class CachedVersionStrategy implements VersionStrategyInterface
20+
{
21+
private $strategy;
22+
23+
private $cache;
24+
25+
private $options;
26+
27+
public function __construct(VersionStrategyInterface $strategy, CacheInterface $cache, array $options = [])
28+
{
29+
$this->strategy = $strategy;
30+
$this->cache = $cache;
31+
$this->options = array_replace([
32+
'prefix' => 'asset_',
33+
'beta' => null,
34+
], $options);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function getVersion(string $path)
41+
{
42+
return $this->cache->get($this->getCacheKey('version_'.$path), function () use ($path) {
43+
return $this->strategy->getVersion($path);
44+
}, $this->options['beta']);
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function applyVersion(string $path)
51+
{
52+
return $this->cache->get($this->getCacheKey('path_'.$path), function () use ($path) {
53+
return $this->strategy->applyVersion($path);
54+
}, $this->options['beta']);
55+
}
56+
57+
private function getCacheKey(string $path): string
58+
{
59+
return $this->options['prefix'].urlencode($path);
60+
}
61+
}

0 commit comments

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