-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
JsonManifestVersionStrategy.php
132 lines (113 loc) · 5.06 KB
/
JsonManifestVersionStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Asset\VersionStrategy;
use Symfony\Component\Asset\Exception\AssetNotFoundException;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Asset\Exception\RuntimeException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Reads the versioned path of an asset from a JSON manifest file.
*
* For example, the manifest file might look like this:
* {
* "main.js": "main.abc123.js",
* "css/styles.css": "css/styles.555abc.css"
* }
*
* You could then ask for the version of "main.js" or "css/styles.css".
*/
class JsonManifestVersionStrategy implements VersionStrategyInterface
{
private $manifestPath;
private $manifestData;
private $httpClient;
private $strictMode;
/**
* @param string $manifestPath Absolute path to the manifest file
* @param bool $strictMode Throws an exception for unknown paths
*/
public function __construct(string $manifestPath, ?HttpClientInterface $httpClient = null, $strictMode = false)
{
$this->manifestPath = $manifestPath;
$this->httpClient = $httpClient;
$this->strictMode = $strictMode;
if (null === $this->httpClient && ($scheme = parse_url($this->manifestPath, \PHP_URL_SCHEME)) && 0 === strpos($scheme, 'http')) {
throw new LogicException(sprintf('The "%s" class needs an HTTP client to use a remote manifest. Try running "composer require symfony/http-client".', self::class));
}
}
/**
* With a manifest, we don't really know or care about what
* the version is. Instead, this returns the path to the
* versioned file.
*/
public function getVersion(string $path)
{
return $this->applyVersion($path);
}
public function applyVersion(string $path)
{
return $this->getManifestPath($path) ?: $path;
}
private function getManifestPath(string $path): ?string
{
if (null === $this->manifestData) {
if (null !== $this->httpClient && ($scheme = parse_url($this->manifestPath, \PHP_URL_SCHEME)) && 0 === strpos($scheme, 'http')) {
try {
$this->manifestData = $this->httpClient->request('GET', $this->manifestPath, [
'headers' => ['accept' => 'application/json'],
])->toArray();
} catch (DecodingExceptionInterface $e) {
throw new RuntimeException(sprintf('Error parsing JSON from asset manifest URL "%s".', $this->manifestPath), 0, $e);
} catch (ClientExceptionInterface $e) {
throw new RuntimeException(sprintf('Error loading JSON from asset manifest URL "%s".', $this->manifestPath), 0, $e);
}
} else {
if (!is_file($this->manifestPath)) {
throw new RuntimeException(sprintf('Asset manifest file "%s" does not exist. Did you forget to build the assets with npm or yarn?', $this->manifestPath));
}
$this->manifestData = json_decode(file_get_contents($this->manifestPath), true);
if (0 < json_last_error()) {
throw new RuntimeException(sprintf('Error parsing JSON from asset manifest file "%s": ', $this->manifestPath).json_last_error_msg());
}
}
}
if (isset($this->manifestData[$path])) {
return $this->manifestData[$path];
}
if ($this->strictMode) {
$message = sprintf('Asset "%s" not found in manifest "%s".', $path, $this->manifestPath);
$alternatives = $this->findAlternatives($path, $this->manifestData);
if (\count($alternatives) > 0) {
$message .= sprintf(' Did you mean one of these? "%s".', implode('", "', $alternatives));
}
throw new AssetNotFoundException($message, $alternatives);
}
return null;
}
private function findAlternatives(string $path, array $manifestData): array
{
$path = strtolower($path);
$alternatives = [];
foreach ($manifestData as $key => $value) {
$lev = levenshtein($path, strtolower($key));
if ($lev <= \strlen($path) / 3 || false !== stripos($key, $path)) {
$alternatives[$key] = isset($alternatives[$key]) ? min($lev, $alternatives[$key]) : $lev;
}
$lev = levenshtein($path, strtolower($value));
if ($lev <= \strlen($path) / 3 || false !== stripos($key, $path)) {
$alternatives[$key] = isset($alternatives[$key]) ? min($lev, $alternatives[$key]) : $lev;
}
}
asort($alternatives);
return array_keys($alternatives);
}
}