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