GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cd2d3a...6886d6 )
by Robert
09:38
created

View::getAssetManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Html;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * View represents a view object in the MVC pattern.
17
 *
18
 * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
19
 *
20
 * View is configured as an application component in [[\yii\base\Application]] by default.
21
 * You can access that instance via `Yii::$app->view`.
22
 *
23
 * You can modify its configuration by adding an array to your application config under `components`
24
 * as it is shown in the following example:
25
 *
26
 * ```php
27
 * 'view' => [
28
 *     'theme' => 'app\themes\MyTheme',
29
 *     'renderers' => [
30
 *         // you may add Smarty or Twig renderer here
31
 *     ]
32
 *     // ...
33
 * ]
34
 * ```
35
 *
36
 * @property \yii\web\AssetManager $assetManager The asset manager. Defaults to the "assetManager" application
37
 * component.
38
 *
39
 * @author Qiang Xue <[email protected]>
40
 * @since 2.0
41
 */
42
class View extends \yii\base\View
43
{
44
    /**
45
     * @event Event an event that is triggered by [[beginBody()]].
46
     */
47
    const EVENT_BEGIN_BODY = 'beginBody';
48
    /**
49
     * @event Event an event that is triggered by [[endBody()]].
50
     */
51
    const EVENT_END_BODY = 'endBody';
52
    /**
53
     * The location of registered JavaScript code block or files.
54
     * This means the location is in the head section.
55
     */
56
    const POS_HEAD = 1;
57
    /**
58
     * The location of registered JavaScript code block or files.
59
     * This means the location is at the beginning of the body section.
60
     */
61
    const POS_BEGIN = 2;
62
    /**
63
     * The location of registered JavaScript code block or files.
64
     * This means the location is at the end of the body section.
65
     */
66
    const POS_END = 3;
67
    /**
68
     * The location of registered JavaScript code block.
69
     * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`.
70
     */
71
    const POS_READY = 4;
72
    /**
73
     * The location of registered JavaScript code block.
74
     * This means the JavaScript code block will be enclosed within `jQuery(window).load()`.
75
     */
76
    const POS_LOAD = 5;
77
    /**
78
     * This is internally used as the placeholder for receiving the content registered for the head section.
79
     */
80
    const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
81
    /**
82
     * This is internally used as the placeholder for receiving the content registered for the beginning of the body section.
83
     */
84
    const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
85
    /**
86
     * This is internally used as the placeholder for receiving the content registered for the end of the body section.
87
     */
88
    const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
89
90
    /**
91
     * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values
92
     * are the registered [[AssetBundle]] objects.
93
     * @see registerAssetBundle()
94
     */
95
    public $assetBundles = [];
96
    /**
97
     * @var string the page title
98
     */
99
    public $title;
100
    /**
101
     * @var array the registered meta tags.
102
     * @see registerMetaTag()
103
     */
104
    public $metaTags;
105
    /**
106
     * @var array the registered link tags.
107
     * @see registerLinkTag()
108
     */
109
    public $linkTags;
110
    /**
111
     * @var array the registered CSS code blocks.
112
     * @see registerCss()
113
     */
114
    public $css;
115
    /**
116
     * @var array the registered CSS files.
117
     * @see registerCssFile()
118
     */
119
    public $cssFiles;
120
    /**
121
     * @var array the registered JS code blocks
122
     * @see registerJs()
123
     */
124
    public $js;
125
    /**
126
     * @var array the registered JS files.
127
     * @see registerJsFile()
128
     */
129
    public $jsFiles;
130
131
    private $_assetManager;
132
133
134
    /**
135
     * Marks the position of an HTML head section.
136
     */
137 30
    public function head()
138
    {
139 30
        echo self::PH_HEAD;
140 30
    }
141
142
    /**
143
     * Marks the beginning of an HTML body section.
144
     */
145 30
    public function beginBody()
146
    {
147 30
        echo self::PH_BODY_BEGIN;
148 30
        $this->trigger(self::EVENT_BEGIN_BODY);
149 30
    }
150
151
    /**
152
     * Marks the ending of an HTML body section.
153
     */
154 30
    public function endBody()
155
    {
156 30
        $this->trigger(self::EVENT_END_BODY);
157 30
        echo self::PH_BODY_END;
158
159 30
        foreach (array_keys($this->assetBundles) as $bundle) {
160 10
            $this->registerAssetFiles($bundle);
161 30
        }
162 30
    }
163
164
    /**
165
     * Marks the ending of an HTML page.
166
     * @param boolean $ajaxMode whether the view is rendering in AJAX mode.
167
     * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
168
     * will be rendered at the end of the view like normal scripts.
169
     */
170 30
    public function endPage($ajaxMode = false)
171
    {
172 30
        $this->trigger(self::EVENT_END_PAGE);
173
174 30
        $content = ob_get_clean();
175
176 30
        echo strtr($content, [
177 30
            self::PH_HEAD => $this->renderHeadHtml(),
178 30
            self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
179 30
            self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
180 30
        ]);
181
182 30
        $this->clear();
183 30
    }
184
185
    /**
186
     * Renders a view in response to an AJAX request.
187
     *
188
     * This method is similar to [[render()]] except that it will surround the view being rendered
189
     * with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]].
190
     * By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files
191
     * that are registered with the view.
192
     *
193
     * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter.
194
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
195
     * @param object $context the context that the view should use for rendering the view. If null,
196
     * existing [[context]] will be used.
197
     * @return string the rendering result
198
     * @see render()
199
     */
200
    public function renderAjax($view, $params = [], $context = null)
201
    {
202
        $viewFile = $this->findViewFile($view, $context);
203
204
        ob_start();
205
        ob_implicit_flush(false);
206
207
        $this->beginPage();
208
        $this->head();
209
        $this->beginBody();
210
        echo $this->renderFile($viewFile, $params, $context);
0 ignored issues
show
Bug introduced by
It seems like $viewFile defined by $this->findViewFile($view, $context) on line 202 can also be of type boolean; however, yii\base\View::renderFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
211
        $this->endBody();
212
        $this->endPage(true);
213
214
        return ob_get_clean();
215
    }
216
217
    /**
218
     * Registers the asset manager being used by this view object.
219
     * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component.
220
     */
221 24
    public function getAssetManager()
222
    {
223 24
        return $this->_assetManager ?: Yii::$app->getAssetManager();
224
    }
225
226
    /**
227
     * Sets the asset manager.
228
     * @param \yii\web\AssetManager $value the asset manager
229
     */
230 65
    public function setAssetManager($value)
231
    {
232 65
        $this->_assetManager = $value;
233 65
    }
234
235
    /**
236
     * Clears up the registered meta tags, link tags, css/js scripts and files.
237
     */
238 30
    public function clear()
239
    {
240 30
        $this->metaTags = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $metaTags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
241 30
        $this->linkTags = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $linkTags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
242 30
        $this->css = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $css.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
243 30
        $this->cssFiles = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $cssFiles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
244 30
        $this->js = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $js.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
245 30
        $this->jsFiles = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $jsFiles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
246 30
        $this->assetBundles = [];
247 30
    }
248
249
    /**
250
     * Registers all files provided by an asset bundle including depending bundles files.
251
     * Removes a bundle from [[assetBundles]] once files are registered.
252
     * @param string $name name of the bundle to register
253
     */
254 10
    protected function registerAssetFiles($name)
255
    {
256 10
        if (!isset($this->assetBundles[$name])) {
257 8
            return;
258
        }
259 10
        $bundle = $this->assetBundles[$name];
260 10
        if ($bundle) {
261 10
            foreach ($bundle->depends as $dep) {
262 8
                $this->registerAssetFiles($dep);
263 10
            }
264 10
            $bundle->registerAssetFiles($this);
265 10
        }
266 10
        unset($this->assetBundles[$name]);
267 10
    }
268
269
    /**
270
     * Registers the named asset bundle.
271
     * All dependent asset bundles will be registered.
272
     * @param string $name the class name of the asset bundle (without the leading backslash)
273
     * @param integer|null $position if set, this forces a minimum position for javascript files.
274
     * This will adjust depending assets javascript file position or fail if requirement can not be met.
275
     * If this is null, asset bundles position settings will not be changed.
276
     * See [[registerJsFile]] for more details on javascript position.
277
     * @return AssetBundle the registered asset bundle instance
278
     * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected
279
     */
280 24
    public function registerAssetBundle($name, $position = null)
281
    {
282 24
        if (!isset($this->assetBundles[$name])) {
283 23
            $am = $this->getAssetManager();
284 23
            $bundle = $am->getBundle($name);
285 23
            $this->assetBundles[$name] = false;
286
            // register dependencies
287 23
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
288 23
            foreach ($bundle->depends as $dep) {
289 15
                $this->registerAssetBundle($dep, $pos);
290 22
            }
291 22
            $this->assetBundles[$name] = $bundle;
292 24
        } elseif ($this->assetBundles[$name] === false) {
293 1
            throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
294
        } else {
295 12
            $bundle = $this->assetBundles[$name];
296
        }
297
298 23
        if ($position !== null) {
299 12
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
300 12
            if ($pos === null) {
301 12
                $bundle->jsOptions['position'] = $pos = $position;
302 12
            } elseif ($pos > $position) {
303 6
                throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'.");
304
            }
305
            // update position for all dependencies
306 12
            foreach ($bundle->depends as $dep) {
307 6
                $this->registerAssetBundle($dep, $pos);
308 12
            }
309 12
        }
310
311 23
        return $bundle;
312
    }
313
314
    /**
315
     * Registers a meta tag.
316
     *
317
     * For example, a description meta tag can be added like the following:
318
     *
319
     * ```php
320
     * $view->registerMetaTag([
321
     *     'name' => 'description',
322
     *     'content' => 'This website is about funny raccoons.'
323
     * ]);
324
     * ```
325
     *
326
     * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`.
327
     *
328
     * @param array $options the HTML attributes for the meta tag.
329
     * @param string $key the key that identifies the meta tag. If two meta tags are registered
330
     * with the same key, the latter will overwrite the former. If this is null, the new meta tag
331
     * will be appended to the existing ones.
332
     */
333
    public function registerMetaTag($options, $key = null)
334
    {
335
        if ($key === null) {
336
            $this->metaTags[] = Html::tag('meta', '', $options);
337
        } else {
338
            $this->metaTags[$key] = Html::tag('meta', '', $options);
339
        }
340
    }
341
342
    /**
343
     * Registers a link tag.
344
     *
345
     * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon)
346
     * can be added like the following:
347
     *
348
     * ```php
349
     * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
350
     * ```
351
     *
352
     * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`.
353
     *
354
     * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which
355
     * has more options for this kind of link tag.
356
     *
357
     * @param array $options the HTML attributes for the link tag.
358
     * @param string $key the key that identifies the link tag. If two link tags are registered
359
     * with the same key, the latter will overwrite the former. If this is null, the new link tag
360
     * will be appended to the existing ones.
361
     */
362
    public function registerLinkTag($options, $key = null)
363
    {
364
        if ($key === null) {
365
            $this->linkTags[] = Html::tag('link', '', $options);
366
        } else {
367
            $this->linkTags[$key] = Html::tag('link', '', $options);
368
        }
369
    }
370
371
    /**
372
     * Registers a CSS code block.
373
     * @param string $css the content of the CSS code block to be registered
374
     * @param array $options the HTML attributes for the `<style>`-tag.
375
     * @param string $key the key that identifies the CSS code block. If null, it will use
376
     * $css as the key. If two CSS code blocks are registered with the same key, the latter
377
     * will overwrite the former.
378
     */
379
    public function registerCss($css, $options = [], $key = null)
380
    {
381
        $key = $key ?: md5($css);
382
        $this->css[$key] = Html::style($css, $options);
383
    }
384
385
    /**
386
     * Registers a CSS file.
387
     * @param string $url the CSS file to be registered.
388
     * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for
389
     * the supported options. The following options are specially handled and are not treated as HTML attributes:
390
     *
391
     * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
392
     *
393
     * @param string $key the key that identifies the CSS script file. If null, it will use
394
     * $url as the key. If two CSS files are registered with the same key, the latter
395
     * will overwrite the former.
396
     */
397 18
    public function registerCssFile($url, $options = [], $key = null)
398
    {
399 18
        $url = Yii::getAlias($url);
400 18
        $key = $key ?: $url;
401
402 18
        $depends = ArrayHelper::remove($options, 'depends', []);
403
404 18
        if (empty($depends)) {
405 18
            $this->cssFiles[$key] = Html::cssFile($url, $options);
0 ignored issues
show
Bug introduced by
It seems like $url defined by \Yii::getAlias($url) on line 399 can also be of type boolean; however, yii\helpers\BaseHtml::cssFile() does only seem to accept array|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
406 18
        } else {
407
            $this->getAssetManager()->bundles[$key] = Yii::createObject([
408
                'class' => AssetBundle::className(),
409
                'baseUrl' => '',
410
                'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
411
                'cssOptions' => $options,
412
                'depends' => (array)$depends,
413
            ]);
414
            $this->registerAssetBundle($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by $key ?: $url on line 400 can also be of type boolean; however, yii\web\View::registerAssetBundle() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
415
        }
416 18
    }
417
418
    /**
419
     * Registers a JS code block.
420
     * @param string $js the JS code block to be registered
421
     * @param integer $position the position at which the JS script tag should be inserted
422
     * in a page. The possible values are:
423
     *
424
     * - [[POS_HEAD]]: in the head section
425
     * - [[POS_BEGIN]]: at the beginning of the body section
426
     * - [[POS_END]]: at the end of the body section
427
     * - [[POS_LOAD]]: enclosed within jQuery(window).load().
428
     *   Note that by using this position, the method will automatically register the jQuery js file.
429
     * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value.
430
     *   Note that by using this position, the method will automatically register the jQuery js file.
431
     *
432
     * @param string $key the key that identifies the JS code block. If null, it will use
433
     * $js as the key. If two JS code blocks are registered with the same key, the latter
434
     * will overwrite the former.
435
     */
436 2
    public function registerJs($js, $position = self::POS_READY, $key = null)
437
    {
438 2
        $key = $key ?: md5($js);
439 2
        $this->js[$position][$key] = $js;
440 2
        if ($position === self::POS_READY || $position === self::POS_LOAD) {
441 2
            JqueryAsset::register($this);
442 2
        }
443 2
    }
444
445
    /**
446
     * Registers a JS file.
447
     * @param string $url the JS file to be registered.
448
     * @param array $options the HTML attributes for the script tag. The following options are specially handled
449
     * and are not treated as HTML attributes:
450
     *
451
     * - `depends`: array, specifies the names of the asset bundles that this JS file depends on.
452
     * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
453
     *     * [[POS_HEAD]]: in the head section
454
     *     * [[POS_BEGIN]]: at the beginning of the body section
455
     *     * [[POS_END]]: at the end of the body section. This is the default value.
456
     *
457
     * Please refer to [[Html::jsFile()]] for other supported options.
458
     *
459
     * @param string $key the key that identifies the JS script file. If null, it will use
460
     * $url as the key. If two JS files are registered with the same key at the same position, the latter
461
     * will overwrite the former. Note that position option takes precedence, thus files registered with the same key,
462
     * but different position option will not override each other.
463
     */
464 20
    public function registerJsFile($url, $options = [], $key = null)
465
    {
466 20
        $url = Yii::getAlias($url);
467 20
        $key = $key ?: $url;
468
469 20
        $depends = ArrayHelper::remove($options, 'depends', []);
470
471 20
        if (empty($depends)) {
472 20
            $position = ArrayHelper::remove($options, 'position', self::POS_END);
473 20
            $this->jsFiles[$position][$key] = Html::jsFile($url, $options);
0 ignored issues
show
Bug introduced by
It seems like $url defined by \Yii::getAlias($url) on line 466 can also be of type boolean; however, yii\helpers\BaseHtml::jsFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
474 20
        } else {
475
            $this->getAssetManager()->bundles[$key] = Yii::createObject([
476
                'class' => AssetBundle::className(),
477
                'baseUrl' => '',
478
                'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
479
                'jsOptions' => $options,
480
                'depends' => (array)$depends,
481
            ]);
482
            $this->registerAssetBundle($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by $key ?: $url on line 467 can also be of type boolean; however, yii\web\View::registerAssetBundle() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
483
        }
484 20
    }
485
486
    /**
487
     * Renders the content to be inserted in the head section.
488
     * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
489
     * @return string the rendered content
490
     */
491 30
    protected function renderHeadHtml()
492
    {
493 30
        $lines = [];
494 30
        if (!empty($this->metaTags)) {
495
            $lines[] = implode("\n", $this->metaTags);
496
        }
497
498 30
        if (!empty($this->linkTags)) {
499
            $lines[] = implode("\n", $this->linkTags);
500
        }
501 30
        if (!empty($this->cssFiles)) {
502 18
            $lines[] = implode("\n", $this->cssFiles);
503 18
        }
504 30
        if (!empty($this->css)) {
505
            $lines[] = implode("\n", $this->css);
506
        }
507 30
        if (!empty($this->jsFiles[self::POS_HEAD])) {
508 2
            $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]);
509 2
        }
510 30
        if (!empty($this->js[self::POS_HEAD])) {
511
            $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']);
512
        }
513
514 30
        return empty($lines) ? '' : implode("\n", $lines);
515
    }
516
517
    /**
518
     * Renders the content to be inserted at the beginning of the body section.
519
     * The content is rendered using the registered JS code blocks and files.
520
     * @return string the rendered content
521
     */
522 30
    protected function renderBodyBeginHtml()
523
    {
524 30
        $lines = [];
525 30
        if (!empty($this->jsFiles[self::POS_BEGIN])) {
526 2
            $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]);
527 2
        }
