1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2020 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace BEdita\WebTools\Utility; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Utility class to handle asset names with revisions/signatures. |
18
|
|
|
* |
19
|
|
|
* Rev manifest file default path is `config/rev-manifest.json` |
20
|
|
|
* Other file paths may be used via `loadManifest()` |
21
|
|
|
*/ |
22
|
|
|
class AssetsRevisions |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Array having asset names as keys and revved asset names as values |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $assets = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Load revision manifest JSON. |
33
|
|
|
* |
34
|
|
|
* @param array $path Manifest file path |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public static function loadManifest(string $path = null): void |
38
|
|
|
{ |
39
|
|
|
static::$assets = []; |
40
|
|
|
if (empty($path)) { |
41
|
|
|
$path = CONFIG . 'rev-manifest.json'; |
42
|
|
|
} |
43
|
|
|
if (file_exists($path)) { |
44
|
|
|
static::$assets = (array)json_decode(file_get_contents($path), true); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Retrieve `revved` asset name if found in manifest or return canonical asset name otherwise |
50
|
|
|
* |
51
|
|
|
* @param string $name Canonical asset name (un-revved) |
52
|
|
|
* @param string $extension Optional extension to use to search asset, like '.js' or '.css' |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public static function get(string $name, string $extension = null): string |
56
|
|
|
{ |
57
|
|
|
if (static::$assets === null) { |
|
|
|
|
58
|
|
|
static::loadManifest(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!empty(static::$assets[$name])) { |
62
|
|
|
return (string)static::$assets[$name]; |
63
|
|
|
} |
64
|
|
|
if (!empty($extension) && !empty(static::$assets[$name . $extension])) { |
65
|
|
|
return (string)static::$assets[$name . $extension]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $name; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieve `revved` asset names array via ::get() call |
73
|
|
|
* |
74
|
|
|
* @param array $names Canonical asset names (un-revved) |
75
|
|
|
* @param string $extension Optional extension to use to search asset, like '.js' or '.css' |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public static function getMulti(array $names, string $extension = null): array |
79
|
|
|
{ |
80
|
|
|
foreach ($names as $k => $val) { |
81
|
|
|
$names[$k] = static::get($val, $extension); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $names; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|