1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* BEdita, API-first content management framework |
6
|
|
|
* Copyright 2020 ChannelWeb Srl, Chialab Srl |
7
|
|
|
* |
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
14
|
|
|
*/ |
15
|
|
|
namespace BEdita\WebTools\Utility; |
16
|
|
|
|
17
|
|
|
use BEdita\WebTools\Utility\Asset\AssetStrategyInterface; |
18
|
|
|
use LogicException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Utility class to handle asset names with revisions/signatures. |
22
|
|
|
* |
23
|
|
|
* It can use different strategies to search assets. |
24
|
|
|
*/ |
25
|
|
|
class AssetsRevisions |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The asset strategy adopted. |
29
|
|
|
* |
30
|
|
|
* @var \BEdita\WebTools\Utility\Asset\AssetStrategyInterface|null |
31
|
|
|
*/ |
32
|
|
|
protected static ?AssetStrategyInterface $strategy = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set an asset strategy to be used. |
36
|
|
|
* |
37
|
|
|
* @param \BEdita\WebTools\Utility\Asset\AssetStrategyInterface $strategy The asset strategy to use |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public static function setStrategy(AssetStrategyInterface $strategy): void |
41
|
|
|
{ |
42
|
|
|
static::$strategy = $strategy; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the current asset strategy adopted. |
47
|
|
|
* |
48
|
|
|
* @return \BEdita\WebTools\Utility\Asset\AssetStrategyInterface|null |
49
|
|
|
*/ |
50
|
|
|
public static function getStrategy(): ?AssetStrategyInterface |
51
|
|
|
{ |
52
|
|
|
return static::$strategy; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Clear asset strategy. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public static function clearStrategy(): void |
61
|
|
|
{ |
62
|
|
|
static::$strategy = null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve asset name or an array of assets . |
67
|
|
|
* Return canonical asset name if no assets was found. |
68
|
|
|
* |
69
|
|
|
* @param string $name Canonical asset name |
70
|
|
|
* @param string $extension Optional extension to use to search asset, like 'js' or 'css' |
71
|
|
|
* @return array|string |
72
|
|
|
*/ |
73
|
|
|
public static function get(string $name, ?string $extension = null): string|array |
74
|
|
|
{ |
75
|
|
|
$strategy = static::getStrategy(); |
76
|
|
|
if ($strategy === null) { |
77
|
|
|
return $name; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$asset = $strategy->get($name, $extension); |
81
|
|
|
if (!empty($asset)) { |
82
|
|
|
return $asset; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve asset names array via ::get() call |
90
|
|
|
* |
91
|
|
|
* @param array $names Canonical asset names |
92
|
|
|
* @param string $extension Optional extension to use to search asset, like 'js' or 'css' |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public static function getMulti(array $names, ?string $extension = null): array |
96
|
|
|
{ |
97
|
|
|
$assets = []; |
98
|
|
|
foreach ($names as $val) { |
99
|
|
|
$assets = array_merge($assets, (array)static::get($val, $extension)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $assets; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Load assets for adopted asset strategy. |
107
|
|
|
* |
108
|
|
|
* @param string $path Manifest file path |
109
|
|
|
* @return void |
110
|
|
|
* @throws \LogicException If startegy is not defined |
111
|
|
|
*/ |
112
|
|
|
public static function loadManifest(?string $path = null): void |
113
|
|
|
{ |
114
|
|
|
$strategy = static::getStrategy(); |
115
|
|
|
if ($strategy === null) { |
116
|
|
|
throw new LogicException('Missing asset strategy'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$strategy->loadAssets($path); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|