|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Webrouse\AssetMacro; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Nette\Utils\Json; |
|
8
|
|
|
use Nette\Utils\JsonException; |
|
9
|
|
|
use Webrouse\AssetMacro\Exceptions\AssetNotFoundException; |
|
10
|
|
|
use Webrouse\AssetMacro\Exceptions\ManifestJsonException; |
|
11
|
|
|
use Webrouse\AssetMacro\Exceptions\RevisionNotFound; |
|
12
|
|
|
|
|
13
|
|
|
class Manifest |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Config */ |
|
16
|
|
|
private $config; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string|null */ |
|
19
|
|
|
private $path; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $data = []; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(Config $config, ?string $path, array $data = null) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->config = $config; |
|
28
|
1 |
|
$this->path = $path; |
|
29
|
|
|
|
|
30
|
1 |
|
if ($data) { |
|
31
|
1 |
|
$raw = $data; |
|
32
|
|
|
} else { |
|
33
|
1 |
|
assert(file_exists($path)); |
|
34
|
|
|
try { |
|
35
|
1 |
|
$raw = Json::decode((string) file_get_contents($path), Json::FORCE_ARRAY); |
|
36
|
|
|
} catch (JsonException $e) { |
|
37
|
|
|
throw new ManifestJsonException('Invalid JSON in manifest.', 0, $e); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Strip optional leading / from source and target path (key and value) |
|
42
|
1 |
|
foreach ($raw as $key => $value) { |
|
43
|
1 |
|
$key = ltrim((string) $key, '/'); |
|
44
|
1 |
|
$value = ltrim((string) $value, '/'); |
|
45
|
1 |
|
$this->data[$key] = $value; |
|
46
|
|
|
} |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
1 |
|
public function getAsset(string $path, bool $needed = true): ?Asset |
|
51
|
|
|
{ |
|
52
|
1 |
|
$revision = self::getRevision($path, $needed); |
|
53
|
|
|
|
|
54
|
1 |
|
if (!file_exists($revision->getAbsolutePath())) { |
|
55
|
1 |
|
Utils::throwError( |
|
56
|
1 |
|
new AssetNotFoundException(sprintf("Asset '%s' not found.", $revision->getAbsolutePath())), |
|
57
|
1 |
|
$this->config->getMissingAssetPolicy(), |
|
58
|
1 |
|
$needed |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
1 |
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return new Asset($revision); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
public function getRevision(string $path, bool $needed = true): Revision |
|
69
|
|
|
{ |
|
70
|
|
|
// Strip optional leading / from path |
|
71
|
1 |
|
$path = Utils::normalizePath(ltrim($path, '/')); |
|
72
|
1 |
|
$revision = isset($this->data[$path]) ? $this->data[$path] : null; |
|
73
|
|
|
|
|
74
|
|
|
// Throw error if revision not found in manifest |
|
75
|
1 |
|
if ($revision === null) { |
|
76
|
1 |
|
Utils::throwError( |
|
77
|
1 |
|
new RevisionNotFound(sprintf("Revision for asset '%s' not found in manifest %s.", $path, $this->path)), |
|
78
|
1 |
|
$this->config->getMissingRevisionPolicy(), |
|
79
|
1 |
|
$needed |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return new Revision($this->config, $path, $revision); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|