528 30
        if (!empty($this->js[self::POS_BEGIN])) {
529
            $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]), ['type' => 'text/javascript']);
530
        }
531
532 30
        return empty($lines) ? '' : implode("\n", $lines);
533
    }
534
535
    /**
536
     * Renders the content to be inserted at the end of the body section.
537
     * The content is rendered using the registered JS code blocks and files.
538
     * @param boolean $ajaxMode whether the view is rendering in AJAX mode.
539
     * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
540
     * will be rendered at the end of the view like normal scripts.
541
     * @return string the rendered content
542
     */
543 30
    protected function renderBodyEndHtml($ajaxMode)
544
    {
545 30
        $lines = [];
546
547 30
        if (!empty($this->jsFiles[self::POS_END])) {
548 16
            $lines[] = implode("\n", $this->jsFiles[self::POS_END]);
549 16
        }
550
551 30
        if ($ajaxMode) {
552
            $scripts = [];
553
            if (!empty($this->js[self::POS_END])) {
554
                $scripts[] = implode("\n", $this->js[self::POS_END]);
555
            }
556
            if (!empty($this->js[self::POS_READY])) {
557
                $scripts[] = implode("\n", $this->js[self::POS_READY]);
558
            }
559
            if (!empty($this->js[self::POS_LOAD])) {
560
                $scripts[] = implode("\n", $this->js[self::POS_LOAD]);
561
            }
562
            if (!empty($scripts)) {
563
                $lines[] = Html::script(implode("\n", $scripts), ['type' => 'text/javascript']);
564
            }
565
        } else {
566 30
            if (!empty($this->js[self::POS_END])) {
567
                $lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']);
568
            }
569 30
            if (!empty($this->js[self::POS_READY])) {
570
                $js = "jQuery(document).ready(function () {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
571
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
572
            }
573 30
            if (!empty($this->js[self::POS_LOAD])) {
574
                $js = "jQuery(window).on('load', function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});";
575
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
576
            }
577
        }
578
579 30
        return empty($lines) ? '' : implode("\n", $lines);
580
    }
581
}
582