Skip to content

Commit 17cf357

Browse files
committed
Allows to download asset manifest from a remote url
1 parent 8c80c5b commit 17cf357

File tree

6 files changed

+198
-5
lines changed

6 files changed

+198
-5
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
576576
->scalarNode('version')->defaultNull()->end()
577577
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
578578
->scalarNode('json_manifest_path')->defaultNull()->end()
579+
->scalarNode('json_manifest_url')->defaultNull()->end()
579580
->scalarNode('base_path')->defaultValue('')->end()
580581
->arrayNode('base_urls')
581582
->requiresAtLeastOneElement()
@@ -601,6 +602,24 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
601602
})
602603
->thenInvalid('You cannot use both "version" and "json_manifest_path" at the same time under "assets".')
603604
->end()
605+
->validate()
606+
->ifTrue(function ($v) {
607+
return isset($v['version_strategy']) && isset($v['json_manifest_url']);
608+
})
609+
->thenInvalid('You cannot use both "version_strategy" and "json_manifest_url" at the same time under "assets" packages.')
610+
->end()
611+
->validate()
612+
->ifTrue(function ($v) {
613+
return isset($v['version']) && isset($v['json_manifest_url']);
614+
})
615+
->thenInvalid('You cannot use both "version" and "json_manifest_url" at the same time under "assets" packages.')
616+
->end()
617+
->validate()
618+
->ifTrue(function ($v) {
619+
return isset($v['json_manifest_path']) && isset($v['json_manifest_url']);
620+
})
621+
->thenInvalid('You cannot use both "json_manifest_path" and "json_manifest_url" at the same time under "assets" packages.')
622+
->end()
604623
->fixXmlConfig('package')
605624
->children()
606625
->arrayNode('packages')
@@ -618,6 +637,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
618637
->end()
619638
->scalarNode('version_format')->defaultNull()->end()
620639
->scalarNode('json_manifest_path')->defaultNull()->end()
640+
->scalarNode('json_manifest_url')->defaultNull()->end()
621641
->scalarNode('base_path')->defaultValue('')->end()
622642
->arrayNode('base_urls')
623643
->requiresAtLeastOneElement()
@@ -643,6 +663,24 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
643663
})
644664
->thenInvalid('You cannot use both "version" and "json_manifest_path" at the same time under "assets" packages.')
645665
->end()
666+
->validate()
667+
->ifTrue(function ($v) {
668+
return isset($v['version_strategy']) && isset($v['json_manifest_url']);
669+
})
670+
->thenInvalid('You cannot use both "version_strategy" and "json_manifest_url" at the same time under "assets" packages.')
671+
->end()
672+
->validate()
673+
->ifTrue(function ($v) {
674+
return isset($v['version']) && isset($v['json_manifest_url']);
675+
})
676+
->thenInvalid('You cannot use both "version" and "json_manifest_url" at the same time under "assets" packages.')
677+
->end()
678+
->validate()
679+
->ifTrue(function ($v) {
680+
return isset($v['json_manifest_path']) && isset($v['json_manifest_url']);
681+
})
682+
->thenInvalid('You cannot use both "json_manifest_path" and "json_manifest_url" at the same time under "assets" packages.')
683+
->end()
646684
->end()
647685
->end()
648686
->end()

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
943943
if ($config['version_strategy']) {
944944
$defaultVersion = new Reference($config['version_strategy']);
945945
} else {
946-
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], $config['json_manifest_path'], '_default');
946+
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], $config['json_manifest_path'], $config['json_manifest_url'], '_default');
947947
}
948948

949949
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
@@ -953,14 +953,14 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
953953
foreach ($config['packages'] as $name => $package) {
954954
if (null !== $package['version_strategy']) {
955955
$version = new Reference($package['version_strategy']);
956-
} elseif (!\array_key_exists('version', $package) && null === $package['json_manifest_path']) {
957-
// if neither version nor json_manifest_path are specified, use the default
956+
} elseif (!\array_key_exists('version', $package) && null === $package['json_manifest_path'] && null === $package['json_manifest_url']) {
957+
// if neither version nor json_manifest_path nor json_manifest_url are specified, use the default
958958
$version = $defaultVersion;
959959
} else {
960960
// let format fallback to main version_format
961961
$format = $package['version_format'] ?: $config['version_format'];
962962
$version = isset($package['version']) ? $package['version'] : null;
963-
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name);
963+
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $package['json_manifest_url'], $name);
964964
}
965965

966966
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
@@ -993,7 +993,7 @@ private function createPackageDefinition(?string $basePath, array $baseUrls, Ref
993993
return $package;
994994
}
995995

