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