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
|
10 |
|
public function head() |
138
|
|
|
{ |
139
|
10 |
|
echo self::PH_HEAD; |
140
|
10 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Marks the beginning of an HTML body section. |
144
|
|
|
*/ |
145
|
10 |
|
public function beginBody() |
146
|
|
|
{ |
147
|
10 |
|
echo self::PH_BODY_BEGIN; |
148
|
10 |
|
$this->trigger(self::EVENT_BEGIN_BODY); |
149
|
10 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Marks the ending of an HTML body section. |
153
|
|
|
*/ |
154
|
10 |
|
public function endBody() |
155
|
|
|
{ |
156
|
10 |
|
$this->trigger(self::EVENT_END_BODY); |
157
|
10 |
|
echo self::PH_BODY_END; |
158
|
|
|
|
159
|
10 |
|
foreach (array_keys($this->assetBundles) as $bundle) { |
160
|
10 |
|
$this->registerAssetFiles($bundle); |
161
|
10 |
|
} |
162
|
10 |
|
} |
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
|
10 |
|
public function endPage($ajaxMode = false) |
171
|
|
|
{ |
172
|
10 |
|
$this->trigger(self::EVENT_END_PAGE); |
173
|
|
|
|
174
|
10 |
|
$content = ob_get_clean(); |
175
|
|
|
|
176
|
10 |
|
echo strtr($content, [ |
177
|
10 |
|
self::PH_HEAD => $this->renderHeadHtml(), |
178
|
10 |
|
self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), |
179
|
10 |
|
self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), |
180
|
10 |
|
]); |
181
|
|
|
|
182
|
10 |
|
$this->clear(); |
183
|
10 |
|
} |
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); |
|
|
|
|
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
|
22 |
|
public function getAssetManager() |
222
|
|
|
{ |
223
|
22 |
|
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
|
39 |
|
public function setAssetManager($value) |
231
|
|
|
{ |
232
|
39 |
|
$this->_assetManager = $value; |
233
|
39 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Clears up the registered meta tags, link tags, css/js scripts and files. |
237
|
|
|
*/ |
238
|
10 |
|
public function clear() |
239
|
|
|
{ |
240
|
10 |
|
$this->metaTags = null; |
|
|
|
|
241
|
10 |
|
$this->linkTags = null; |
|
|
|
|
242
|
10 |
|
$this->css = null; |
|
|
|
|
243
|
10 |
|
$this->cssFiles = null; |
|
|
|
|
244
|
10 |
|
$this->js = null; |
|
|
|
|
245
|
10 |
|
$this->jsFiles = null; |
|
|
|
|
246
|
10 |
|
$this->assetBundles = []; |
247
|
10 |
|
} |
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
|
22 |
|
public function registerAssetBundle($name, $position = null) |
281
|
|
|
{ |
282
|
22 |
|
if (!isset($this->assetBundles[$name])) { |
283
|
21 |
|
$am = $this->getAssetManager(); |
284
|
21 |
|
$bundle = $am->getBundle($name); |
285
|
21 |
|
$this->assetBundles[$name] = false; |
286
|
|
|
// register dependencies |
287
|
21 |
|
$pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
288
|
21 |
|
foreach ($bundle->depends as $dep) { |
289
|
15 |
|
$this->registerAssetBundle($dep, $pos); |
290
|
20 |
|
} |
291
|
20 |
|
$this->assetBundles[$name] = $bundle; |
292
|
22 |
|
} elseif ($this->assetBundles[$name] === false) { |
293
|
1 |
|
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); |
294
|
|
|
} else { |
295
|
10 |
|
$bundle = $this->assetBundles[$name]; |
296
|
|
|
} |
297
|
|
|
|
298
|
21 |
|
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
|
21 |
|
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
|
8 |
|
public function registerCssFile($url, $options = [], $key = null) |
398
|
|
|
{ |
399
|
8 |
|
$url = Yii::getAlias($url); |
400
|
8 |
|
$key = $key ?: $url; |
401
|
8 |
|
$depends = ArrayHelper::remove($options, 'depends', []); |
402
|
|
|
|
403
|
8 |
|
if (empty($depends)) { |
404
|
8 |
|
$this->cssFiles[$key] = Html::cssFile($url, $options); |
|
|
|
|
405
|
8 |
|
} else { |
406
|
|
|
$this->getAssetManager()->bundles[$key] = new AssetBundle([ |
407
|
|
|
'baseUrl' => '', |
408
|
|
|
'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
409
|
|
|
'cssOptions' => $options, |
410
|
|
|
'depends' => (array) $depends, |
411
|
|
|
]); |
412
|
|
|
$this->registerAssetBundle($key); |
|
|
|
|
413
|
|
|
} |
414
|
8 |
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Registers a JS code block. |
418
|
|
|
* @param string $js the JS code block to be registered |
419
|
|
|
* @param integer $position the position at which the JS script tag should be inserted |
420
|
|
|
* in a page. The possible values are: |
421
|
|
|
* |
422
|
|
|
* - [[POS_HEAD]]: in the head section |
423
|
|
|
* - [[POS_BEGIN]]: at the beginning of the body section |
424
|
|
|
* - [[POS_END]]: at the end of the body section |
425
|
|
|
* - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
426
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
427
|
|
|
* - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. |
428
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
429
|
|
|
* |
430
|
|
|
* @param string $key the key that identifies the JS code block. If null, it will use |
431
|
|
|
* $js as the key. If two JS code blocks are registered with the same key, the latter |
432
|
|
|
* will overwrite the former. |
433
|
|
|
*/ |
434
|
|
|
public function registerJs($js, $position = self::POS_READY, $key = null) |
435
|
|
|
{ |
436
|
|
|
$key = $key ?: md5($js); |
437
|
|
|
$this->js[$position][$key] = $js; |
438
|
|
|
if ($position === self::POS_READY || $position === self::POS_LOAD) { |
439
|
|
|
JqueryAsset::register($this); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Registers a JS file. |
445
|
|
|
* @param string $url the JS file to be registered. |
446
|
|
|
* @param array $options the HTML attributes for the script tag. The following options are specially handled |
447
|
|
|
* and are not treated as HTML attributes: |
448
|
|
|
* |
449
|
|
|
* - `depends`: array, specifies the names of the asset bundles that this JS file depends on. |
450
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
451
|
|
|
* * [[POS_HEAD]]: in the head section |
452
|
|
|
* * [[POS_BEGIN]]: at the beginning of the body section |
453
|
|
|
* * [[POS_END]]: at the end of the body section. This is the default value. |
454
|
|
|
* |
455
|
|
|
* Please refer to [[Html::jsFile()]] for other supported options. |
456
|
|
|
* |
457
|
|
|
* @param string $key the key that identifies the JS script file. If null, it will use |
458
|
|
|
* $url as the key. If two JS files are registered with the same key, the latter |
459
|
|
|
* will overwrite the former. |
460
|
|
|
*/ |
461
|
10 |
|
public function registerJsFile($url, $options = [], $key = null) |
462
|
|
|
{ |
463
|
10 |
|
$url = Yii::getAlias($url); |
464
|
10 |
|
$key = $key ?: $url; |
465
|
10 |
|
$depends = ArrayHelper::remove($options, 'depends', []); |
466
|
|
|
|
467
|
10 |
|
if (empty($depends)) { |
468
|
10 |
|
$position = ArrayHelper::remove($options, 'position', self::POS_END); |
469
|
10 |
|
$this->jsFiles[$position][$key] = Html::jsFile($url, $options); |
|
|
|
|
470
|
10 |
|
} else { |
471
|
|
|
$this->getAssetManager()->bundles[$key] = new AssetBundle([ |
472
|
|
|
'baseUrl' => '', |
473
|
|
|
'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
474
|
|
|
'jsOptions' => $options, |
475
|
|
|
'depends' => (array) $depends, |
476
|
|
|
]); |
477
|
|
|
$this->registerAssetBundle($key); |
|
|
|
|
478
|
|
|
} |
479
|
10 |
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Renders the content to be inserted in the head section. |
483
|
|
|
* The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
484
|
|
|
* @return string the rendered content |
485
|
|
|
*/ |
486
|
10 |
|
protected function renderHeadHtml() |
487
|
|
|
{ |
488
|
10 |
|
$lines = []; |
489
|
10 |
|
if (!empty($this->metaTags)) { |
490
|
|
|
$lines[] = implode("\n", $this->metaTags); |
491
|
|
|
} |
492
|
|
|
|
493
|
10 |
|
if (!empty($this->linkTags)) { |
494
|
|
|
$lines[] = implode("\n", $this->linkTags); |
495
|
|
|
} |
496
|
10 |
|
if (!empty($this->cssFiles)) { |
497
|
8 |
|
$lines[] = implode("\n", $this->cssFiles); |
498
|
8 |
|
} |
499
|
10 |
|
if (!empty($this->css)) { |
500
|
|
|
$lines[] = implode("\n", $this->css); |
501
|
|
|
} |
502
|
10 |
|
if (!empty($this->jsFiles[self::POS_HEAD])) { |
503
|
2 |
|
$lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); |
504
|
2 |
|
} |
505
|
10 |
|
if (!empty($this->js[self::POS_HEAD])) { |
506
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']); |
507
|
|
|
} |
508
|
|
|
|
509
|
10 |
|
return empty($lines) ? '' : implode("\n", $lines); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Renders the content to be inserted at the beginning of the body section. |
514
|
|
|
* The content is rendered using the registered JS code blocks and files. |
515
|
|
|
* @return string the rendered content |
516
|
|
|
*/ |
517
|
10 |
|
protected function renderBodyBeginHtml() |
518
|
|
|
{ |
519
|
10 |
|
$lines = []; |
520
|
10 |
|
if (!empty($this->jsFiles[self::POS_BEGIN])) { |
521
|
2 |
|
$lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]); |
522
|
2 |
|
} |
523
|
10 |
|
if (!empty($this->js[self::POS_BEGIN])) { |
524
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]), ['type' => 'text/javascript']); |
525
|
|
|
} |
526
|
|
|
|
527
|
10 |
|
return empty($lines) ? '' : implode("\n", $lines); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Renders the content to be inserted at the end of the body section. |
532
|
|
|
* The content is rendered using the registered JS code blocks and files. |
533
|
|
|
* @param boolean $ajaxMode whether the view is rendering in AJAX mode. |
534
|
|
|
* If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
535
|
|
|
* will be rendered at the end of the view like normal scripts. |
536
|
|
|
* @return string the rendered content |
537
|
|
|
*/ |
538
|
10 |
|
protected function renderBodyEndHtml($ajaxMode) |
539
|
|
|
{ |
540
|
10 |
|
$lines = []; |
541
|
|
|
|
542
|
10 |
|
if (!empty($this->jsFiles[self::POS_END])) { |
543
|
6 |
|
$lines[] = implode("\n", $this->jsFiles[self::POS_END]); |
544
|
6 |
|
} |
545
|
|
|
|
546
|
10 |
|
if ($ajaxMode) { |
547
|
|
|
$scripts = []; |
548
|
|
|
if (!empty($this->js[self::POS_END])) { |
549
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_END]); |
550
|
|
|
} |
551
|
|
|
if (!empty($this->js[self::POS_READY])) { |
552
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_READY]); |
553
|
|
|
} |
554
|
|
|
if (!empty($this->js[self::POS_LOAD])) { |
555
|
|
|
$scripts[] = implode("\n", $this->js[self::POS_LOAD]); |
556
|
|
|
} |
557
|
|
|
if (!empty($scripts)) { |
558
|
|
|
$lines[] = Html::script(implode("\n", $scripts), ['type' => 'text/javascript']); |
559
|
|
|
} |
560
|
|
|
} else { |
561
|
10 |
|
if (!empty($this->js[self::POS_END])) { |
562
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']); |
563
|
|
|
} |
564
|
10 |
|
if (!empty($this->js[self::POS_READY])) { |
565
|
|
|
$js = "jQuery(document).ready(function () {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; |
566
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
567
|
|
|
} |
568
|
10 |
|
if (!empty($this->js[self::POS_LOAD])) { |
569
|
|
|
$js = "jQuery(window).load(function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});"; |
570
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
|
574
|
10 |
|
return empty($lines) ? '' : implode("\n", $lines); |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.