1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leonidas\Framework\Module\Abstracts\Traits; |
4
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Ui\Asset\InlineScriptCollectionInterface; |
6
|
|
|
use Leonidas\Contracts\Ui\Asset\InlineStyleCollectionInterface; |
7
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptCollectionInterface; |
8
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptLoaderInterface; |
9
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptLocalizationCollectionInterface; |
10
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptPrinterInterface; |
11
|
|
|
use Leonidas\Contracts\Ui\Asset\StyleCollectionInterface; |
12
|
|
|
use Leonidas\Contracts\Ui\Asset\StyleLoaderInterface; |
13
|
|
|
use Leonidas\Contracts\Ui\Asset\StylePrinterInterface; |
14
|
|
|
use Leonidas\Library\Core\Asset\ScriptLoader; |
15
|
|
|
use Leonidas\Library\Core\Asset\ScriptPrinter; |
16
|
|
|
use Leonidas\Library\Core\Asset\StyleLoader; |
17
|
|
|
use Leonidas\Library\Core\Asset\StylePrinter; |
18
|
|
|
|
19
|
|
|
trait ProvisionsAssetsTrait |
20
|
|
|
{ |
21
|
|
|
use AbstractModuleTraitTrait; |
22
|
|
|
use MustBeInitiatedTrait; |
23
|
|
|
|
24
|
|
|
protected ScriptLoaderInterface $scriptLoader; |
25
|
|
|
|
26
|
|
|
protected ScriptPrinterInterface $scriptPrinter; |
27
|
|
|
|
28
|
|
|
protected StyleLoaderInterface $styleLoader; |
29
|
|
|
|
30
|
|
|
protected StylePrinterInterface $stylePrinter; |
31
|
|
|
|
32
|
|
|
protected ?ScriptCollectionInterface $scripts = null; |
33
|
|
|
|
34
|
|
|
protected ?StyleCollectionInterface $styles = null; |
35
|
|
|
|
36
|
|
|
protected ?InlineScriptCollectionInterface $inlineScripts = null; |
37
|
|
|
|
38
|
|
|
protected ?InlineStyleCollectionInterface $inlineStyles = null; |
39
|
|
|
|
40
|
|
|
protected ?ScriptLocalizationCollectionInterface $scriptLocalizations = null; |
41
|
|
|
|
42
|
|
|
protected function getScriptLoader(): ScriptLoaderInterface |
43
|
|
|
{ |
44
|
|
|
return $this->scriptLoader; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function getScriptPrinter(): ScriptPrinterInterface |
48
|
|
|
{ |
49
|
|
|
return $this->scriptPrinter; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getStyleLoader(): StyleLoaderInterface |
53
|
|
|
{ |
54
|
|
|
return $this->styleLoader; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getStylePrinter(): StylePrinterInterface |
58
|
|
|
{ |
59
|
|
|
return $this->stylePrinter; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getScripts(): ?ScriptCollectionInterface |
63
|
|
|
{ |
64
|
|
|
return $this->scripts; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getStyles(): ?StyleCollectionInterface |
68
|
|
|
{ |
69
|
|
|
return $this->styles; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getInlineScripts(): ?InlineScriptCollectionInterface |
73
|
|
|
{ |
74
|
|
|
return $this->inlineScripts; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getInlineStyles(): ?InlineStyleCollectionInterface |
78
|
|
|
{ |
79
|
|
|
return $this->inlineStyles; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getScriptLocalizations(): ?ScriptLocalizationCollectionInterface |
83
|
|
|
{ |
84
|
|
|
return $this->scriptLocalizations; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function initiationContexts(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
'default' => [ |
91
|
|
|
'scripts', |
92
|
|
|
'inlineScripts', |
93
|
|
|
'scriptLocalizations', |
94
|
|
|
'styles', |
95
|
|
|
'inlineStyles', |
96
|
|
|
'scriptLoader', |
97
|
|
|
'scriptPrinter', |
98
|
|
|
'styleLoader', |
99
|
|
|
'stylePrinter', |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function hasScripts(): bool |
105
|
|
|
{ |
106
|
|
|
return ($scripts = $this->getScripts()) |
107
|
|
|
&& !empty($scripts->getScripts()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function hasStyles(): bool |
111
|
|
|
{ |
112
|
|
|
return ($styles = $this->getStyles()) |
113
|
|
|
&& !empty($styles->getStyles()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function hasInlineScripts(): bool |
117
|
|
|
{ |
118
|
|
|
return ($inlineScripts = $this->getInlineScripts()) |
119
|
|
|
&& !empty($inlineScripts->getScripts()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function hasInlineStyles(): bool |
123
|
|
|
{ |
124
|
|
|
return ($inlineStyles = $this->getInlineStyles()) |
125
|
|
|
&& !empty($inlineStyles->getStyles()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function hasScriptLocalizations(): bool |
129
|
|
|
{ |
130
|
|
|
return ($scriptLocalizations = $this->getScriptLocalizations()) |
131
|
|
|
&& !empty($scriptLocalizations->getLocalizations()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function provisionAssets(?string $hookSuffix = null): void |
135
|
|
|
{ |
136
|
|
|
$this->init('default'); |
137
|
|
|
|
138
|
|
|
$request = $this->getServerRequest(); |
139
|
|
|
|
140
|
|
|
$scriptLoader = $this->getScriptLoader(); |
141
|
|
|
$styleLoader = $this->getStyleLoader(); |
142
|
|
|
|
143
|
|
|
if ($hookSuffix) { |
144
|
|
|
$request = $request->withAttribute('hook_suffix', $hookSuffix); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($this->hasScripts()) { |
148
|
|
|
$scriptLoader->load($this->getScripts(), $request); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($this->hasStyles()) { |
152
|
|
|
$styleLoader->load($this->getStyles(), $request); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($this->hasInlineScripts()) { |
156
|
|
|
$scriptLoader->support($this->getInlineScripts(), $request); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($this->hasInlineStyles()) { |
160
|
|
|
$styleLoader->support($this->getInlineStyles(), $request); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($this->hasScriptLocalizations()) { |
164
|
|
|
$scriptLoader->localize($this->getScriptLocalizations(), $request); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$scriptLoader->activate(...$this->activateScripts()); |
168
|
|
|
$scriptLoader->deactivate(...$this->deactivateScripts()); |
169
|
|
|
|
170
|
|
|
$styleLoader->activate(...$this->activateStyles()); |
171
|
|
|
$styleLoader->deactivate(...$this->deactivateStyles()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function filterScriptLoaderTag(string $tag, string $handle, string $src): string |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
if (!$this->isInitiated() || !$this->hasScripts() || !$this->getScripts()->hasScript($handle)) { |
177
|
|
|
return $tag; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->getScriptPrinter() |
181
|
|
|
->merge($tag, $this->getScripts()->getScript($handle)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function filterStyleLoaderTag(string $tag, string $handle, string $href, string $media): string |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
if (!$this->isInitiated() || !$this->hasStyles() || !$this->getStyles()->hasStyle($handle)) { |
187
|
|
|
return $tag; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->getStylePrinter() |
191
|
|
|
->merge($tag, $this->getStyles()->getStyle($handle)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function asset(?string $asset = null): string |
195
|
|
|
{ |
196
|
|
|
return $this->extension->url( |
197
|
|
|
$this->configCascade($this->assetsConfigCascade()) . $asset |
|
|
|
|
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function assetsConfigCascade(): array |
202
|
|
|
{ |
203
|
|
|
return [ |
204
|
|
|
'view.assets.path', 'view.assets', 'theme.assets', |
205
|
|
|
]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function version(?string $version = null): string |
209
|
|
|
{ |
210
|
|
|
if ($this->extension->isInDev()) { |
211
|
|
|
return (string) time(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $version ?: $this->extension->getVersion(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
protected function scriptLoader(): ScriptLoaderInterface |
218
|
|
|
{ |
219
|
|
|
return new ScriptLoader(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function scriptPrinter(): ScriptPrinterInterface |
223
|
|
|
{ |
224
|
|
|
return new ScriptPrinter(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
protected function styleLoader(): StyleLoaderInterface |
228
|
|
|
{ |
229
|
|
|
return new StyleLoader(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected function stylePrinter(): StylePrinterInterface |
233
|
|
|
{ |
234
|
|
|
return new StylePrinter(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
protected function scripts(): ?ScriptCollectionInterface |
238
|
|
|
{ |
239
|
|
|
return null; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
protected function styles(): ?StyleCollectionInterface |
243
|
|
|
{ |
244
|
|
|
return null; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
protected function inlineScripts(): ?InlineScriptCollectionInterface |
248
|
|
|
{ |
249
|
|
|
return null; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
protected function inlineStyles(): ?InlineStyleCollectionInterface |
253
|
|
|
{ |
254
|
|
|
return null; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
protected function scriptLocalizations(): ?ScriptLocalizationCollectionInterface |
258
|
|
|
{ |
259
|
|
|
return null; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
protected function activateScripts(): array |
263
|
|
|
{ |
264
|
|
|
return []; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
protected function deactivateScripts(): array |
268
|
|
|
{ |
269
|
|
|
return []; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
protected function activateStyles(): array |
273
|
|
|
{ |
274
|
|
|
return []; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
protected function deactivateStyles(): array |
278
|
|
|
{ |
279
|
|
|
return []; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.