Completed
Pull Request — master (#57)
by Wilmer
02:09
created

WebView   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 680
Duplicated Lines 0 %

Test Coverage

Coverage 62.94%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 65
eloc 170
dl 0
loc 680
ccs 107
cts 170
cp 0.6294
rs 3.2
c 5
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A registerLinkTag() 0 6 2
A clear() 0 9 1
A setAssetManager() 0 3 1
A renderBodyBeginHtml() 0 11 4
A registerMetaTag() 0 6 2
A registerJs() 0 4 2
A registerJsFile() 0 18 4
A registerCssFile() 0 18 4
A head() 0 3 1
B registerAssetBundle() 0 38 8
A renderAjax() 0 15 1
A registerAssetFiles() 0 16 4
B renderBodyEndHtml() 0 37 11
A beginBody() 0 4 1
B renderHeadHtml() 0 24 8
A endPage() 0 13 1
A endBody() 0 7 2
A getAssetManager() 0 3 1
A registerCsrfMetaTags() 0 3 1
A getAssetBundles() 0 3 1
A registerJsVar() 0 4 1
A createBundle() 0 9 1
A registerCss() 0 4 2
A getEventDispatcher() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like WebView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WebView, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\View;
5
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Yiisoft\Asset\AssetBundle;
8
use Yiisoft\Asset\AssetManager;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Arrays\ArrayHelper;
11
use Yiisoft\View\Event\BodyBegin;
12
use Yiisoft\View\Event\BodyEnd;
13
use Yiisoft\View\Event\PageEnd;
14
15
/**
16
 * View represents a view object in the MVC pattern.
17
 *
18
 * View provides a set of methods (e.g. {@see render()} for rendering purpose.
19
 *
20
 * You can modify its configuration by adding an array to your application config under `components` as it is shown in
21
 * the following example:
22
 *
23
 * ```php
24
 * 'view' => [
25
 *     'theme' => 'app\themes\MyTheme',
26
 *     'renderers' => [
27
 *         // you may add Smarty or Twig renderer here
28
 *     ]
29
 *     // ...
30
 * ]
31
 * ```
32
 *
33
 * For more details and usage information on View, see the [guide article on views](guide:structure-views).
34
 */
35
class WebView extends View
36
{
37
    /**
38
     * The location of registered JavaScript code block or files.
39
     * This means the location is in the head section.
40
     */
41
    public const POS_HEAD = 1;
42
43
    /**
44
     * The location of registered JavaScript code block or files.
45
     * This means the location is at the beginning of the body section.
46
     */
47
    public const POS_BEGIN = 2;
48
49
    /**
50
     * The location of registered JavaScript code block or files.
51
     * This means the location is at the end of the body section.
52
     */
53
    public const POS_END = 3;
54
55
    /**
56
     * The location of registered JavaScript code block.
57
     * This means the JavaScript code block will be executed when HTML document composition is ready.
58
     */
59
    public const POS_READY = 4;
60
61
    /**
62
     * The location of registered JavaScript code block.
63
     * This means the JavaScript code block will be executed when HTML page is completely loaded.
64
     */
65
    public const POS_LOAD = 5;
66
67
    /**
68
     * This is internally used as the placeholder for receiving the content registered for the head section.
69
     */
70
    private const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
71
72
    /**
73
     * This is internally used as the placeholder for receiving the content registered for the beginning of the body
74
     * section.
75
     */
76
    private const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
77
78
    /**
79
     * This is internally used as the placeholder for receiving the content registered for the end of the body section.
80
     */
81
    private const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
82
83
    /**
84
     * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values
85
     * are the registered {@see AssetBundle} objects.
86
     *
87
     * {@see registerAssetBundle()}
88
     */
89
    private $assetBundles = [];
90
91
    /**
92
     * @var AssetManager $assetManager
93
     */
94
    private $assetManager;
95
96
    /**
97
     * @var string the page title
98
     */
99
    private $title;
0 ignored issues
show
introduced by
The private property $title is not used, and could be removed.
Loading history...
100
101
    /**
102
     * @var array the registered meta tags.
103
     *
104
     * {@see registerMetaTag()}
105
     */
106
    private $metaTags = [];
107
108
    /**
109
     * @var array the registered link tags.
110
     *
111
     * {@see registerLinkTag()}
112
     */
113
    private $linkTags = [];
114
115
    /**
116
     * @var array the registered CSS code blocks.
117
     *
118
     * {@see registerCss()}
119
     */
120
    private $css = [];
121
122
    /**
123
     * @var array the registered CSS files.
124
     *
125
     * {@see registerCssFile()}
126
     */
127
    private $cssFiles = [];
128
129
    /**
130
     * @var array the registered JS code blocks
131
     *
132
     * {@see registerJs()}
133
     */
134
    private $js = [];
135
136
    /**
137
     * @var array the registered JS files.
138
     *
139
     * {@see registerJsFile()}
140
     */
141
    private $jsFiles = [];
142
143
    /**
144
     * Marks the position of an HTML head section.
145
     */
146 32
    public function head(): void
147
    {
148 32
        echo self::PH_HEAD;
149
    }
150
151
    /**
152
     * Marks the beginning of an HTML body section.
153
     */
154 32
    public function beginBody(): void
155
    {
156 32
        echo self::PH_BODY_BEGIN;
157 32
        $this->eventDispatcher->dispatch(new BodyBegin($this->getViewFile()));
0 ignored issues
show
Bug introduced by
It seems like $this->getViewFile() can also be of type boolean; however, parameter $file of Yiisoft\View\Event\BodyBegin::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
        $this->eventDispatcher->dispatch(new BodyBegin(/** @scrutinizer ignore-type */ $this->getViewFile()));
Loading history...
158
    }
159
160
    /**
161
     * Marks the ending of an HTML body section.
162
     */
163 32
    public function endBody(): void
164
    {
165 32
        $this->eventDispatcher->dispatch(new BodyEnd($this->getViewFile()));
0 ignored issues
show
Bug introduced by
It seems like $this->getViewFile() can also be of type boolean; however, parameter $file of Yiisoft\View\Event\BodyEnd::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        $this->eventDispatcher->dispatch(new BodyEnd(/** @scrutinizer ignore-type */ $this->getViewFile()));
Loading history...
166 32
        echo self::PH_BODY_END;
167
168 32
        foreach (array_keys($this->assetBundles) as $bundle) {
169 8
            $this->registerAssetFiles($bundle);
170
        }
171
    }
172
173
    /**
174
     * Marks the ending of an HTML page.
175
     *
176
     * @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at
177
     * {@see POS_READY} and {@see POS_LOAD} positions will be rendered at the end of the view like
178
     * normal scripts.
179
     */
180 32
    public function endPage($ajaxMode = false): void
181
    {
182 32
        $this->eventDispatcher->dispatch(new PageEnd($this->getViewFile()));
0 ignored issues
show
Bug introduced by
It seems like $this->getViewFile() can also be of type boolean; however, parameter $file of Yiisoft\View\Event\PageEnd::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
        $this->eventDispatcher->dispatch(new PageEnd(/** @scrutinizer ignore-type */ $this->getViewFile()));
Loading history...
183
184 32
        $content = ob_get_clean();
185
186 32
        echo strtr($content, [
187 32
            self::PH_HEAD => $this->renderHeadHtml(),
188 32
            self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
189 32
            self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
190
        ]);
191
192 32
        $this->clear();
193
    }
194
195
    /**
196
     * Renders a view in response to an AJAX request.
197
     *
198
     * This method is similar to {@see render()} except that it will surround the view being rendered with the calls of
199
     * {@see beginPage()}, {@see head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the
200
     * method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the
201
     * view.
202
     *
203
     * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter.
204
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view
205
     * file.
206
     * @param object $context the context that the view should use for rendering the view. If null, existing [[context]]
207
     * will be used.
208
     *
209
     * @return string the rendering result
210
     *
211
     * {@see render()}
212
     */
213
    public function renderAjax(string $view, array $params = [], $context = null): string
214
    {
215
        $viewFile = $this->findViewFile($view, $context);
0 ignored issues
show
Bug introduced by
The method findViewFile() does not exist on Yiisoft\View\WebView. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        /** @scrutinizer ignore-call */ 
216
        $viewFile = $this->findViewFile($view, $context);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
217
        ob_start();
218
        ob_implicit_flush(0);
219
220
        $this->beginPage();
221
        $this->head();
222
        $this->beginBody();
223
        echo $this->renderFile($viewFile, $params, $context);
224
        $this->endBody();
225
        $this->endPage(true);
226
227
        return ob_get_clean();
228
    }
229
230
    /**
231
     * Registers the asset manager being used by this view object.
232
     *
233
     * @return array the asset manager. Defaults to the "assetManager" application component.
234
     */
235 10
    public function getAssetBundles(): array
236
    {
237 10
        return $this->assetBundles;
238
    }
239
240
    /**
241
     * Registers the asset manager being used by this view object.
242
     *
243
     * @return AssetManager the asset manager. Defaults to the "assetManager" application component.
244
     */
245 33
    public function getAssetManager(): AssetManager
246
    {
247 33
        return $this->assetManager;
248
    }
249
250
251
    /**
252
     * @return EventDispatcherInterface. Defaults to the "eventDispatcher" application component.
0 ignored issues
show
Documentation Bug introduced by
The doc comment EventDispatcherInterface. at position 0 could not be parsed: Unknown type name 'EventDispatcherInterface.' at position 0 in EventDispatcherInterface..
Loading history...
253
     */
254 27
    public function getEventDispatcher(): EventDispatcherInterface
255
    {
256 27
        return $this->eventDispatcher;
257
    }
258
259
    /**
260
     * Sets the asset manager.
261
     *
262
     * @param AssetManager $value the asset manager
263
     *
264
     * @return void
265
     */
266 81
    public function setAssetManager(AssetManager $value): void
267
    {
268 81
        $this->assetManager = $value;
269
    }
270
271
    /**
272
     * Clears up the registered meta tags, link tags, css/js scripts and files.
273
     *
274
     * @return void
275
     */
276 32
    public function clear(): void
277
    {
278 32
        $this->metaTags = [];
279 32
        $this->linkTags = [];
280 32
        $this->css = [];
281 32
        $this->cssFiles = [];
282 32
        $this->js = [];
283 32
        $this->jsFiles = [];
284 32
        $this->assetBundles = [];
285
    }
286
287
    /**
288
     * Registers all files provided by an asset bundle including depending bundles files.
289
     *
290
     * Removes a bundle from {@see assetBundles} once files are registered.
291
     *
292
     * @param string $name name of the bundle to register
293
     *
294
     * @return void
295
     */
296 8
    protected function registerAssetFiles(string $name): void
297
    {
298 8
        if (!isset($this->assetBundles[$name])) {
299 7
            return;
300
        }
301
302 8
        $bundle = $this->assetBundles[$name];
303
304 8
        if ($bundle) {
0 ignored issues
show
introduced by
$bundle is of type Yiisoft\Asset\AssetBundle, thus it always evaluated to true.
Loading history...
305 8
            foreach ($bundle->depends as $dep) {
306 7
                $this->registerAssetFiles($dep);
307
            }
308 8
            $bundle->registerAssetFiles($this);
309
        }
310
311 8
        unset($this->assetBundles[$name]);
312
    }
313
314
    /**
315
     * Registers the named asset bundle.
316
     *
317
     * All dependent asset bundles will be registered.
318
     *
319
     * @param string $name the class name of the asset bundle (without the leading backslash)
320
     * @param int|null $position if set, this forces a minimum position for javascript files. This will adjust depending
321
     * assets javascript file position or fail if requirement can not be met. If this is null, asset
322
     * bundles position settings will not be changed.
323
     *
324
     * {@see registerJsFile()} for more details on javascript position.
325
     *
326
     * @throws \RuntimeException if the asset bundle does not exist or a circular dependency is detected
327
     *
328
     * @return AssetBundle the registered asset bundle instance
329
     */
330 12
    public function registerAssetBundle(string $name, ?int $position = null): AssetBundle
331
    {
332 12
        if (!isset($this->assetBundles[$name])) {
333 12
            $bundle = $this->getAssetManager()->getBundle($name);
334
335 12
            $this->assetBundles[$name] = false;
336
337
            // register dependencies
338
339 12
            $pos = $bundle->jsOptions['position'] ?? null;
340
341 12
            foreach ($bundle->depends as $dep) {
342 10
                $this->registerAssetBundle($dep, $pos);
343
            }
344
345 11
            $this->assetBundles[$name] = $bundle;
346 8
        } elseif ($this->assetBundles[$name] === false) {
0 ignored issues
show
introduced by
The condition $this->assetBundles[$name] === false is always false.
Loading history...
347 1
            throw new \RuntimeException("A circular dependency is detected for bundle '$name'.");
348
        } else {
349 7
            $bundle = $this->assetBundles[$name];
350
        }
351
352 11
        if ($position !== null) {
353 8
            $pos = $bundle->jsOptions['position'] ?? null;
354
355 8
            if ($pos === null) {
356 8
                $bundle->jsOptions['position'] = $pos = $position;
357 2
            } elseif ($pos > $position) {
358 2
                throw new \RuntimeException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'.");
359
            }
360
361
            // update position for all dependencies
362 8
            foreach ($bundle->depends as $dep) {
363 6
                $this->registerAssetBundle($dep, $pos);
364
            }
365
        }
366
367 11
        return $bundle;
368
    }
369
370
    /**
371
     * Registers a meta tag.
372
     *
373
     * For example, a description meta tag can be added like the following:
374
     *
375
     * ```php
376
     * $view->registerMetaTag([
377
     *     'name' => 'description',
378
     *     'content' => 'This website is about funny raccoons.'
379
     * ]);
380
     * ```
381
     *
382
     * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`.
383
     *
384
     * @param array $options the HTML attributes for the meta tag.
385
     * @param string $key the key that identifies the meta tag. If two meta tags are registered with the same key, the
386
     * latter will overwrite the former. If this is null, the new meta tag will be appended to the
387
     * existing ones.
388
     *
389
     * @return void
390
     */
391
    public function registerMetaTag(array $options, string $key = null): void
392
    {
393
        if ($key === null) {
394
            $this->metaTags[] = Html::tag('meta', '', $options);
395
        } else {
396
            $this->metaTags[$key] = Html::tag('meta', '', $options);
397
        }
398
    }
399
400
    /**
401
     * Registers a link tag.
402
     *
403
     * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) can be added like the
404
     * following:
405
     *
406
     * ```php
407
     * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
408
     * ```
409
     *
410
     * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`.
411
     *
412
     * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which has more options
413
     * for this kind of link tag.
414
     *
415
     * @param array $options the HTML attributes for the link tag.
416
     * @param string|null $key the key that identifies the link tag. If two link tags are registered with the same
417
     * key, the latter will overwrite the former. If this is null, the new link tag will be appended
418
     * to the existing ones.
419
     */
420
    public function registerLinkTag(array $options, ?string $key = null): void
421
    {
422
        if ($key === null) {
423
            $this->linkTags[] = Html::tag('link', '', $options);
424
        } else {
425
            $this->linkTags[$key] = Html::tag('link', '', $options);
426
        }
427
    }
428
429
    /**
430
     * Registers CSRF meta tags.
431
     *
432
     * They are rendered dynamically to retrieve a new CSRF token for each request.
433
     *
434
     * ```php
435
     * $view->registerCsrfMetaTags();
436
     * ```
437
     *
438
     * The above code will result in `<meta name="csrf-param" content="[Yiisoft\Web\Request::$csrfParam]">` and
439
     * `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page.
440
     *
441
     * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()`
442
     * from `yii.js`.
443
     */
444
    public function registerCsrfMetaTags(): void
445
    {
446
        $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return Yiisoft\Html\Html::csrfMetaTags();');
447
    }
448
449
    /**
450
     * Registers a CSS code block.
451
     *
452
     * @param string $css the content of the CSS code block to be registered
453
     * @param array $options the HTML attributes for the `<style>`-tag.
454
     * @param string $key the key that identifies the CSS code block. If null, it will use $css as the key. If two CSS
455
     * code blocks are registered with the same key, the latter will overwrite the former.
456
     */
457
    public function registerCss(string $css, array $options = [], string $key = null): void
458
    {
459
        $key = $key ?: md5($css);
460
        $this->css[$key] = Html::style($css, $options);
461
    }
462
463
    /**
464
     * Registers a CSS file.
465
     *
466
     * This method should be used for simple registration of CSS files. If you want to use features of
467
     * {@see AssetManager} like appending timestamps to the URL and file publishing options, use {@see AssetBundle}
468
     * and {@see registerAssetBundle()} instead.
469
     *
470
     * @param string $url the CSS file to be registered.
471
     * @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()}
472
     * for the supported options. The following options are specially handled and are not treated as HTML
473
     * attributes:
474
     *
475
     *   - `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
476
     *
477
     * @param string $key the key that identifies the CSS script file. If null, it will use $url as the key. If two CSS
478
     * files are registered with the same key, the latter will overwrite the former.
479
     *
480
     * @return void
481
     */
482 18
    public function registerCssFile(string $url, array $options = [], string $key = null): void
483
    {
484 18
        $key = $key ?: $url;
485
486 18
        $depends = ArrayHelper::remove($options, 'depends', []);
487
488 18
        if (empty($depends)) {
489 18
            $this->cssFiles[$key] = Html::cssFile($url, $options);
490
        } else {
491
            $bundle = $this->createBundle([
492
                'baseUrl' => '',
493
                'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
494
                'cssOptions' => $options,
495
                'depends' => (array) $depends,
496
            ]);
497
            $bundles = [$key => $bundle];
0 ignored issues
show
Unused Code introduced by
The assignment to $bundles is dead and can be removed.
Loading history...
498
499
            $this->registerAssetBundle($key);
500
        }
501
    }
502
503
    /**
504
     * Registers a JS code block.
505
     *
506
     * @param string $js the JS code block to be registered
507
     * @param int $position the position at which the JS script tag should be inserted in a page.
508
     *
509
     * The possible values are:
510
     *
511
     * - [[POS_HEAD]]: in the head section
512
     * - [[POS_BEGIN]]: at the beginning of the body section
513
     * - [[POS_END]]: at the end of the body section. This is the default value.
514
     * - [[POS_LOAD]]: executed when HTML page is completely loaded.
515
     * - [[POS_READY]]: executed when HTML document composition is ready.
516
     *
517
     * @param string $key the key that identifies the JS code block. If null, it will use $js as the key. If two JS code
518
     * blocks are registered with the same key, the latter will overwrite the former.
519
     *
520
     * @return void
521
     */
522 1
    public function registerJs(string $js, int $position = self::POS_END, string $key = null): void
523
    {
524 1
        $key = $key ?: md5($js);
525 1
        $this->js[$position][$key] = $js;
526
    }
527
528
    /**
529
     * Registers a JS file.
530
     *
531
     * This method should be used for simple registration of JS files. If you want to use features of
532
     * {@see AssetManager} like appending timestamps to the URL and file publishing options, use {@see AssetBundle}
533
     * and {@see registerAssetBundle()} instead.
534
     *
535
     * @param string $url the JS file to be registered.
536
     * @param array $options the HTML attributes for the script tag. The following options are specially handled and
537
     * are not treated as HTML attributes:
538
     *
539
     * - `depends`: array, specifies the names of the asset bundles that this JS file depends on.
540
     * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
541
     *     * [[POS_HEAD]]: in the head section
542
     *     * [[POS_BEGIN]]: at the beginning of the body section
543
     *     * [[POS_END]]: at the end of the body section. This is the default value.
544
     *
545
     * Please refer to {@see \Yiisoft\Html\Html::jsFile()} for other supported options.
546
     *
547
     * @param string $key the key that identifies the JS script file. If null, it will use $url as the key. If two JS
548
     * files are registered with the same key at the same position, the latter will overwrite the former.
549
     * Note that position option takes precedence, thus files registered with the same key, but different
550
     * position option will not override each other.
551
     *
552
     * @return void
553
     */
554 19
    public function registerJsFile(string $url, array $options = [], string $key = null): void
555
    {
556 19
        $key = $key ?: $url;
557
558 19
        $depends = ArrayHelper::remove($options, 'depends', []);
559
560 19
        if (empty($depends)) {
561 19
            $position = ArrayHelper::remove($options, 'position', self::POS_END);
562 19
            $this->jsFiles[$position][$key] = Html::jsFile($url, $options);
563
        } else {
564
            $bundle = $this->createBundle([
565
                'baseUrl' => '',
566
                'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
567
                'jsOptions' => $options,
568
                'depends' => (array) $depends,
569
            ]);
570
            $bundles = [$key => $bundle];
0 ignored issues
show
Unused Code introduced by
The assignment to $bundles is dead and can be removed.
Loading history...
571
            $this->registerAssetBundle($key);
572
        }
573
    }
574
575
    /**
576
     * Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated
577
     * variable names.
578
     *
579
     * @param string $name Name of the variable
580
     * @param array|string $value Value of the variable
581
     * @param int $position the position in a page at which the JavaScript variable should be inserted.
582
     *
583
     * The possible values are:
584
     *
585
     * - [[POS_HEAD]]: in the head section. This is the default value.
586
     * - [[POS_BEGIN]]: at the beginning of the body section.
587
     * - [[POS_END]]: at the end of the body section.
588
     * - [[POS_LOAD]]: enclosed within jQuery(window).load().
589
     *   Note that by using this position, the method will automatically register the jQuery js file.
590
     * - [[POS_READY]]: enclosed within jQuery(document).ready().
591
     *   Note that by using this position, the method will automatically register the jQuery js file.
592
     */
593 1
    public function registerJsVar(string $name, $value, int $position = self::POS_HEAD): void
594
    {
595 1
        $js = sprintf('var %s = %s;', $name, \Yiisoft\Json\Json::htmlEncode($value));
596 1
        $this->registerJs($js, $position, $name);
597
    }
598
599
    /**
600
     * Renders the content to be inserted in the head section.
601
     *
602
     * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
603
     *
604
     * @return string the rendered content
605
     */
606 32
    protected function renderHeadHtml(): string
607
    {
608 32
        $lines = [];
609 32
        if (!empty($this->metaTags)) {
610
            $lines[] = implode("\n", $this->metaTags);
611
        }
612
613 32
        if (!empty($this->linkTags)) {
614
            $lines[] = implode("\n", $this->linkTags);
615
        }
616 32
        if (!empty($this->cssFiles)) {
617 18
            $lines[] = implode("\n", $this->cssFiles);
618
        }
619 32
        if (!empty($this->css)) {
620
            $lines[] = implode("\n", $this->css);
621
        }
622 32
        if (!empty($this->jsFiles[self::POS_HEAD])) {
623 3
            $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]);
624
        }
625 32
        if (!empty($this->js[self::POS_HEAD])) {
626 1
            $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]));
