8000 Option to make asset manifests strict on missing item · symfony/symfony@47e4487 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47e4487

Browse files
committed
Option to make asset manifests strict on missing item
1 parent 0000dfe commit 47e4487

File tree

16 files changed

+202
-115
lines changed

16 files changed

+202
-115
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
677677
->{!class_exists(FullStack::class) && class_exists(Package::class) ? 'canBeDisabled' : 'canBeEnabled'}()
678678
->fixXmlConfig('base_url')
679679
->children()
680+
->booleanNode('strict')->defaultFalse()->end()
680681
->scalarNode('version_strategy')->defaultNull()->end()
681682
->scalarNode('version')->defaultNull()->end()
682683
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
@@ -714,6 +715,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
714715
->prototype('array')
715716
->fixXmlConfig('base_url')
716717
->children()
718+
->booleanNode('strict')->defaultFalse()->end()
717719
->scalarNode('version_strategy')->defaultNull()->end()
718720
->scalarNode('version')
719721
->beforeNormalization()

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
10511051
if ($config['version_strategy']) {
10521052
$defaultVersion = new Reference($config['version_strategy']);
10531053
} else {
1054-
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], $config['json_manifest_path'], '_default');
1054+
$defaultVersion = $this->createVersion($container, $config['version'], $config['version_format'], $config['json_manifest_path'], '_default', $config['strict']);
10551055
}
10561056

10571057
$defaultPackage = $this->createPackageDefinition($config['base_path'], $config['base_urls'], $defaultVersion);
@@ -1068,7 +1068,7 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
10681068
// let format fallback to main version_format
10691069
$format = $package['version_format'] ?: $config['version_format'];
10701070
$version = isset($package['version']) ? $package['version'] : null;
1071-
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name);
1071+
$version = $this->createVersion($container, $version, $format, $package['json_manifest_path'], $name, $package['strict']);
10721072
}
10731073

10741074
$container->setDefinition('assets._package_'.$name, $this->createPackageDefinition($package['base_path'], $package['base_urls'], $version));
@@ -1101,7 +1101,7 @@ private function createPackageDefinition(?string $basePath, array $baseUrls, Ref
11011101
return $package;
11021102
}
11031103

1104-
private function createVersion(ContainerBuilder $container, ?string $version, ?string $format, ?string $jsonManifestPath, string $name): Reference
1104+
private function createVersion(ContainerBuilder $container, ?string $version, ?string $format, ?string $jsonManifestPath, string $name, bool $strict): Reference
11051105
{
11061106
// Configuration prevents $version and $jsonManifestPath from being set
11071107
if (null !== $version) {
@@ -1117,12 +1117,15 @@ private function createVersion(ContainerBuilder $container, ?string $version, ?s
11171117

11181118
if (null !== $jsonManifestPath) {
11191119
$definitionName = 'assets.json_manifest_version_strategy';
1120+
$strictArg = 1;
11201121
if (0 === strpos(parse_url($jsonManifestPath, \PHP_URL_SCHEME), 'http')) {
11211122
$definitionName = 'assets.remote_json_manifest_version_strategy';
1123+
$strictArg = 2;
11221124
}
11231125

11241126
$def = new ChildDefinition($definitionName);
11251127
$def->replaceArgument(0, $jsonManifestPath);
1128+
$def->replaceArgument($strictArg, $strict);
11261129
$container->setDefinition('assets._version_'.$name, $def);
11271130

11281131
return new Reference('assets._version_'.$name);

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

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

8283
->set('assets.remote_json_manifest_version_strategy', RemoteJsonManifestVersionStrategy::class)
8384
->abstract()
8485
->args([
8586
abstract_arg('manifest url'),
8687
service('http_client'),
88+
false,
8789
])
8890
;
8991
};

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<xsd:attribute name="version" type="xsd:string" />
152152
<xsd:attribute name="version-format" type="xsd:string" />
153153
<xsd:attribute name="json-manifest-path" type="xsd:string" />
154+
<xsd:attribute name="strict" type="xsd:boolean" />
154155
</xsd:complexType>
155156

156157
<xsd:complexType name="package">
@@ -164,6 +165,7 @@
164165
<xsd:attribute name="version" type="xsd:string" />
165166
<xsd:attribute name="version-format" type="xsd:string" />
166167
<xsd:attribute name="json-manifest-path" type="xsd:string" />
168+
<xsd:attribute name="strict" type="xsd:boolean" />
167169
</xsd:complexType>
168170

169171
<xsd:complexType name="translator">

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function testAssetsCanBeEnabled()
8383
'base_urls' => [],
8484
'packages' => [],
8585
'json_manifest_path' => null,
86+
'strict' => false,
8687
];
8788

8889
$this->assertEquals($defaultConfig, $config['assets']);
@@ -454,6 +455,7 @@ protected static function getBundleDefaultConfig()
454455
'base_urls' => [],
455456
'packages' => [],
456457
'json_manifest_path' => null,
458+
'strict' => false,
457459
],
458460
'cache' => [
459461
'pools' => [],

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
'remote_manifest' => [
3131
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
3232
],
33+
'strict_manifest_strategy' => [
34+
'json_manifest_path' => '/path/to/manifest.json',
35+
'strict' => true,
36+
],
37+
'strict_remote_manifest' => [
38+
'json_manifest_path' => 'https://cdn.example.com/manifest.json',
39+
'strict' => true,
40+
],
3341
],
3442
],
3543
]);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
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="strict_manifest_strategy" json-manifest-path="/path/to/manifest.json" strict="true" />
27+
<framework:package name="strict_remote_manifest" json-manifest-path="https://cdn.example.com/manifest.json" strict="true" />
2628
</framework:assets>
2729
</framework:config>
2830
</container>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ framework:
2121
json_manifest_path: '/path/to/manifest.json'
2222
remote_manifest:
2323
json_manifest_path: 'https://cdn.example.com/manifest.json'
24+
strict_manifest_strategy:
25+
json_manifest_path: '/path/to/manifest.json'
26+
strict: true
27+
strict_remote_manifest:
28+
json_manifest_path: 'https://cdn.example.com/manifest.json'
29+
strict: true

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public function testAssets()
579579

