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

Commit 8bcf3d8

Browse filesBrowse files
committed
Allow env variables in json_manifest_path
1 parent e2053d0 commit 8bcf3d8
Copy full SHA for 8bcf3d8

File tree

8 files changed

+143
-1
lines changed
Filter options

8 files changed

+143
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,8 @@ private function createVersion(ContainerBuilder $container, ?string $version, ?s
11321132
$definitionName = 'assets.json_manifest_version_strategy';
11331133
if (0 === strpos(parse_url($jsonManifestPath, \PHP_URL_SCHEME), 'http')) {
11341134
$definitionName = 'assets.remote_json_manifest_version_strategy';
1135+
} elseif ($container->resolveEnvPlaceholders($jsonManifestPath) !== $jsonManifestPath) {
1136+
$definitionName = 'assets.dynamic_json_manifest_version_strategy';
11351137
}
11361138

11371139
$def = new ChildDefinition($definitionName);

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Asset\Packages;
1717
use Symfony\Component\Asset\PathPackage;
1818
use Symfony\Component\Asset\UrlPackage;
19+
use Symfony\Component\Asset\VersionStrategy\DynamicJsonManifestVersionStrategy;
1920
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
2021
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
2122
use Symfony\Component\Asset\VersionStrategy\RemoteJsonManifestVersionStrategy;
@@ -85,5 +86,12 @@
8586
abstract_arg('manifest url'),
8687
service('http_client'),
8788
])
89+
90+
->set('assets.dynamic_json_manifest_version_strategy', DynamicJsonManifestVersionStrategy::class)
91+
->abstract()
92+
->args([
93+
abstract_arg('manifest url'),
94+
service('http_client'),
95+
])
8896
;
8997
};

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/assets.php
+9Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/assets.xml
+7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/assets.yml
+8Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+11-1Lines changed: 11 additions & 1 deletion
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');
@@ -623,6 +623,16 @@ public function testAssets()
623623
$versionStrategy = $container->getDefinition($package->getArgument(1));
624624
$this->assertSame('assets.remote_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.remote_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.dynamic_json_manifest_version_strategy', $versionStrategy->getParent());
635+
$this->assertStringMatchesFormat('env_%s', $versionStrategy->getArgument(0));
626636
}
627637

628638
public function testAssetsDefaultVersionStrategyAsService()
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\DynamicJsonManifestVersionStrategy;
16+
use Symfony\Component\Asset\VersionStrategy\RemoteJsonManifestVersionStrategy;
17+
use Symfony\Component\HttpClient\Exception\JsonException;
18+
use Symfony\Component\HttpClient\MockHttpClient;
19+
use Symfony\Component\HttpClient\Response\MockResponse;
20+
21+
class DynamicJsonManifestVersionStrategyTest extends TestCase
22+
{
23+
/**
24+
* @dataProvider ProvideStrategies
25+
*/
26+
public function testGetVersionRemote(DynamicJsonManifestVersionStrategy $strategy)
27+
{
28+
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
29+
}
30+
31+
/**
32+
* @dataProvider ProvideStrategies
33+
*/
34+
public function testApplyVersionRemote($strategy)
35+
{
36+
$this->assertSame('css/styles.555def.css', $strategy->ApplyVersion('css/styles.css'));
37+
}
38+
39+
public function ProvideStrategies()
40+
{
41+
$httpClient = new MockHttpClient(function ($method, $url, $options) {
42+
$filename = __DIR__.'/../fixtures/'.basename($url);
43+
44+
if (file_exists($filename)) {
45+
return new MockResponse(file_get_contents($filename), ['http_headers' => ['content-type' => 'application/json']]);
46+
}
47+
48+
return new MockResponse('{}', ['http_code' => 404]);
49+
});
50+
51+
yield [new DynamicJsonManifestVersionStrategy('https://cdn.example.com/manifest-valid.json', $httpClient)];
52+
53+
$httpClient = new MockHttpClient();
54+
yield [new DynamicJsonManifestVersionStrategy(__DIR__.'/../fixtures/manifest-valid.json', $httpClient)];
55+
}
56+
}
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 local or remote JSON manifest file.
18+
*/
19+
class DynamicJsonManifestVersionStrategy implements VersionStrategyInterface
20+
{
21+
private $decorated;
22+
23+
public function __construct(string $jsonManifestPath, HttpClientInterface $httpClient)
24+
{
25+
if (0 === strpos(parse_url($jsonManifestPath, \PHP_URL_SCHEME), 'http')) {
26+
$this->decorated = new RemoteJsonManifestVersionStrategy($jsonManifestPath, $httpClient);
27+
} else {
28+
$this->decorated = new JsonManifestVersionStrategy($jsonManifestPath);
29+
}
30+
}
31+
32+
public function getVersion(string $path)
33+
{
34+
return $this->decorated->getVersion($path);
35+
}
36+
37+
public function applyVersion(string $path)
38+
{
39+
return $this->decorated->applyVersion($path);
40+
}
41+
42+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.