8000 Allow env variables in `json_manifest_path` · symfony/symfony@4e4a81c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e4a81c

Browse files
committed
Allow env variables in json_manifest_path
1 parent c82567b commit 4e4a81c

File tree

15 files changed

+138
-33
lines changed

15 files changed

+138
-33
lines changed

UPGRADE-5.3.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 5.2 to 5.3
22
=======================
33

4+
Asset
5+
-----
6+
7+
* Deprecated `RemoteJsonManifestVersionStrategy`, use `JsonManifestVersionStrategy` instead.
8+
49
Form
510
----
611

UPGRADE-6.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 5.x to 6.0
22
=======================
33

4+
Asset
5+
-----
6+
7+
* Removed `RemoteJsonManifestVersionStrategy`, use `JsonManifestVersionStrategy` instead.
8+
49
Config
510
------
611

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,12 +1129,7 @@ private function createVersion(ContainerBuilder $container, ?string $version, ?s
11291129
}
11301130

11311131
if (null !== $jsonManifestPath) {
1132-
$definitionName = 'assets.json_manifest_version_strategy';
1133-
if (0 === strpos(parse_url($jsonManifestPath, \PHP_URL_SCHEME), 'http')) {
1134-
$definitionName = 'assets.remote_json_manifest_version_strategy';
1135-
}
1136-
1137-
$def = new ChildDefinition($definitionName);
1132+
$def = new ChildDefinition('assets.json_manifest_version_strategy');
11381133
$def->replaceArgument(0, $jsonManifestPath);
11391134
$container->setDefinition('assets._version_'.$name, $def);
11401135

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@
7777
->abstract()
7878
->args([
7979
abstract_arg('manifest path'),
80+
service('http_client'),
8081
])
8182

8283
->set('assets.remote_json_manifest_version_strategy', RemoteJsonManifestVersionStrategy::class)
8384
->abstract()
85+
->deprecate('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "assets.json_manifest_version_strategy" instead.')
8486
->args([
8587
abstract_arg('manifest url'),
8688
service('http_client'),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
'remote_manifest' => [
3131
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
3232
],
33+
'var_manifest' => [
34+
'json_manifest_path' => '%var_json_manifest_path%',
35+
],
36+
'env_manifest' => [
37+
'json_manifest_path' => '%env(env_manifest)%',
38+
],
3339
],
3440
],
3541
]);
42+
43+
$container->setParameter('var_json_manifest_path', 'https://cdn.example.com/manifest.json');
44+
$container->setParameter('env(env_manifest)', 'https://cdn.example.com/manifest.json');

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
</framework:package>
2424
<framework:package name="json_manifest_strategy" json-manifest-path="/path/to/manifest.json" />
2525
<framework:package name="remote_manifest" json-manifest-path="https://cdn.example.com/manifest.json" />
26+
<framework:package name="var_manifest" json-manifest-path="%var_json_manifest_path%" />
27+
<framework:package name="env_manifest" json-manifest-path="%env(env_manifest)%" />
2628
</framework:assets>
2729
</framework:config>
30+
31+
<parameters>
32+
<parameter key="var_json_manifest_path">https://cdn.example.com/manifest.json</parameter>
33+
<parameter key="env(env_manifest)">https://cdn.example.com/manifest.json</parameter>
34+
</parameters>
2835
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ framework:
2121
json_manifest_path: '/path/to/manifest.json'
2222
remote_manifest:
2323
json_manifest_path: 'https://cdn.example.com/manifest.json'
24+
var_manifest:
25+
json_manifest_path: '%var_json_manifest_path%'
26+
env_manifest:
27+
json_manifest_path: '%env(env_manifest)%'
28+
29+
parameters:
30+
var_json_manifest_path: 'https://cdn.example.com/manifest.json'
31+
env(env_manifest): https://cdn.example.com/manifest.json

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public function testAssets()
597597

598598
// packages
599599
$packages = $packages->getArgument(1);
600-
$this->assertCount(7, $packages);
600+
$this->assertCount(9, $packages);
601601

602602
$package = $container->getDefinition((string) $packages['images_path']);
603603
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
@@ -621,8 +621,18 @@ public function testAssets()
621621

