Skip to content

[Asset] Add CachedVersionStrategy to decorate any Asset version strategy #36371

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 2 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 @@ -580,6 +580,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
->scalarNode('version')->defaultNull()->end()
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
->scalarNode('json_manifest_path')->defaultNull()->end()
->booleanNode('cache')->defaultFalse()->end()
->scalarNode('base_path')->defaultValue('')->end()
->arrayNode('base_urls')
->requiresAtLeastOneElement()
Expand Down Expand Up @@ -622,6 +623,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
->end()
->scalarNode('version_format')->defaultNull()->end()
->scalarNode('json_manifest_path')->defaultNull()->end()
->scalarNode('cache')->defaultFalse()->end()
->scalarNode('base_path')->defaultValue('')->end()
->arrayNode('base_urls')
->requiresAtLeastOneElement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,10 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], $config['json_manifest_path'], '_default');
}

if ($config['cache']) {
$defaultVersion = $this->decorateVersionStrategyWithCache($container, $defaultVersion);
}

$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
$container->setDefinition('assets._default_package', $defaultPackage);

Expand All @@ -1011,6 +1015,9 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
$format = $package['version_format'] ?: $config['version_format'];
$version = isset($package['version']) ? $package['version'] : null;
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name);
if ($package['cache']) {
$version = $this->decorateVersionStrategyWithCache($container, $version);
}
}

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

private function decorateVersionStrategyWithCache(ContainerBuilder $container, Reference $reference): Reference
{
$cachedReference = new Reference($reference.'.cached');
$cached = new ChildDefinition('assets.cached_version_strategy');
$cached->replaceArgument(0, $reference);
$container->setDefinition($cachedReference, $cached);

return $cachedReference;
}

private function registerTranslatorConfiguration(array $config, ContainerBuilder $container, LoaderInterface $loader, string $defaultLocale)
{
if (!$this->isConfigEnabled($container, $config)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@
<argument /> <!-- manifest url -->
<argument type="service" id="http_client" />
</service>

<service id="assets.cached_version_strategy" class="Symfony\Component\Asset\VersionStrategy\CachedVersionStrategy" abstract="true">
<argument /> <!-- decorated version strategy -->
<argument type="service" id="cache.assets" />
</service>
</services>
</container>
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<argument type="service" id="cache.app" />
</service>

<service id="cache.assets" parent="cache.system" public="false">
<tag name="cache.pool" />
</service>

<service id="cache.system" parent="cache.adapter.system" public="true">
<tag name="cache.pool" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<xsd:attribute name="version" type="xsd:string" />
<xsd:attribute name="version-format" type="xsd:string" />
<xsd:attribute name="json-manifest-path" type="xsd:string" />
<xsd:attribute name="cache" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="package">
Expand All @@ -157,6 +158,7 @@
<xsd:attribute name="version" type="xsd:string" />
<xsd:attribute name="version-format" type="xsd:string" />
<xsd:attribute name="json-manifest-path" type="xsd:string" />
<xsd:attribute name="cache" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="form-resources">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function testAssetsCanBeEnabled()
'base_urls' => [],
'packages' => [],
'json_manifest_path' => null,
'cache' => false,
];

$this->assertEquals($defaultConfig, $config['assets']);
Expand Down Expand Up @@ -439,6 +440,7 @@ protected static function getBundleDefaultConfig()
'base_urls' => [],
'packages' => [],
'json_manifest_path' => null,
'cache' => false,
],
'cache' => [
'pools' => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
'remote_manifest' => [
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
],
'cached' => [
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
'cache' => true,
],
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</framework:package>
<framework:package name="json_manifest_strategy" json-manifest-path="/path/to/manifest.json" />
<framework:package name="remote_manifest" json-manifest-path="https://cdn.example.com/manifest.json" />
<framework:package name="cached" json-manifest-path="https://cdn.example.com/manifest.json" cache="true" />
</framework:assets>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ framework:
json_manifest_path: '/path/to/manifest.json'
remote_manifest:
json_manifest_path: 'https://cdn.example.com/manifest.json'
cached:
json_manifest_path: 'https://cdn.example.com/manifest.json'
cache: true
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public function testAssets()

// packages
$packages = $packages->getArgument(1);
$this->assertCount(7, $packages);
$this->assertCount(8, $packages);

$package = $container->getDefinition((string) $packages['images_path']);
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
Expand All @@ -561,6 +561,10 @@ public function testAssets()
$versionStrategy = $container->getDefinition($package->getArgument(1));
$this->assertSame('assets.remote_json_manifest_version_strategy', $versionStrategy->getParent());
$this->assertSame('https://cdn.example.com/manifest.json', $versionStrategy->getArgument(0));

$package = $container->getDefinition($packages['cached']);
$versionStrategy = $container->getDefinition($package->getArgument(1));
$this->assertSame('assets.cached_version_strategy', $versionStrategy->getParent());
}

public function testAssetsDefaultVersionStrategyAsService()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Asset\Tests\VersionStrategy;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\VersionStrategy\CachedVersionStrategy;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class CachedVersionStrategyTest extends TestCase
{
public function testGetVersion()
{
$value = '1.0.0';
$strategy = $this->getMockBuilder(VersionStrategyInterface::class)->getMock();
$strategy
->expects($this->once())
->method('getVersion')
->willReturn($value);

$cache = new ArrayAdapter();
$cachedVersionStrategy = new CachedVersionStrategy($strategy, $cache);
$path = 'test-path';

$this->assertSame($value, $cachedVersionStrategy->getVersion($path));
$this->assertSame($value, $cachedVersionStrategy->getVersion($path), '2nd call is cached');
}

public function testApplyVersion()
{
$value = 'test/path/1.0.0';
$strategy = $this->getMockBuilder(VersionStrategyInterface::class)->getMock();
$strategy
->expects($this->once())
->method('applyVersion')
->willReturn($value);

$cache = new ArrayAdapter();
$cachedVersionStrategy = new CachedVersionStrategy($strategy, $cache);
$path = 'test/path';

$this->assertSame($value, $cachedVersionStrategy->applyVersion($path));
$this->assertSame($value, $cachedVersionStrategy->applyVersion($path), '2nd call is cached');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Asset\VersionStrategy;

use Symfony\Contracts\Cache\CacheInterface;

/**
* Cache generated versions and path of any version strategy.
*
* @author Jérôme TAMARELLE <jerome@tamarelle.net>
*/
class CachedVersionStrategy implements VersionStrategyInterface
{
private $strategy;

private $cache;

public function __construct(VersionStrategyInterface $strategy, CacheInterface $cache)
{
$this->strategy = $strategy;
$this->cache = $cache;
}

/**
* {@inheritdoc}
*/
public function getVersion(string $path)
{
return $this->cache->get('v_'.rawurlencode($path), function () use ($path) {
return $this->strategy->getVersion($path);
});
}

/**
* {@inheritdoc}
*/
public function applyVersion(string $path)
{
return $this->cache->get('p_'.rawurlencode($path), function () use ($path) {
return $this->strategy->applyVersion($path);
});
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Asset/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"require-dev": {
"symfony/http-client": "^4.4|^5.0",
"symfony/http-foundation": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0"
"symfony/http-kernel": "^4.4|^5.0",
"symfony/cache": "^4.4|^5.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Asset\\": "" },
Expand Down