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 92d5fde

Browse filesBrowse files
committed
Remove wrong abstraction
1 parent 47e4487 commit 92d5fde
Copy full SHA for 92d5fde

6 files changed

+140
-134
lines changed

‎src/Symfony/Component/Asset/Tests/VersionStrategy/AbstractJsonManifestVersionStrategyTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/Tests/VersionStrategy/AbstractJsonManifestVersionStrategyTest.php
-51Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/Tests/VersionStrategy/JsonManifestVersionStrategyTest.php
+37-4Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,59 @@
1111

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

14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Asset\Exception\RuntimeException;
1416
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
1517

16-
class JsonManifestVersionStrategyTest extends AbstractJsonManifestVersionStrategyTest
18+
class JsonManifestVersionStrategyTest extends TestCase
1719
{
20+
public function testGetVersion()
21+
{
22+
$strategy = $this->createStrategy('manifest-valid.json');
23+
24+
$this->assertSame('main.123abc.js', $strategy->getVersion('main.js'));
25+
}
26+
27+
public function testApplyVersion()
28+
{
29+
$strategy = $this->createStrategy('manifest-valid.json');
30+
31+
$this->assertSame('css/styles.555def.css', $strategy->getVersion('css/styles.css'));
32+
}
33+
34+
public function testApplyVersionWhenKeyDoesNotExistInManifest()
35+
{
36+
$strategy = $this->createStrategy('manifest-valid.json');
37+
38+
$this->assertSame('css/other.css', $strategy->getVersion('css/other.css'));
39+
}
40+
1841
public function testMissingManifestFileThrowsException()
1942
{
20-
$this->expectException('RuntimeException');
43+
$this->expectException(RuntimeException::class);
2144
$strategy = $this->createStrategy('non-existent-file.json');
2245
$strategy->getVersion('main.js');
2346
}
2447

2548
public function testManifestFileWithBadJSONThrowsException()
2649
{
27-
$this->expectException('RuntimeException');
50+
$this->expectException(RuntimeException::class);
2851
$this->expectExceptionMessage('Error parsing JSON');
2952
$strategy = $this->createStrategy('manifest-invalid.json');
3053
$strategy->getVersion('main.js');
3154
}
3255

33-
protected function createStrategy($manifestFilename, $strict = false)
56+
public function testStrictExceptionWhenKeyDoesNotExistInManifest()
57+
{
58+
$strategy = $this->createStrategy('manifest-valid.json', true);
59+
60+
$this->expectException(RuntimeException::class);
61+
$this->expectExceptionMessage('Asset "css/other.css" not found in manifest "');
62+
63+
$strategy->getVersion('css/other.css');
64+
}
65+
66+
private function createStrategy($manifestFilename, $strict = false)
3467
{
3568
return new JsonManifestVersionStrategy(__DIR__.'/../fixtures/'.$manifestFilename, $strict);
3669
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/Tests/VersionStrategy/RemoteJsonManifestVersionStrategyTest.php
+27-5Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,52 @@
1111

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

14+
use PHPUnit\Framework\TestCase;
1415
use Symfony\Component\Asset\VersionStrategy\RemoteJsonManifestVersionStrategy;
1516
use Symfony\Component\HttpClient\Exception\JsonException;
1617
use Symfony\Component\HttpClient\MockHttpClient;
1718
use Symfony\Component\HttpClient\Response\MockResponse;
1819

19-
class RemoteJsonManifestVersionStrategyTest extends AbstractJsonManifestVersionStrategyTest
20+
class RemoteJsonManifestVersionStrategyTest extends TestCase
2021
{
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+
2143
public function testMissingManifestFileThrowsException()
2244
{
2345
$this->expectException('RuntimeException');
2446
$this->expectExceptionMessage('HTTP 404 returned for "https://cdn.example.com/non-existent-file.json"');
25-
$strategy = $this->createStrategy('non-existent-file.json');
47+
$strategy = $this->createStrategy('https://cdn.example.com/non-existent-file.json');
2648
$strategy->getVersion('main.js');
2749
}
2850

2951
public function testManifestFileWithBadJSONThrowsException()
3052
{
3153
$this->expectException(JsonException::class);
3254
$this->expectExceptionMessage('Syntax error');
33-
$strategy = $this->createStrategy('manifest-invalid.json');
55+
$strategy = $this->createStrategy('https://cdn.example.com/manifest-invalid.json');
3456
$strategy->getVersion('main.js');
3557
}
3658

37-
protected function createStrategy($manifestUrl, $strict = false)
59+
private function createStrategy($manifestUrl, $strict = false)
3860
{
3961
$httpClient = new MockHttpClient(function ($method, $url, $options) {
4062
$filename = __DIR__.'/../fixtures/'.basename($url);
@@ -46,6 +68,6 @@ protected function createStrategy($manifestUrl, $strict = false)
4668
return new MockResponse('{}', ['http_code' => 404]);
4769
});
4870

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

‎src/Symfony/Component/Asset/VersionStrategy/AbstractJsonManifestVersionStrategy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/VersionStrategy/AbstractJsonManifestVersionStrategy.php
-59Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/VersionStrategy/JsonManifestVersionStrategy.php
+41-8Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,53 @@
2424
*
2525
* You could then ask for the version of "main.js" or "css/styles.css".
2626
*/
27-
class JsonManifestVersionStrategy extends AbstractJsonManifestVersionStrategy
27+
class JsonManifestVersionStrategy implements VersionStrategyInterface
2828
{
29-
protected function getManifestData(string $path): array
29+
private $manifestPath;
30+
private $manifestData;
31+
private $strict;
32+
33+
/**
34+
* @param string $manifestPath Absolute path to the manifest file
35+
* @param bool $strict Throws an exception for unknown paths
36+
*/
37+
public function __construct(string $manifestPath, $strict = false)
3038
{
31-
if (!is_file($path)) {
32-
throw new RuntimeException(sprintf('Asset manifest file "%s" does not exist.', $path));
39+
$this->manifestPath = $manifestPath;
40+
$this->strict = $strict;
41+
}
42+
43+
/**
44+
* With a manifest, we don't really know or care about what
45+
* the version is. Instead, this returns the path to the
46+
* versioned file.
47+
*/
48+
public function getVersion(string $path)
49+
{
50+
return $this->applyVersion($path);
51+
}
52+
53+
public function applyVersion(string $path)
54+
{
55+
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+
}
59+
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());
63+
}
3364
}
3465

35-
$data = json_decode(file_get_contents($path), true);
66+
if (isset($this->manifestData[$path])) {
67+
return $this->manifestData[$path];
68+
}
3669

37-
if (0 < json_last_error()) {
38-
throw new RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": ', $path).json_last_error_msg());
70+
if ($this->strict) {
71+
throw new RuntimeException(sprintf('Asset "%s" not found in manifest "%s".', $path, $this->manifestPath));
3972
}
4073

41-
return $data;
74+
return $path;
4275
}
4376
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Asset/VersionStrategy/RemoteJsonManifestVersionStrategy.php
+35-7Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Asset\VersionStrategy;
1313

14+
use Symfony\Component\Asset\Exception\RuntimeException;
1415
use Symfony\Contracts\HttpClient\HttpClientInterface;
1516

1617
/**
@@ -24,23 +25,50 @@
2425
*
2526
* You could then ask for the version of "main.js" or "css/styles.css".
2627
*/
27-
class RemoteJsonManifestVersionStrategy extends AbstractJsonManifestVersionStrategy
28+
class RemoteJsonManifestVersionStrategy implements VersionStrategyInterface
2829
{
30+
private $manifestData;
31+
private $manifestUrl;
2932
private $httpClient;
33+
private $strict;
3034

3135
/**
3236
* @param string $manifestUrl Absolute URL to the manifest file
37+
* @param bool $strict Throws an exception for unknown paths
3338
*/
34-
public function __construct(string $manifestUrl, HttpClientInterface $httpClient, bool $strict = false)
39+
public function __construct(string $manifestUrl, HttpClientInterface $httpClient, $strict = false)
3540
{
36-
parent::__construct($manifestUrl, $strict);
41+
$this->manifestUrl = $manifestUrl;
3742
$this->httpClient = $httpClient;
43+
$this->strict = $strict;
3844
}
3945

40-
protected function getManifestData(string $path): array
46+
/**
47+
* With a manifest, we don't really know or care about what
48+
* the version is. Instead, this returns the path to the
49+
* versioned file.
50+
*/
51+
public function getVersion(string $path)
52+
{
53+
return $this->applyVersion($path);
54+
}
55+
56+
public function applyVersion(string $path)
4157
{
42-
return $this->httpClient->request('GET', $path, [
43-
'headers' => ['accept' => 'application/json'],
44-
])->toArray();
58+
if (null === $this->manifestData) {
59+
$this->manifestData = $this->httpClient->request('GET', $this->manifestUrl, [
60+
'headers' => ['accept' => 'application/json'],
61+
])->toArray();
62+
}
63+
64+
if (isset($this->manifestData[$path])) {
65+
return $this->manifestData[$path];
66+
}
67+
68+
if ($this->strict) {
69+
throw new RuntimeException(sprintf('Asset "%s" not found in manifest "%s".', $path, $this->manifestUrl));
70+
}
71+
72+
return $path;
4573
}
4674
}

0 commit comments

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