1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\View\State; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
use Yiisoft\Html\Tag\Link; |
11
|
|
|
use Yiisoft\Html\Tag\Meta; |
12
|
|
|
use Yiisoft\Html\Tag\Script; |
13
|
|
|
use Yiisoft\Html\Tag\Style; |
14
|
|
|
use Yiisoft\Json\Json; |
15
|
|
|
use Yiisoft\View\WebView; |
16
|
|
|
|
17
|
|
|
use function array_key_exists; |
18
|
|
|
use function get_class; |
19
|
|
|
use function gettype; |
20
|
|
|
use function in_array; |
21
|
|
|
use function is_array; |
22
|
|
|
use function is_object; |
23
|
|
|
use function is_string; |
24
|
|
|
|
25
|
|
|
final class WebViewState |
26
|
|
|
{ |
27
|
|
|
use StateTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string The page title |
31
|
|
|
*/ |
32
|
|
|
private string $title = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Meta[] The registered meta tags. |
36
|
|
|
* |
37
|
|
|
* @see registerMeta() |
38
|
|
|
* @see registerMetaTag() |
39
|
|
|
*/ |
40
|
|
|
private array $metaTags = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array The registered link tags. |
44
|
|
|
* @psalm-var array<int, Link[]> |
45
|
|
|
* |
46
|
|
|
* @see registerLink() |
47
|
|
|
* @see registerLinkTag() |
48
|
|
|
*/ |
49
|
|
|
private array $linkTags = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array The registered CSS code blocks. |
53
|
|
|
* @psalm-var array<int, string[]|Style[]> |
54
|
|
|
* |
55
|
|
|
* {@see registerCss()} |
56
|
|
|
*/ |
57
|
|
|
private array $css = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array The registered CSS files. |
61
|
|
|
* @psalm-var array<int, string[]> |
62
|
|
|
* |
63
|
|
|
* {@see registerCssFile()} |
64
|
|
|
*/ |
65
|
|
|
private array $cssFiles = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array The registered JS code blocks |
69
|
|
|
* @psalm-var array<int, string[]|Script[]> |
70
|
|
|
* |
71
|
|
|
* {@see registerJs()} |
72
|
|
|
*/ |
73
|
|
|
private array $js = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array The registered JS files. |
77
|
|
|
* @psalm-var array<int, string[]> |
78
|
|
|
* |
79
|
|
|
* {@see registerJsFile()} |
80
|
|
|
*/ |
81
|
|
|
private array $jsFiles = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get title in views. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
2 |
|
public function getTitle(): string |
89
|
|
|
{ |
90
|
2 |
|
return $this->title; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Meta[] The registered meta tags. |
95
|
|
|
*/ |
96
|
47 |
|
public function getMetaTags(): array |
97
|
|
|
{ |
98
|
47 |
|
return $this->metaTags; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array The registered link tags. |
103
|
|
|
* @psalm-return array<int, Link[]> |
104
|
|
|
*/ |
105
|
47 |
|
public function getLinkTags(): array |
106
|
|
|
{ |
107
|
47 |
|
return $this->linkTags; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array The registered CSS code blocks. |
112
|
|
|
* @psalm-return array<int, string[]|Style[]> |
113
|
|
|
*/ |
114
|
47 |
|
public function getCss(): array |
115
|
|
|
{ |
116
|
47 |
|
return $this->css; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array The registered CSS files. |
121
|
|
|
* @psalm-return array<int, string[]> |
122
|
|
|
*/ |
123
|
47 |
|
public function getCssFiles(): array |
124
|
|
|
{ |
125
|
47 |
|
return $this->cssFiles; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array The registered JS code blocks |
130
|
|
|
* @psalm-return array<int, string[]|Script[]> |
131
|
|
|
*/ |
132
|
47 |
|
public function getJs(): array |
133
|
|
|
{ |
134
|
47 |
|
return $this->js; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array The registered JS files. |
139
|
|
|
* @psalm-return array<int, string[]> |
140
|
|
|
*/ |
141
|
47 |
|
public function getJsFiles(): array |
142
|
|
|
{ |
143
|
47 |
|
return $this->jsFiles; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set title in views. |
148
|
|
|
* |
149
|
|
|
* {@see getTitle()} |
150
|
|
|
* |
151
|
|
|
* @param string $value |
152
|
|
|
* |
153
|
|
|
* @return static |
154
|
|
|
*/ |
155
|
3 |
|
public function setTitle(string $value): self |
156
|
|
|
{ |
157
|
3 |
|
$this->title = $value; |
158
|
3 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Registers a meta tag. |
163
|
|
|
* |
164
|
|
|
* For example, a description meta tag can be added like the following: |
165
|
|
|
* |
166
|
|
|
* ```php |
167
|
|
|
* $state->registerMeta([ |
168
|
|
|
* 'name' => 'description', |
169
|
|
|
* 'content' => 'This website is about funny raccoons.' |
170
|
|
|
* ]); |
171
|
|
|
* ``` |
172
|
|
|
* |
173
|
|
|
* @param array $attributes The HTML attributes for the meta tag. |
174
|
|
|
* @param string|null $key The key that identifies the meta tag. If two meta tags are registered with the same key, |
175
|
|
|
* the latter will overwrite the former. If this is null, the new meta tag will be appended to the existing ones. |
176
|
|
|
*/ |
177
|
1 |
|
public function registerMeta(array $attributes, ?string $key = null): void |
178
|
|
|
{ |
179
|
1 |
|
$this->registerMetaTag(Html::meta($attributes), $key); |
180
|
1 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Registers a {@see Meta} tag. |
184
|
|
|
* |
185
|
|
|
* @see registerMeta() |
186
|
|
|
*/ |
187
|
4 |
|
public function registerMetaTag(Meta $meta, ?string $key = null): void |
188
|
|
|
{ |
189
|
4 |
|
$key === null |
190
|
3 |
|
? $this->metaTags[] = $meta |
191
|
1 |
|
: $this->metaTags[$key] = $meta; |
192
|
4 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Registers a link tag. |
196
|
|
|
* |
197
|
|
|
* For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) can be added like the |
198
|
|
|
* following: |
199
|
|
|
* |
200
|
|
|
* ```php |
201
|
|
|
* $view->registerLink(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
202
|
|
|
* ``` |
203
|
|
|
* |
204
|
|
|
* **Note:** To register link tags for CSS stylesheets, use {@see registerCssFile()]} instead, which has more |
205
|
|
|
* options for this kind of link tag. |
206
|
|
|
* |
207
|
|
|
* @param array $attributes The HTML attributes for the link tag. |
208
|
|
|
* @param int $position The position at which the link tag should be inserted in a page. |
209
|
|
|
* @param string|null $key The key that identifies the link tag. If two link tags are registered with the same key, |
210
|
|
|
* the latter will overwrite the former. If this is null, the new link tag will be appended to the existing ones. |
211
|
|
|
*/ |
212
|
3 |
|
public function registerLink(array $attributes, int $position = WebView::POSITION_HEAD, ?string $key = null): void |
213
|
|
|
{ |
214
|
3 |
|
$this->registerLinkTag(Html::link()->attributes($attributes), $position, $key); |
215
|
3 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Registers a {@see Link} tag. |
219
|
|
|
* |
220
|
|
|
* @see registerLink() |
221
|
|
|
*/ |
222
|
8 |
|
public function registerLinkTag(Link $link, int $position = WebView::POSITION_HEAD, ?string $key = null): void |
223
|
|
|
{ |
224
|
8 |
|
$key === null |
225
|
7 |
|
? $this->linkTags[$position][] = $link |
226
|
1 |
|
: $this->linkTags[$position][$key] = $link; |
227
|
8 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Registers a CSS code block. |
231
|
|
|
* |
232
|
|
|
* @param string $css The content of the CSS code block to be registered. |
233
|
|
|
* @param string|null $key The key that identifies the CSS code block. If null, it will use $css as the key. |
234
|
|
|
* If two CSS code blocks are registered with the same key, the latter will overwrite the former. |
235
|
|
|
* @param array $attributes The HTML attributes for the {@see Style} tag. |
236
|
|
|
*/ |
237
|
7 |
|
public function registerCss( |
238
|
|
|
string $css, |
239
|
|
|
int $position = WebView::POSITION_HEAD, |
240
|
|
|
array $attributes = [], |
241
|
|
|
?string $key = null |
242
|
|
|
): void { |
243
|
7 |
|
$key = $key ?: md5($css); |
244
|
7 |
|
$this->css[$position][$key] = $attributes === [] ? $css : Html::style($css, $attributes); |
245
|
7 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Registers a CSS code block from file. |
249
|
|
|
* |
250
|
|
|
* @param string $path The path or URL to CSS file. |
251
|
|
|
* |
252
|
|
|
* @see registerCss() |
253
|
|
|
*/ |
254
|
2 |
|
public function registerCssFromFile( |
255
|
|
|
string $path, |
256
|
|
|
int $position = WebView::POSITION_HEAD, |
257
|
|
|
array $attributes = [], |
258
|
|
|
?string $key = null |
259
|
|
|
): void { |
260
|
2 |
|
$css = file_get_contents($path); |
261
|
2 |
|
if ($css === false) { |
262
|
1 |
|
throw new RuntimeException(sprintf('File %s could not be read.', $path)); |
263
|
|
|
} |
264
|
|
|
|
265
|
1 |
|
$this->registerCss($css, $position, $attributes, $key); |
266
|
1 |
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Register a {@see Style} tag. |
270
|
|
|
* |
271
|
|
|
* @see registerJs() |
272
|
|
|
*/ |
273
|
2 |
|
public function registerStyleTag(Style $style, int $position = WebView::POSITION_HEAD, ?string $key = null): void |
274
|
|
|
{ |
275
|
2 |
|
$key = $key ?: md5($style->render()); |
276
|
2 |
|
$this->css[$position][$key] = $style; |
277
|
2 |
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Registers a CSS file. |
281
|
|
|
* |
282
|
|
|
* This method should be used for simple registration of CSS files. If you want to use features of |
283
|
|
|
* {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use |
284
|
|
|
* {@see \Yiisoft\Assets\AssetBundle}. |
285
|
|
|
* |
286
|
|
|
* @param string $url The CSS file to be registered. |
287
|
|
|
* @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} |
288
|
|
|
* for the supported options. |
289
|
|
|
* @param string|null $key The key that identifies the CSS script file. If null, it will use $url as the key. |
290
|
|
|
* If two CSS files are registered with the same key, the latter will overwrite the former. |
291
|
|
|
*/ |
292
|
13 |
|
public function registerCssFile( |
293
|
|
|
string $url, |
294
|
|
|
int $position = WebView::POSITION_HEAD, |
295
|
|
|
array $options = [], |
296
|
|
|
string $key = null |
297
|
|
|
): void { |
298
|
13 |
|
if (!$this->isValidCssPosition($position)) { |
299
|
2 |
|
throw new InvalidArgumentException('Invalid position of CSS file.'); |
300
|
|
|
} |
301
|
|
|
|
302
|
11 |
|
$this->cssFiles[$position][$key ?: $url] = Html::cssFile($url, $options)->render(); |
303
|
11 |
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* It processes the CSS configuration generated by the asset manager and converts it into HTML code. |
307
|
|
|
* |
308
|
|
|
* @param array $cssFiles |
309
|
|
|
*/ |
310
|
4 |
|
public function addCssFiles(array $cssFiles): void |
311
|
|
|
{ |
312
|
|
|
/** @var mixed $value */ |
313
|
4 |
|
foreach ($cssFiles as $key => $value) { |
314
|
4 |
|
$this->registerCssFileByConfig( |
315
|
4 |
|
is_string($key) ? $key : null, |
316
|
4 |
|
is_array($value) ? $value : [$value], |
317
|
|
|
); |
318
|
|
|
} |
319
|
1 |
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* It processes the CSS strings generated by the asset manager. |
323
|
|
|
* |
324
|
|
|
* @param array $cssStrings |
325
|
|
|
*/ |
326
|
7 |
|
public function addCssStrings(array $cssStrings): void |
327
|
|
|
{ |
328
|
|
|
/** @var mixed $value */ |
329
|
7 |
|
foreach ($cssStrings as $key => $value) { |
330
|
7 |
|
$this->registerCssStringByConfig( |
331
|
7 |
|
is_string($key) ? $key : null, |
332
|
7 |
|
is_array($value) ? $value : [$value, WebView::POSITION_HEAD], |
333
|
|
|
); |
334
|
|
|
} |
335
|
1 |
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Registers a JS code block. |
339
|
|
|
* |
340
|
|
|
* @param string $js the JS code block to be registered |
341
|
|
|
* @param int $position the position at which the JS script tag should be inserted in a page. |
342
|
|
|
* |
343
|
|
|
* The possible values are: |
344
|
|
|
* |
345
|
|
|
* - {@see WebView::POSITION_HEAD}: in the head section |
346
|
|
|
* - {@see WebView::POSITION_BEGIN}: at the beginning of the body section |
347
|
|
|
* - {@see WebView::POSITION_END}: at the end of the body section. This is the default value. |
348
|
|
|
* - {@see WebView::POSITION_LOAD}: executed when HTML page is completely loaded. |
349
|
|
|
* - {@see WebView::POSITION_READY}: executed when HTML document composition is ready. |
350
|
|
|
* @param string|null $key The key that identifies the JS code block. If null, it will use $js as the key. |
351
|
|
|
* If two JS code blocks are registered with the same key, the latter will overwrite the former. |
352
|
|
|
*/ |
353
|
7 |
|
public function registerJs(string $js, int $position = WebView::POSITION_END, ?string $key = null): void |
354
|
|
|
{ |
355
|
7 |
|
$key = $key ?: md5($js); |
356
|
7 |
|
$this->js[$position][$key] = $js; |
357
|
7 |
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Register a `script` tag |
361
|
|
|
* |
362
|
|
|
* @see registerJs() |
363
|
|
|
*/ |
364
|
4 |
|
public function registerScriptTag(Script $script, int $position = WebView::POSITION_END, ?string $key = null): void |
365
|
|
|
{ |
366
|
4 |
|
$key = $key ?: md5($script->render()); |
367
|
4 |
|
$this->js[$position][$key] = $script; |
368
|
4 |
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Registers a JS file. |
372
|
|
|
* |
373
|
|
|
* This method should be used for simple registration of JS files. If you want to use features of |
374
|
|
|
* {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use |
375
|
|
|
* {@see \Yiisoft\Assets\AssetBundle}. |
376
|
|
|
* |
377
|
|
|
* @param string $url The JS file to be registered. |
378
|
|
|
* @param array $options The HTML attributes for the script tag. The following options are specially handled and |
379
|
|
|
* are not treated as HTML attributes: |
380
|
|
|
* |
381
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
382
|
|
|
* * {@see WebView::POSITION_HEAD}: in the head section |
383
|
|
|
* * {@see WebView::POSITION_BEGIN}: at the beginning of the body section |
384
|
|
|
* * {@see WebView::POSITION_END}: at the end of the body section. This is the default value. |
385
|
|
|
* |
386
|
|
|
* Please refer to {@see \Yiisoft\Html\Html::javaScriptFile()} for other supported options. |
387
|
|
|
* @param string|null $key The key that identifies the JS script file. If null, it will use $url as the key. |
388
|
|
|
* If two JS files are registered with the same key at the same position, the latter will overwrite the former. |
389
|
|
|
* Note that position option takes precedence, thus files registered with the same key, but different |
390
|
|
|
* position option will not override each other. |
391
|
|
|
*/ |
392
|
13 |
|
public function registerJsFile( |
393
|
|
|
string $url, |
394
|
|
|
int $position = WebView::POSITION_END, |
395
|
|
|
array $options = [], |
396
|
|
|
string $key = null |
397
|
|
|
): void { |
398
|
13 |
|
if (!$this->isValidJsPosition($position)) { |
399
|
2 |
|
throw new InvalidArgumentException('Invalid position of JS file.'); |
400
|
|
|
} |
401
|
|
|
|
402
|
11 |
|
$this->jsFiles[$position][$key ?: $url] = Html::javaScriptFile($url, $options)->render(); |
403
|
11 |
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated |
407
|
|
|
* variable names. |
408
|
|
|
* |
409
|
|
|
* @param string $name Name of the variable |
410
|
|
|
* @param mixed $value Value of the variable |
411
|
|
|
* @param int $position The position in a page at which the JavaScript variable should be inserted. |
412
|
|
|
* |
413
|
|
|
* The possible values are: |
414
|
|
|
* |
415
|
|
|
* - {@see WebView::POSITION_HEAD}: in the head section. This is the default value. |
416
|
|
|
* - {@see WebView::POSITION_BEGIN}: at the beginning of the body section. |
417
|
|
|
* - {@see WebView::POSITION_END}: at the end of the body section. |
418
|
|
|
* - {@see WebView::POSITION_LOAD}: enclosed within jQuery(window).load(). |
419
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
420
|
|
|
* - {@see POSITION_READY}: enclosed within jQuery(document).ready(). |
421
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
422
|
|
|
*/ |
423
|
3 |
|
public function registerJsVar(string $name, $value, int $position = WebView::POSITION_HEAD): void |
424
|
|
|
{ |
425
|
3 |
|
$js = sprintf('var %s = %s;', $name, Json::htmlEncode($value)); |
426
|
3 |
|
$this->registerJs($js, $position, $name); |
427
|
3 |
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* It processes the JS configuration generated by the asset manager and converts it into HTML code. |
431
|
|
|
* |
432
|
|
|
* @param array $jsFiles |
433
|
|
|
*/ |
434
|
4 |
|
public function addJsFiles(array $jsFiles): void |
435
|
|
|
{ |
436
|
|
|
/** @var mixed $value */ |
437
|
4 |
|
foreach ($jsFiles as $key => $value) { |
438
|
4 |
|
$this->registerJsFileByConfig( |
439
|
4 |
|
is_string($key) ? $key : null, |
440
|
4 |
|
is_array($value) ? $value : [$value], |
441
|
|
|
); |
442
|
|
|
} |
443
|
1 |
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* It processes the JS strings generated by the asset manager. |
447
|
|
|
* |
448
|
|
|
* @param array $jsStrings |
449
|
|
|
* |
450
|
|
|
* @throws InvalidArgumentException |
451
|
|
|
*/ |
452
|
7 |
|
public function addJsStrings(array $jsStrings): void |
453
|
|
|
{ |
454
|
|
|
/** @var mixed $value */ |
455
|
7 |
|
foreach ($jsStrings as $key => $value) { |
456
|
7 |
|
$this->registerJsStringByConfig( |
457
|
7 |
|
is_string($key) ? $key : null, |
458
|
7 |
|
is_array($value) ? $value : [$value, WebView::POSITION_END] |
459
|
|
|
); |
460
|
|
|
} |
461
|
1 |
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* It processes the JS variables generated by the asset manager and converts it into JS code. |
465
|
|
|
* |
466
|
|
|
* @param array $jsVars |
467
|
|
|
* |
468
|
|
|
* @throws InvalidArgumentException |
469
|
|
|
*/ |
470
|
5 |
|
public function addJsVars(array $jsVars): void |
471
|
|
|
{ |
472
|
|
|
/** @var mixed $value */ |
473
|
5 |
|
foreach ($jsVars as $key => $value) { |
474
|
5 |
|
if (is_string($key)) { |
475
|
1 |
|
$this->registerJsVar($key, $value, WebView::POSITION_HEAD); |
476
|
|
|
} else { |
477
|
5 |
|
$this->registerJsVarByConfig((array) $value); |
478
|
|
|
} |
479
|
|
|
} |
480
|
1 |
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Clears the data for working with the event loop: |
484
|
|
|
* - the added parameters and blocks; |
485
|
|
|
* - the registered meta tags, link tags, css/js scripts, files and title. |
486
|
|
|
*/ |
487
|
1 |
|
public function clear(): void |
488
|
|
|
{ |
489
|
1 |
|
$this->parameters = []; |
490
|
1 |
|
$this->blocks = []; |
491
|
1 |
|
$this->title = ''; |
492
|
1 |
|
$this->metaTags = []; |
493
|
1 |
|
$this->linkTags = []; |
494
|
1 |
|
$this->css = []; |
495
|
1 |
|
$this->cssFiles = []; |
496
|
1 |
|
$this->js = []; |
497
|
1 |
|
$this->jsFiles = []; |
498
|
1 |
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @throws InvalidArgumentException |
502
|
|
|
*/ |
503
|
4 |
|
private function registerCssFileByConfig(?string $key, array $config): void |
504
|
|
|
{ |
505
|
4 |
|
if (!array_key_exists(0, $config)) { |
506
|
1 |
|
throw new InvalidArgumentException('Do not set CSS file.'); |
507
|
|
|
} |
508
|
3 |
|
$file = $config[0]; |
509
|
|
|
|
510
|
3 |
|
if (!is_string($file)) { |
511
|
1 |
|
throw new InvalidArgumentException( |
512
|
1 |
|
sprintf( |
513
|
1 |
|
'CSS file should be string. Got %s.', |
514
|
1 |
|
$this->getType($file), |
515
|
|
|
) |
516
|
|
|
); |
517
|
|
|
} |
518
|
|
|
|
519
|
2 |
|
$position = (int) ($config[1] ?? WebView::POSITION_HEAD); |
520
|
|
|
|
521
|
2 |
|
unset($config[0], $config[1]); |
522
|
2 |
|
$this->registerCssFile($file, $position, $config, $key); |
523
|
1 |
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* @throws InvalidArgumentException |
527
|
|
|
*/ |
528
|
7 |
|
private function registerCssStringByConfig(?string $key, array $config): void |
529
|
|
|
{ |
530
|
7 |
|
if (!array_key_exists(0, $config)) { |
531
|
2 |
|
throw new InvalidArgumentException('Do not set CSS string.'); |
532
|
|
|
} |
533
|
5 |
|
$css = $config[0]; |
534
|
|
|
|
535
|
5 |
|
if (!is_string($css) && !($css instanceof Style)) { |
536
|
2 |
|
throw new InvalidArgumentException( |
537
|
2 |
|
sprintf( |
538
|
2 |
|
'CSS string should be string or instance of \\' . Style::class . '. Got %s.', |
539
|
2 |
|
$this->getType($css), |
540
|
|
|
) |
541
|
|
|
); |
542
|
|
|
} |
543
|
|
|
|
544
|
3 |
|
$position = $config[1] ?? WebView::POSITION_HEAD; |
545
|
3 |
|
if (!$this->isValidCssPosition($position)) { |
546
|
2 |
|
throw new InvalidArgumentException('Invalid position of CSS strings.'); |
547
|
|
|
} |
548
|
|
|
|
549
|
1 |
|
unset($config[0], $config[1]); |
550
|
1 |
|
if ($config !== []) { |
551
|
1 |
|
$css = ($css instanceof Style ? $css : Html::style($css))->attributes($config); |
552
|
|
|
} |
553
|
|
|
|
554
|
1 |
|
is_string($css) |
555
|
1 |
|
? $this->registerCss($css, $position, [], $key) |
556
|
1 |
|
: $this->registerStyleTag($css, $position, $key); |
557
|
1 |
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* @param mixed $position |
561
|
|
|
* |
562
|
|
|
* @psalm-assert =int $position |
563
|
|
|
*/ |
564
|
16 |
|
private function isValidCssPosition($position): bool |
565
|
|
|
{ |
566
|
|
|
return in_array( |
567
|
16 |
|
$position, |
568
|
|
|
[ |
569
|
|
|
WebView::POSITION_HEAD, |
570
|
|
|
WebView::POSITION_BEGIN, |
571
|
|
|
WebView::POSITION_END, |
572
|
|
|
], |
573
|
|
|
true, |
574
|
|
|
); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* @throws InvalidArgumentException |
579
|
|
|
*/ |
580
|
4 |
|
private function registerJsFileByConfig(?string $key, array $config): void |
581
|
|
|
{ |
582
|
4 |
|
if (!array_key_exists(0, $config)) { |
583
|
1 |
|
throw new InvalidArgumentException('Do not set JS file.'); |
584
|
|
|
} |
585
|
3 |
|
$file = $config[0]; |
586
|
|
|
|
587
|
3 |
|
if (!is_string($file)) { |
588
|
1 |
|
throw new InvalidArgumentException( |
589
|
1 |
|
sprintf( |
590
|
1 |
|
'JS file should be string. Got %s.', |
591
|
1 |
|
$this->getType($file), |
592
|
|
|
) |
593
|
|
|
); |
594
|
|
|
} |
595
|
|
|
|
596
|
2 |
|
$position = (int) ($config[1] ?? WebView::POSITION_END); |
597
|
|
|
|
598
|
2 |
|
unset($config[0], $config[1]); |
599
|
2 |
|
$this->registerJsFile($file, $position, $config, $key); |
600
|
1 |
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* @throws InvalidArgumentException |
604
|
|
|
*/ |
605
|
7 |
|
private function registerJsStringByConfig(?string $key, array $config): void |
606
|
|
|
{ |
607
|
7 |
|
if (!array_key_exists(0, $config)) { |
608
|
2 |
|
throw new InvalidArgumentException('Do not set JS string.'); |
609
|
|
|
} |
610
|
5 |
|
$js = $config[0]; |
611
|
|
|
|
612
|
5 |
|
if (!is_string($js) && !($js instanceof Script)) { |
613
|
2 |
|
throw new InvalidArgumentException( |
614
|
2 |
|
sprintf( |
615
|
2 |
|
'JS string should be string or instance of \\' . Script::class . '. Got %s.', |
616
|
2 |
|
$this->getType($js), |
617
|
|
|
) |
618
|
|
|
); |
619
|
|
|
} |
620
|
|
|
|
621
|
3 |
|
$position = $config[1] ?? WebView::POSITION_END; |
622
|
3 |
|
if (!$this->isValidJsPosition($position)) { |
623
|
2 |
|
throw new InvalidArgumentException('Invalid position of JS strings.'); |
624
|
|
|
} |
625
|
|
|
|
626
|
1 |
|
unset($config[0], $config[1]); |
627
|
1 |
|
if ($config !== []) { |
628
|
1 |
|
$js = ($js instanceof Script ? $js : Html::script($js))->attributes($config); |
629
|
|
|
} |
630
|
|
|
|
631
|
1 |
|
is_string($js) |
632
|
1 |
|
? $this->registerJs($js, $position, $key) |
633
|
1 |
|
: $this->registerScriptTag($js, $position, $key); |
634
|
1 |
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* @throws InvalidArgumentException |
638
|
|
|
*/ |
639
|
5 |
|
private function registerJsVarByConfig(array $config): void |
640
|
|
|
{ |
641
|
5 |
|
if (!array_key_exists(0, $config)) { |
642
|
1 |
|
throw new InvalidArgumentException('Do not set JS variable name.'); |
643
|
|
|
} |
644
|
4 |
|
$key = $config[0]; |
645
|
|
|
|
646
|
4 |
|
if (!is_string($key)) { |
647
|
1 |
|
throw new InvalidArgumentException( |
648
|
1 |
|
sprintf( |
649
|
1 |
|
'JS variable name should be string. Got %s.', |
650
|
1 |
|
$this->getType($key), |
651
|
|
|
) |
652
|
|
|
); |
653
|
|
|
} |
654
|
|
|
|
655
|
3 |
|
if (!array_key_exists(1, $config)) { |
656
|
1 |
|
throw new InvalidArgumentException('Do not set JS variable value.'); |
657
|
|
|
} |
658
|
|
|
/** @var mixed */ |
659
|
2 |
|
$value = $config[1]; |
660
|
|
|
|
661
|
2 |
|
$position = $config[2] ?? WebView::POSITION_HEAD; |
662
|
2 |
|
if (!$this->isValidJsPosition($position)) { |
663
|
1 |
|
throw new InvalidArgumentException('Invalid position of JS variable.'); |
664
|
|
|
} |
665
|
|
|
|
666
|
1 |
|
$this->registerJsVar($key, $value, $position); |
667
|
1 |
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* @param mixed $position |
671
|
|
|
* |
672
|
|
|
* @psalm-assert =int $position |
673
|
|
|
*/ |
674
|
18 |
|
private function isValidJsPosition($position): bool |
675
|
|
|
{ |
676
|
|
|
return in_array( |
677
|
18 |
|
$position, |
678
|
|
|
[ |
679
|
|
|
WebView::POSITION_HEAD, |
680
|
|
|
WebView::POSITION_BEGIN, |
681
|
|
|
WebView::POSITION_END, |
682
|
|
|
WebView::POSITION_READY, |
683
|
|
|
WebView::POSITION_LOAD, |
684
|
|
|
], |
685
|
|
|
true, |
686
|
|
|
); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* @param mixed $value |
691
|
|
|
*/ |
692
|
7 |
|
private function getType($value): string |
693
|
|
|
{ |
694
|
7 |
|
return is_object($value) ? get_class($value) : gettype($value); |
695
|
|
|
} |
696
|
|
|
} |
697
|
|
|
|