627
        }
628
629 32
        return empty($lines) ? '' : implode("\n", $lines);
630
    }
631
632
    /**
633
     * Renders the content to be inserted at the beginning of the body section.
634
     *
635
     * The content is rendered using the registered JS code blocks and files.
636
     *
637
     * @return string the rendered content
638
     */
639 32
    protected function renderBodyBeginHtml(): string
640
    {
641 32
        $lines = [];
642 32
        if (!empty($this->jsFiles[self::POS_BEGIN])) {
643 3
            $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]);
644
        }
645 32
        if (!empty($this->js[self::POS_BEGIN])) {
646
            $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]));
647
        }
648
649 32
        return empty($lines) ? '' : implode("\n", $lines);
650
    }
651
652
    /**
653
     * Renders the content to be inserted at the end of the body section.
654
     *
655
     * The content is rendered using the registered JS code blocks and files.
656
     *
657
     * @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at
658
     * [[POS_READY]] and [[POS_LOAD]] positions will be rendered at the end of the view like normal scripts.
659
     *
660
     * @return string the rendered content
661
     */
662 32
    protected function renderBodyEndHtml(bool $ajaxMode): string
663
    {
664 32
        $lines = [];
665
666 32
        if (!empty($this->jsFiles[self::POS_END])) {
667 15
            $lines[] = implode("\n", $this->jsFiles[self::POS_END]);
668
        }
669
670 32
        if ($ajaxMode) {
671
            $scripts = [];
672
            if (!empty($this->js[self::POS_END])) {
673
                $scripts[] = implode("\n", $this->js[self::POS_END]);
674
            }
675
            if (!empty($this->js[self::POS_READY])) {
676
                $scripts[] = implode("\n", $this->js[self::POS_READY]);
677
            }
678
            if (!empty($this->js[self::POS_LOAD])) {
679
                $scripts[] = implode("\n", $this->js[self::POS_LOAD]);
680
            }
681
            if (!empty($scripts)) {
682
                $lines[] = Html::script(implode("\n", $scripts));
683
            }
684
        } else {
685 32
            if (!empty($this->js[self::POS_END])) {
686
                $lines[] = Html::script(implode("\n", $this->js[self::POS_END]));
687
            }
688 32
            if (!empty($this->js[self::POS_READY])) {
689
                $js = "document.addEventListener('DOMContentLoaded', function(event) {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
690
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
691
            }
692 32
            if (!empty($this->js[self::POS_LOAD])) {
693
                $js = "window.addEventListener('load', function (event) {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});";
694
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
695
            }
696
        }
697
698 32
        return empty($lines) ? '' : implode("\n", $lines);
699
    }
700
701
    /**
702
     * @param array $options
703
     *
704
     * @return AssetBundle
705
     */
706
    private function createBundle(array $options): AssetBundle
707
    {
708
        $bundle = new AssetBundle();
709
        $bundle->baseUrl = $options['baseUrl'];
710
        $bundle->js = $options['js'];
711
        $bundle->jsOptions = $options['jsOptions'];
712
        $bundle->depends = $options['depends'];
713
714
        return $bundle;
715
    }
716
}
717