580580
// packages
581581
$packages = $packages->getArgument(1);
582-
$this->assertCount(7, $packages);
582+
$this->assertCount(9, $packages);
583583

584584
$package = $container->getDefinition((string) $packages['images_path']);
585585
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
@@ -600,11 +600,25 @@ public function testAssets()
600600
$versionStrategy = $container->getDefinition((string) $package->getArgument(1));
601601
$this->assertEquals('assets.json_manifest_version_strategy', $versionStrategy->getParent());
602602
$this->assertEquals('/path/to/manifest.json', $versionStrategy->getArgument(0));
603+
$this->assertFalse($versionStrategy->getArgument(1));
603604

604605
$package = $container->getDefinition($packages['remote_manifest']);
605606
$versionStrategy = $container->getDefinition($package->getArgument(1));
606607
$this->assertSame('assets.remote_json_manifest_version_strategy', $versionStrategy->getParent());
607608
$this->assertSame('https://cdn.example.com/manifest.json', $versionStrategy->getArgument(0));
609+
$this->assertFalse($versionStrategy->getArgument(2));
610+
611+
$package = $container->getDefinition((string) $packages['strict_manifest_strategy']);
612+
$versionStrategy = $container->getDefinition((string) $package->getArgument(1));
613+
$this->assertEquals('assets.json_manifest_version_strategy', $versionStrategy->getParent());
614+
$this->assertEquals('/path/to/manifest.json', $versionStrategy->getArgument(0));
615+
$this->assertTrue($versionStrategy->getArgument(1));
616+
617+
$package = $container->getDefinition($packages['strict_remote_manifest']);
618+
$versionStrategy = $container->getDefinition($package->getArgument(1));
619+
$this->assertSame('assets.remote_json_manifest_version_strategy', $versionStrategy->getParent());
620+
$this->assertSame('https://cdn.example.com/manifest.json', $versionStrategy->getArgument(0));
621+
$this->assertTrue($versionStrategy->getArgument(2));
608622
}
609623

