8000 Remove wrong abstraction · symfony/symfony@c75560b · GitHub
[go: up one dir, main page]

Skip to content

Commit c75560b

Browse files
committed
Remove wrong abstraction
1 parent 47e4487 commit c75560b

File tree

3 files changed

+76
-74
lines changed

3 files changed

+76
-74
lines changed

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines 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

Lines 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