|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Assets; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Assets\Factory\Minifier as MinifierFactory; |
|
8
|
|
|
use League\Flysystem\FileNotFoundException; |
|
9
|
|
|
use MatthiasMullie\Minify\CSS as CssMinifier; |
|
10
|
|
|
use MatthiasMullie\Minify\JS as JsMinifier; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
14
|
|
|
*/ |
|
15
|
|
|
class AssetManager |
|
16
|
|
|
{ |
|
17
|
|
|
const EXT_CSS = '.css'; |
|
18
|
|
|
const EXT_JS = '.js'; |
|
19
|
|
|
|
|
20
|
|
|
const ERROR_EMPTY_GROUP_NAME = 'Group name must not be empty.'; |
|
21
|
|
|
|
|
22
|
|
|
/** @var MinifierFactory */ |
|
23
|
|
|
protected $minifierFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** @var FileFinder */ |
|
26
|
|
|
protected $fileFinder; |
|
27
|
|
|
|
|
28
|
|
|
/** @var CacheManager */ |
|
29
|
|
|
protected $cacheManager; |
|
30
|
|
|
|
|
31
|
|
|
/** @var JsMinifier[] */ |
|
32
|
|
|
protected $jsMinifiers = []; |
|
33
|
|
|
|
|
34
|
|
|
/** @var CssMinifier[] */ |
|
35
|
|
|
protected $cssMinifiers = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* AssetManager constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param MinifierFactory $minifierFactory |
|
41
|
|
|
* @param FileFinder $fileFinder |
|
42
|
|
|
* @param CacheManager $cacheManager |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(MinifierFactory $minifierFactory, FileFinder $fileFinder, CacheManager $cacheManager) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->minifierFactory = $minifierFactory; |
|
47
|
|
|
$this->fileFinder = $fileFinder; |
|
48
|
|
|
$this->cacheManager = $cacheManager; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $groupName |
|
53
|
|
|
* @param string $rawPath |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function addCss(string $groupName, string $rawPath) |
|
58
|
|
|
{ |
|
59
|
|
|
$fixedPath = $rawPath; |
|
60
|
|
|
if ($this->getExtension($rawPath) !== static:: EXT_CSS) { |
|
61
|
|
|
$fixedPath = $rawPath . static::EXT_CSS; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$content = $this->fileFinder->read($fixedPath, $groupName); |
|
65
|
|
|
|
|
66
|
|
|
$this->getCssMinifier($groupName)->add($content); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $groupName |
|
71
|
|
|
* @param string $rawPath |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function addJs(string $groupName, string $rawPath) |
|
76
|
|
|
{ |
|
77
|
|
|
$fixedPath = $rawPath; |
|
78
|
|
|
if ($this->getExtension($rawPath) !== static:: EXT_JS) { |
|
79
|
|
|
$fixedPath = $rawPath . static::EXT_JS; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$content = $this->fileFinder->read($fixedPath, $groupName); |
|
83
|
|
|
|
|
84
|
|
|
$this->getJsMinifier($groupName)->add($content); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $path |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getExtension(string $path): string |
|
93
|
|
|
{ |
|
94
|
|
|
$dotrpos = strrpos($path, '.'); |
|
95
|
|
|
|
|
96
|
|
|
if ($dotrpos === false) { |
|
97
|
|
|
return ''; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return substr($path, $dotrpos); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $groupName |
|
105
|
|
|
* @param string $content |
|
106
|
|
|
*/ |
|
107
|
|
|
public function addCssContent(string $groupName, string $content) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->getCssMinifier($groupName)->add($content); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $groupName |
|
114
|
|
|
* @param string $content |
|
115
|
|
|
*/ |
|
116
|
|
|
public function addJsContent(string $groupName, string $content) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->getJsMinifier($groupName)->add($content); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $groupName |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
* @throws \League\Flysystem\FileExistsException |
|
126
|
|
|
*/ |
|
127
|
|
|
public function renderCss(string $groupName): string |
|
128
|
|
|
{ |
|
129
|
|
|
$content = $this->getCssMinifier($groupName)->minify(); |
|
130
|
|
|
$cachePath = $groupName . static::EXT_CSS; |
|
131
|
|
|
|
|
132
|
|
|
$this->cacheManager->write($cachePath, $content); |
|
133
|
|
|
|
|
134
|
|
|
return $content; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $groupName |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
* @throws \League\Flysystem\FileExistsException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function renderJs(string $groupName): string |
|
144
|
|
|
{ |
|
145
|
|
|
$content = $this->getJsMinifier($groupName)->minify(); |
|
146
|
|
|
$cachePath = $groupName . static::EXT_JS; |
|
147
|
|
|
|
|
148
|
|
|
$this->cacheManager->write($cachePath, $content); |
|
149
|
|
|
|
|
150
|
|
|
return $content; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $cachePath |
|
155
|
|
|
* |
|
156
|
|
|
* @return string|null |
|
157
|
|
|
* @throws \League\Flysystem\FileExistsException |
|
158
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
159
|
|
|
*/ |
|
160
|
|
|
public function renderRaw(string $cachePath): ?string |
|
161
|
|
|
{ |
|
162
|
|
|
$content = $this->fileFinder->read($cachePath); |
|
163
|
|
|
if (null === $content) { |
|
164
|
|
|
return null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($content === '' && !$this->fileFinder->has($cachePath)) { |
|
168
|
|
|
return null; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$this->cacheManager->write($cachePath, $content); |
|
172
|
|
|
|
|
173
|
|
|
return $content; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param string $groupName |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
* @throws \League\Flysystem\FileExistsException |
|
181
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
182
|
|
|
*/ |
|
183
|
|
|
public function ensureCssWebPath(string $groupName): string |
|
184
|
|
|
{ |
|
185
|
|
|
$cachePath = $groupName . static::EXT_CSS; |
|
186
|
|
|
|
|
187
|
|
|
if (!$this->cacheManager->has($cachePath)) { |
|
188
|
|
|
$this->renderCss($groupName); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this->cacheManager->getWebPath($cachePath); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $groupName |
|
196
|
|
|
* |
|
197
|
|
|
* @return string |
|
198
|
|
|
* @throws \League\Flysystem\FileExistsException |
|
199
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
200
|
|
|
*/ |
|
201
|
|
|
public function ensureJsWebPath(string $groupName): string |
|
202
|
|
|
{ |
|
203
|
|
|
$cachePath = $groupName . static::EXT_JS; |
|
204
|
|
|
|
|
205
|
|
|
if (!$this->cacheManager->has($cachePath)) { |
|
206
|
|
|
$this->renderJs($groupName); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $this->cacheManager->getWebPath($cachePath); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param string $groupName |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
* @throws \League\Flysystem\FileExistsException |
|
217
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
218
|
|
|
*/ |
|
219
|
|
|
public function ensureImgWebPath(string $cachePath): string |
|
220
|
|
|
{ |
|
221
|
|
|
if (!$this->cacheManager->has($cachePath)) { |
|
222
|
|
|
if ($this->renderRaw($cachePath) === null) { |
|
223
|
|
|
throw new FileNotFoundException($cachePath); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $this->cacheManager->getWebPath($cachePath); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param string $key |
|
232
|
|
|
* |
|
233
|
|
|
* @return CssMinifier |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function getCssMinifier(string $key): CssMinifier |
|
236
|
|
|
{ |
|
237
|
|
|
if (!array_key_exists($key, $this->cssMinifiers)) { |
|
238
|
|
|
$this->cssMinifiers[$key] = $this->minifierFactory->createCssMinifier(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $this->cssMinifiers[$key]; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param string $key |
|
246
|
|
|
* |
|
247
|
|
|
* @return JsMinifier |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function getJsMinifier(string $key): JsMinifier |
|
250
|
|
|
{ |
|
251
|
|
|
if (!array_key_exists($key, $this->jsMinifiers)) { |
|
252
|
|
|
$this->jsMinifiers[$key] = $this->minifierFactory->createJsMinifier(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return $this->jsMinifiers[$key]; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|