610624
public function testAssetsDefaultVersionStrategyAsService()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
13+
14+
/**
15+
* Base RuntimeException for the Asset component.
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
class RuntimeException extends \RuntimeException implements ExceptionInterface
20+
{
21+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Exception\RuntimeException;
16+
17+
abstract class AbstractJsonManifestVersionStrategyTest extends TestCase
18+
{
19+
public function testGetVersion()
20+
{
21+
$strategy = $this->createStrategy('manifest-valid.json');
22+
23+
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
24+
}
25+
26+
public function testApplyVersion()
27+
{
28+
$strategy = $this->createStrategy('manifest-valid.json');
29+
30+
$this->assertSame('css/styles.555def.css', $strategy->getVersion('css/styles.css'));
31+
}
32+
33+
public function testApplyVersionWhenKeyDoesNotExistInManifest()
34+
{
35+
$strategy = $this->createStrategy('manifest-valid.json');
36+
37+
$this->assertSame('css/other.css', $strategy->getVersion('css/other.css'));
38+
}
39+
40+
public function testStrictExceptionWhenKeyDoesNotExistInManifest()
41+
{
42+
$strategy = $this->createStrategy('manifest-valid.json', true);
43+
44+
$this->expectException(RuntimeException::class);
45+
$this->expectExceptionMessage('Asset "css/other.css" not found in manifest "');
46+
47+
$strategy->getVersion('css/other.css');
48+
}
49+
50+
abstract protected function createStrategy($manifestFilename, $strict = false);
51+
}

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,10 @@
1111

1212
namespace Symfony\Component\Asset\Tests\VersionStrategy;
1313

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
1615

17-
class JsonManifestVersionStrategyTest extends TestCase
16+
class JsonManifestVersionStrategyTest extends AbstractJsonManifestVersionStrategyTest
1817
{
19-
public function testGetVersion()
20-
{
21-
$strategy = $this->createStrategy('manifest-valid.json');
22-
23-
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
24-
}
25-
26-
public function testApplyVersion()
27-
{
28-
$strategy = $this->createStrategy('manifest-valid.json');
29-
30-
$this->assertSame('css/styles.555def.css', $strategy->getVersion('css/styles.css'));
31-
}
32-
33-
public function testApplyVersionWhenKeyDoesNotExistInManifest()
34-
{
35-
$strategy = $this->createStrategy('manifest-valid.json');
36-
37-
$this->assertSame('css/other.css', $strategy->getVersion('css/other.css'));
38-
}
39-
4018
public function testMissingManifestFileThrowsException()
4119
{
4220
$this->expectException('RuntimeException');
@@ -52,8 +30,8 @@ public function testManifestFileWithBadJSONThrowsException()
5230
$strategy->getVersion('main.js');
5331
}
5432

55-
private function createStrategy($manifestFilename)
33+
protected function createStrategy($manifestFilename, $strict = false)
5634
{
57-
return new JsonManifestVersionStrategy(__DIR__.'/../fixtures/'.$manifestFilename);
35+
return new JsonManifestVersionStrategy(__DIR__.'/../fixtures/'.$manifestFilename, $strict);
5836
}
5937
}

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,30 @@
1111

1212
namespace Symfony\Component\Asset\Tests\VersionStrategy;
1313

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\Asset\VersionStrategy\RemoteJsonManifestVersionStrategy;
1615
use Symfony\Component\HttpClient\Exception\JsonException;
1716
use Symfony\Component\HttpClient\MockHttpClient;
1817
use Symfony\Component\HttpClient\Response\MockResponse;
1918

20-
class RemoteJsonManifestVersionStrategyTest extends TestCase
19+
class RemoteJsonManifestVersionStrategyTest extends AbstractJsonManifestVersionStrategyTest
2120
{
22-
public function testGetVersion()
23-
{
24-
$strategy = $this->createStrategy('https://cdn.example.com/manifest-valid.json');
25-
26-
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
27-
}
28-
29-
public function testApplyVersion()
30-
{
31-
$strategy = $this->createStrategy('https://cdn.example.com/manifest-valid.json');
32-
33-
$this->assertSame('css/styles.555def.css', $strategy->getVersion('css/styles.css'));
34-
}
35-
36-
public function testApplyVersionWhenKeyDoesNotExistInManifest()
37-
{
38-
$strategy = $this->createStrategy('https://cdn.example.com/manifest-valid.json');
39-
40-
$this->assertSame('css/other.css', $strategy->getVersion('css/other.css'));
41-
}
42-
4321
public function testMissingManifestFileThrowsException()
4422
{
4523
$this->expectException('RuntimeException');
4624
$this->expectExceptionMessage('HTTP 404 returned for "https://cdn.example.com/non-existent-file.json"');
47-
$strategy = $this->createStrategy('https://cdn.example.com/non-existent-file.json');
25+
$strategy = $this->createStrategy('non-existent-file.json');
4826
$strategy->getVersion('main.js');
4927
}
5028

5129
public function testManifestFileWithBadJSONThrowsException()
5230
{
5331
$this->expectException(JsonException::class);
5432
$this->expectExceptionMessage('Syntax error');
55-
$strategy = $this->createStrategy('https://cdn.example.com/manifest-invalid.json');
33+
$strategy = $this->createStrategy('manifest-invalid.json');
5634
$strategy->getVersion('main.js');
5735
}
5836

59-
private function createStrategy($manifestUrl)
37+
protected function createStrategy($manifestUrl, $strict = false)
6038
{
6139
$httpClient = new MockHttpClient(function ($method, $url, $options) {
6240
$filename = __DIR__.'/../fixtures/'.basename($url);
@@ -68,6 +46,6 @@ private function createStrategy($manifestUrl)
6846
return new MockResponse('{}', ['http_code' => 404]);
6947
});
7048

71-
return new RemoteJsonManifestVersionStrategy($manifestUrl, $httpClient);
49+
return new RemoteJsonManifestVersionStrategy('https://cdn.example.com/'.$manifestUrl, $httpClient, $strict);
7250
}
7351
}

0 commit comments

Comments
 (0)
0