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

[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 1 commit
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
Next Next commit
Add CachedVersionStrategy to decorate any Asset version strategy
framework:
    assets:
        json_manifest_path: 'https://cdn.example.com/manifest.json'
        cache_version: true
  • Loading branch information
GromNaN committed May 6, 2020
commit dc90706bc90463cd78b053fc3ba3fd95d2af18b3
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_version')->defaultFalse()->end()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not defaultNull?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've kept the boolean and removed options.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use_cache => true/false/string => when string, it's the name of the cache pool to use?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(or just cache btw)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't get the name cache_version
cache: true/false looks better to me. Do I miss something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed to cache.
I wanted to avoid confusion. This cache the result of the URL generation, a developer might think this is the HTTP cache when the browser requests the URL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_json_manifest then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or json_manifest_cache might even be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can cache any version strategy, not only the manifest.
We could also remove this option and always enable the cache on remote json manifests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we? How would this work with env vars?
Or we might just always enable the cache, even for local files.
For thoughts :)

->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_version')->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 (false !== $config['cache_version']) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
$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 (false !== $package['cache_version']) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
$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 4 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-version" 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-version" 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_version' => 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_version' => 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_version' => 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-version="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_version: 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 3 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.