Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
feature #1518 add json manifest version strategy support (MatTheCat)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.1.x-dev branch.

Discussion
----------

add json manifest version strategy support

Commits
-------

65382f4 add json manifest version strategy support
  • Loading branch information
fabpot committed Jun 15, 2017
2 parents 268e3d3 + 65382f4 commit 9cbf194
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Silex/Provider/AssetServiceProvider.php
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Asset\UrlPackage;
use Symfony\Component\Asset\Context\RequestStackContext;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;

/**
Expand All @@ -33,7 +34,7 @@ public function register(Container $app)
$app['assets.packages'] = function ($app) {
$packages = array();
foreach ($app['assets.named_packages'] as $name => $package) {
$version = $app['assets.strategy_factory'](isset($package['version']) ? $package['version'] : '', isset($package['version_format']) ? $package['version_format'] : null);
$version = $app['assets.strategy_factory'](isset($package['version']) ? $package['version'] : null, isset($package['version_format']) ? $package['version_format'] : null, isset($package['json_manifest_path']) ? $package['json_manifest_path'] : null, $name);

$packages[$name] = $app['assets.package_factory'](isset($package['base_path']) ? $package['base_path'] : '', isset($package['base_urls']) ? $package['base_urls'] : array(), $version, $name);
}
Expand All @@ -42,7 +43,7 @@ public function register(Container $app)
};

$app['assets.default_package'] = function ($app) {
$version = $app['assets.strategy_factory']($app['assets.version'], $app['assets.version_format']);
$version = $app['assets.strategy_factory']($app['assets.version'], $app['assets.version_format'], $app['assets.json_manifest_path'], 'default');

return $app['assets.package_factory']($app['assets.base_path'], $app['assets.base_urls'], $version, 'default');
};
Expand All @@ -55,17 +56,30 @@ public function register(Container $app)
$app['assets.base_urls'] = array();
$app['assets.version'] = null;
$app['assets.version_format'] = null;
$app['assets.json_manifest_path'] = null;

$app['assets.named_packages'] = array();

// prototypes

$app['assets.strategy_factory'] = $app->protect(function ($version, $format) use ($app) {
if (!$version) {
return new EmptyVersionStrategy();
$app['assets.strategy_factory'] = $app->protect(function ($version, $format, $jsonManifestPath, $name) use ($app) {
if ($version && $jsonManifestPath) {
throw new \LogicException(sprintf('Asset package "%s" cannot have version and manifest.', $name));
}

return new StaticVersionStrategy($version, $format);
if ($version) {
return new StaticVersionStrategy($version, $format);
}

if ($jsonManifestPath) {
if (!class_exists('Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy')) {
throw new \RuntimeException('You must require symfony/asset >= 3.3 to use JSON manifest version strategy.');
}

return new JsonManifestVersionStrategy($jsonManifestPath);
}

return new EmptyVersionStrategy();
});

$app['assets.package_factory'] = $app->protect(function ($basePath, $baseUrls, $version, $name) use ($app) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Silex/Tests/Fixtures/manifest.json
@@ -0,0 +1,3 @@
{
"app.js": "some-random-hash.js"
}
16 changes: 16 additions & 0 deletions tests/Silex/Tests/Provider/AssetServiceProviderTest.php
Expand Up @@ -33,4 +33,20 @@ public function testGenerateAssetUrl()
$this->assertEquals('/whatever-makes-sense/foo.css?css2', $app['assets.packages']->getUrl('foo.css', 'css'));
$this->assertEquals('https://img.example.com/foo.png', $app['assets.packages']->getUrl('/foo.png', 'images'));
}

public function testJsonManifestVersionStrategy()
{
if (!class_exists('Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy')) {
$this->markTestSkipped('JsonManifestVersionStrategy class is not available.');

return;
}

$app = new Application();
$app->register(new AssetServiceProvider(), array(
'assets.json_manifest_path' => __DIR__.'/../Fixtures/manifest.json',
));

$this->assertEquals('/some-random-hash.js', $app['assets.packages']->getUrl('app.js'));
}
}

0 comments on commit 9cbf194

Please sign in to comment.