622622
$package = $container->getDefinition($packages['remote_manifest']);
623623
$versionStrategy = $container->getDefinition($package->getArgument(1));
624-
$this->assertSame('assets.remote_json_manifest_version_strategy', $versionStrategy->getParent());
624+
$this->assertSame('assets.json_manifest_version_strategy', $versionStrategy->getParent());
625625
$this->assertSame('https://cdn.example.com/manifest.json', $versionStrategy->getArgument(0));
626+
627+
$package = $container->getDefinition($packages['var_manifest']);
628+
$versionStrategy = $container->getDefinition($package->getArgument(1));
629+
$this->assertSame('assets.json_manifest_version_strategy', $versionStrategy->getParent());
630+
$this->assertSame('https://cdn.example.com/manifest.json', $versionStrategy->getArgument(0));
631+
632+
$package = $container->getDefinition($packages['env_manifest']);
633+
$versionStrategy = $container->getDefinition($package->getArgument(1));
634+
$this->assertSame('assets.json_manifest_version_strategy', $versionStrategy->getParent());
635+
$this->assertStringMatchesFormat('env_%s', $versionStrategy->getArgument(0));
626636
}
627637

628638
public function testAssetsDefaultVersionStrategyAsService()

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"require-dev": {
3535
"doctrine/annotations": "~1.7",
3636
"doctrine/cache": "~1.0",
37-
"symfony/asset": "^5.1",
37+
"symfony/asset": "^5.3",
3838
"symfony/browser-kit": "^4.4|^5.0",
3939
"symfony/console": "^5.2",
4040
"symfony/css-selector": "^4.4|^5.0",
@@ -71,7 +71,7 @@
7171
"phpdocumentor/reflection-docblock": "<3.0",
7272
"phpdocumentor/type-resolver": "<0.2.1",
7373
"phpunit/phpunit": "<5.4.3",
74-
"symfony/asset": "<5.1",
74+
"symfony/asset": "<5.3",
7575
"symfony/browser-kit": "<4.4",
7676
"symfony/console": "<5.2",
7777
"symfony/dotenv": "<5.1",

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.3.0
5+
-----
6+
7+
* deprecated `RemoteJsonManifestVersionStrategy`, use `JsonManifestVersionStrategy` instead.
8+
49
5.1.0
510
-----
611

src/Symfony/Component/Asset/Tests/VersionStrategy/JsonManifestVersionStrategyTest.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,83 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
1618

1719
class JsonManifestVersionStrategyTest extends TestCase
1820
{
19-
public function testGetVersion()
21+
/**
22+
* @dataProvider ProvideValidStrategies
23+
*/
24+
public function testGetVersion(JsonManifestVersionStrategy $strategy)
2025
{
21-
$strategy = $this->createStrategy('manifest-valid.json');
22-
2326
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
2427
}
2528

26-
public function testApplyVersion()
29+
/**
30+
* @dataProvider ProvideValidStrategies
31+
*/
32+
public function testApplyVersion(JsonManifestVersionStrategy $strategy)
2733
{
28-
$strategy = $this->createStrategy('manifest-valid.json');
29-
3034
$this->assertSame('css/styles.555def.css', $strategy->applyVersion('css/styles.css'));
3135
}
3236

33-
public function testApplyVersionWhenKeyDoesNotExistInManifest()
37+
/**
38+
* @dataProvider ProvideValidStrategies
39+
*/
40+
public function testApplyVersionWhenKeyDoesNotExistInManifest(JsonManifestVersionStrategy $strategy)
3441
{
35-
$strategy = $this->createStrategy('manifest-valid.json');
36-
3742
$this->assertSame('css/other.css', $strategy->applyVersion('css/other.css'));
3843
}
3944

40-
public function testMissingManifestFileThrowsException()
45+
/**
46+
* @dataProvider ProvideMissingStrategies
47+
*/
48+
public function testMissingManifestFileThrowsException(JsonManifestVersionStrategy $strategy)
4149
{
4250
$this->expectException('RuntimeException');
43-
$strategy = $this->createStrategy('non-existent-file.json');
4451
$strategy->getVersion('main.js');
4552
}
4653

47-
public function testManifestFileWithBadJSONThrowsException()
54+
/**
55+
* @dataProvider ProvideInvalidStrategies
56+
*/
57+
public function testManifestFileWithBadJSONThrowsException(JsonManifestVersionStrategy $strategy)
4858
{
4959
$this->expectException('RuntimeException');
5060
$this->expectExceptionMessage('Error parsing JSON');
51-
$strategy = $this->createStrategy('manifest-invalid.json');
5261
$strategy->getVersion('main.js');
5362
}
5463

55-
private function createStrategy($manifestFilename)
64+
public function provideValidStrategies()
65+
{
66+
yield from $this->provideStrategies('manifest-valid.json');
67+
}
68+
69+
public function provideInvalidStrategies()
70+
{
71+
yield from $this->provideStrategies('manifest-invalid.json');
72+
}
73+
74+
public function provideMissingStrategies()
75+
{
76+
yield from $this->provideStrategies('non-existent-file.json');
77+
}
78+
79+
public function provideStrategies(string $manifestPath)
5680
{
57-
return new JsonManifestVersionStrategy(__DIR__.'/../fixtures/'.$manifestFilename);
81+
$httpClient = new MockHttpClient(function ($method, $url, $options) {
82+
$filename = __DIR__.'/../fixtures/'.basename($url);
83+
84+
if (file_exists($filename)) {
85+
return new MockResponse(file_get_contents($filename), ['http_headers' => ['content-type' => 'application/json']]);
86+
}
87+
88+
return new MockResponse('{}', ['http_code' => 404]);
89+
});
90+
91+
yield [new JsonManifestVersionStrategy('https://cdn.example.com/'.$manifestPath, $httpClient)];
92+
93+
yield [new JsonManifestVersionStrategy(__DIR__.'/../fixtures/'.$manifestPath)];
5894
}
5995
}

src/Symfony/Component/Asset/Tests/VersionStrategy/RemoteJsonManifestVersionStrategyTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use Symfony\Component\HttpClient\MockHttpClient;
1818
use Symfony\Component\HttpClient\Response\MockResponse;
1919

20+
/**
21+
* @group legacy
22+
*/
2023
class RemoteJsonManifestVersionStrategyTest extends TestCase
2124
{
2225
public function testGetVersion()

src/Symfony/Component/Asset/VersionStrategy/JsonManifestVersionStrategy.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Asset\VersionStrategy;
1313

14+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
15+
use Symfony\Contracts\HttpClient\HttpClientInterface;
16+
1417
/**
1518
* Reads the versioned path of an asset from a JSON manifest file.
1619
*
@@ -26,13 +29,15 @@ class JsonManifestVersionStrategy implements VersionStrategyInterface
2629
{
2730
private $manifestPath;
2831
private $manifestData;
32+
private $httpClient;
2933

3034
/**
3135
* @param string $manifestPath Absolute path to the manifest file
3236
*/
33-
public function __construct(string $manifestPath)
37+
public function __construct(string $manifestPath, HttpClientInterface $httpClient = null)
3438
{
3539
$this->manifestPath = $manifestPath;
40+
$this->httpClient = $httpClient;
3641
}
3742

3843
/**
@@ -53,13 +58,23 @@ public function applyVersion(string $path)
5358
private function getManifestPath(string $path): ?string
5459
{
5560
if (null === $this->manifestData) {
56-
if (!is_file($this->manifestPath)) {
57-
throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath));
58-
}
61+
if (null !== $this->httpClient && 0 === strpos(parse_url($this->manifestPath, \PHP_URL_SCHEME), 'http')) {
62+
try {
63+
$this->manifestData = $this->httpClient->request('GET', $this->manifestPath, [
64+
'headers' => ['accept' => 'application/json'],
65+
])->toArray();
66+
} catch (DecodingExceptionInterface $e) {
67+
throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest URL "%s".', $this->manifestPath), 0, $e);
68+
}
69+
} else {
70+
if (!is_file($this->manifestPath)) {
71+
throw new \RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $this->manifestPath));
72+
}
5973

60-
$this->manifestData = json_decode(file_get_contents($this->manifestPath), true);
61-
if (0 < json_last_error()) {
62-
throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": ', $this->manifestPath).json_last_error_msg());
74+
$this->manifestData = json_decode(file_get_contents($this->manifestPath), true);
75+
if (0 < json_last_error()) {
76+
throw new \RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": ', $this->manifestPath).json_last_error_msg());
77+
}
6378
}
6479
}
6580

src/Symfony/Component/Asset/VersionStrategy/RemoteJsonManifestVersionStrategy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Contracts\HttpClient\HttpClientInterface;
1515

16+
trigger_deprecation('symfony/asset', '5.3', 'The "%s" class is deprecated, use "%s" instead.', RemoteJsonManifestVersionStrategy::class, JsonManifestVersionStrategy::class);
17+
1618
/**
1719
* Reads the versioned path of an asset from a remote JSON manifest file.
1820
*
@@ -23,6 +25,8 @@
2325
* }
2426
*
2527
* You could then ask for the version of "main.js" or "css/styles.css".
28+
*
29+
* @deprecated since Symfony 5.3, use JsonManifestVersionStrategy instead.
2630
*/
2731
class RemoteJsonManifestVersionStrategy implements VersionStrategyInterface
2832
{

src/Symfony/Component/Asset/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.2.5"
19+
"php": ">=7.2.5",
20+
"symfony/deprecation-contracts": "^2.1"
2021
},
2122
"suggest": {
2223
"symfony/http-foundation": ""

0 commit comments

Comments
 (0)
0