996-
private function createVersion(ContainerBuilder $container, ?string $version, ?string $format, ?string $jsonManifestPath, string $name): Reference
996+
private function createVersion(ContainerBuilder $container, ?string $version, ?string $format, ?string $jsonManifestPath, ?string $jsonManifestUrl, string $name): Reference
997997
{
998998
// Configuration prevents $version and $jsonManifestPath from being set
999999
if (null !== $version) {
@@ -1015,6 +1015,14 @@ private function createVersion(ContainerBuilder $container, ?string $version, ?s
10151015
return new Reference('assets._version_'.$name);
10161016
}
10171017

1018+
if (null !== $jsonManifestUrl) {
1019+
$def = new ChildDefinition('assets.remote_json_manifest_version_strategy');
1020+
$def->replaceArgument(0, $jsonManifestUrl);
1021+
$container->setDefinition('assets._version_'.$name, $def);
1022+
1023+
return new Reference('assets._version_'.$name);
1024+
}
1025+
10181026
return new Reference('assets.empty_version_strategy');
10191027
}
10201028

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@
5050
<service id="assets.json_manifest_version_strategy" class="Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy" abstract="true">
5151
<argument /> <!-- manifest path -->
5252
</service>
53+
54+
<service id="assets.remote_json_manifest_version_strategy" class="Symfony\Component\Asset\VersionStrategy\RemoteJsonManifestVersionStrategy" abstract="true">
55+
<argument /> <!-- manifest url -->
56+
<argument type="service" id="http_client"/>
57+
</service>
5358
</services>
5459
</container>

src/Symfony/Component/Asset/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* added `RemoteJsonManifestVersionStrategy` to download manifest over HTTP.
8+
49
4.2.0
510
-----
611

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Tests\VersionStrategy;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Asset\VersionStrategy\RemoteJsonManifestVersionStrategy;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
18+
19+
class RemoteJsonManifestVersionStrategyTest extends TestCase
20+
{
21+
public function testGetVersion()
22+
{
23+
$strategy = $this->createStrategy('https://cdn.example.com/manifest-valid.json');
24+
25+
$this->assertEquals('main.123abc.js', $strategy->getVersion('main.js'));
26+
}
27+
28+
public function testApplyVersion()
29+
{
30+
$strategy = $this->createStrategy('https://cdn.example.com/manifest-valid.json');
31+
32+
$this->assertEquals('css/styles.555def.css', $strategy->getVersion('css/styles.css'));
33+
}
34+
35+
public function testApplyVersionWhenKeyDoesNotExistInManifest()
36+
{
37+
$strategy = $this->createStrategy('https://cdn.example.com/manifest-valid.json');
38+
39+
$this->assertEquals('css/other.css', $strategy->getVersion('css/other.css'));
40+
}
41+
42+
public function testMissingManifestFileThrowsException()
43+
{
44+
$this->expectException('RuntimeException');
45+
$strategy = $this->createStrategy('https://cdn.example.com/non-existent-file.json');
46+
$strategy->getVersion('main.js');
47+
}
48+
49+
public function testManifestFileWithBadJSONThrowsException()
50+
{
51+
$this->expectException('RuntimeException');
52+
$this->expectExceptionMessage('Error parsing JSON');
53+
$strategy = $this->createStrategy('https://cdn.example.com/manifest-invalid.json');
54+
$strategy->getVersion('main.js');
55+
}
56+
57+
private function createStrategy($manifestUrl)
58+
{
59+
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
60+
$filename = __DIR__.'/../fixtures/'.basename($url);
61+
62+
if (file_exists($filename)) {
63+
return new MockResponse(file_get_contents($filename));
64+
}
65+
66+
return new MockResponse('{}', ['http_code' => 404]);
67+
});
68+
69+
return new RemoteJsonManifestVersionStrategy($manifestUrl, $this->httpClient);
70+
}
71+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\HttpClient\HttpClientInterface;
15+
16+
/**
17+
* Reads the versioned path of an asset from a remote JSON manifest file.
18+
*
19+
* For example, the manifest file might look like this:
20+
* {
21+
* "main.js": "main.abc123.js",
22+
* "css/styles.css": "css/styles.555abc.css"
23+
* }
24+
*
25+
* You could then ask for the version of "main.js" or "css/styles.css".
26+
*/
27+
class RemoteJsonManifestVersionStrategy implements VersionStrategyInterface
28+
{
29+
private $manifestData;
30+
private $httpResponse;
31+
32+
/**
33+
* @param string $manifestUrl Absolute url to the manifest file
34+
*/
35+
public function __construct(string $manifestUrl, HttpClientInterface $httpClient)
36+
{
37+
$this->httpResponse = $httpClient->request('GET', $manifestUrl);
38+
}
39+
40+
/**
41+
* With a manifest, we don't really know or care about what
42+
* the version is. Instead, this returns the path to the
43+
* versioned file.
44+
*/
45+
public function getVersion(string $path)
46+
{
47+
return $this->applyVersion($path);
48+
}
49+
50+
public function applyVersion(string $path)
51+
{
52+
return $this->getManifestPath($path) ?: $path;
53+
}
54+
55+
private function getManifestPath(string $path): ?string
56+
{
57+
if (null === $this->manifestData) {
58+
$this->manifestData = json_decode($this->httpResponse->getContent(), true);
59+
if (0 < json_last_error()) {
60+
throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s" - %s', $this->httpResponse->getInfo()['url'], json_last_error_msg()));
61+
}
62+
}
63+
64+
return isset($this->manifestData[$path]) ? $this->manifestData[$path] : null;
65+
}
66+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy