1 | <?php |
||||||
2 | /** |
||||||
3 | * @link https://www.yiiframework.com/ |
||||||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||||||
5 | * @license https://www.yiiframework.com/license/ |
||||||
6 | */ |
||||||
7 | |||||||
8 | namespace yii\web; |
||||||
9 | |||||||
10 | use Yii; |
||||||
11 | use yii\base\InvalidConfigException; |
||||||
12 | use yii\helpers\ArrayHelper; |
||||||
13 | use yii\helpers\Html; |
||||||
14 | use yii\helpers\Url; |
||||||
15 | |||||||
16 | /** |
||||||
17 | * View represents a view object in the MVC pattern. |
||||||
18 | * |
||||||
19 | * View provides a set of methods (e.g. [[render()]]) for rendering purpose. |
||||||
20 | * |
||||||
21 | * View is configured as an application component in [[\yii\base\Application]] by default. |
||||||
22 | * You can access that instance via `Yii::$app->view`. |
||||||
23 | * |
||||||
24 | * You can modify its configuration by adding an array to your application config under `components` |
||||||
25 | * as it is shown in the following example: |
||||||
26 | * |
||||||
27 | * ```php |
||||||
28 | * 'view' => [ |
||||||
29 | * 'theme' => 'app\themes\MyTheme', |
||||||
30 | * 'renderers' => [ |
||||||
31 | * // you may add Smarty or Twig renderer here |
||||||
32 | * ] |
||||||
33 | * // ... |
||||||
34 | * ] |
||||||
35 | * ``` |
||||||
36 | * |
||||||
37 | * For more details and usage information on View, see the [guide article on views](guide:structure-views). |
||||||
38 | * |
||||||
39 | * @property \yii\web\AssetManager $assetManager The asset manager. Defaults to the "assetManager" application |
||||||
40 | * component. |
||||||
41 | * |
||||||
42 | * @author Qiang Xue <[email protected]> |
||||||
43 | * @since 2.0 |
||||||
44 | * |
||||||
45 | * @phpstan-type RegisterJsFileOptions array{ |
||||||
46 | * depends?: class-string[], |
||||||
47 | * position?: int, |
||||||
48 | * appendTimestamp?: boolean |
||||||
49 | * } |
||||||
50 | * |
||||||
51 | * @psalm-type RegisterJsFileOptions = array{ |
||||||
52 | * depends?: class-string[], |
||||||
53 | * position?: int, |
||||||
54 | * appendTimestamp?: boolean |
||||||
55 | * } |
||||||
56 | * |
||||||
57 | * @phpstan-type RegisterCssFileOptions array{ |
||||||
58 | * depends?: class-string[], |
||||||
59 | * appendTimestamp?: boolean |
||||||
60 | * } |
||||||
61 | * |
||||||
62 | * @psalm-type RegisterCssFileOptions = array{ |
||||||
63 | * depends?: class-string[], |
||||||
64 | * appendTimestamp?: boolean |
||||||
65 | * } |
||||||
66 | */ |
||||||
67 | class View extends \yii\base\View |
||||||
68 | { |
||||||
69 | /** |
||||||
70 | * @event Event an event that is triggered by [[beginBody()]]. |
||||||
71 | */ |
||||||
72 | const EVENT_BEGIN_BODY = 'beginBody'; |
||||||
73 | /** |
||||||
74 | * @event Event an event that is triggered by [[endBody()]]. |
||||||
75 | */ |
||||||
76 | const EVENT_END_BODY = 'endBody'; |
||||||
77 | /** |
||||||
78 | * The location of registered JavaScript code block or files. |
||||||
79 | * This means the location is in the head section. |
||||||
80 | */ |
||||||
81 | const POS_HEAD = 1; |
||||||
82 | /** |
||||||
83 | * The location of registered JavaScript code block or files. |
||||||
84 | * This means the location is at the beginning of the body section. |
||||||
85 | */ |
||||||
86 | const POS_BEGIN = 2; |
||||||
87 | /** |
||||||
88 | * The location of registered JavaScript code block or files. |
||||||
89 | * This means the location is at the end of the body section. |
||||||
90 | */ |
||||||
91 | const POS_END = 3; |
||||||
92 | /** |
||||||
93 | * The location of registered JavaScript code block. |
||||||
94 | * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`. |
||||||
95 | */ |
||||||
96 | const POS_READY = 4; |
||||||
97 | /** |
||||||
98 | * The location of registered JavaScript code block. |
||||||
99 | * This means the JavaScript code block will be enclosed within `jQuery(window).load()`. |
||||||
100 | */ |
||||||
101 | const POS_LOAD = 5; |
||||||
102 | /** |
||||||
103 | * This is internally used as the placeholder for receiving the content registered for the head section. |
||||||
104 | */ |
||||||
105 | const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>'; |
||||||
106 | /** |
||||||
107 | * This is internally used as the placeholder for receiving the content registered for the beginning of the body section. |
||||||
108 | */ |
||||||
109 | const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>'; |
||||||
110 | /** |
||||||
111 | * This is internally used as the placeholder for receiving the content registered for the end of the body section. |
||||||
112 | */ |
||||||
113 | const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>'; |
||||||
114 | |||||||
115 | /** |
||||||
116 | * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values |
||||||
117 | * are the registered [[AssetBundle]] objects. |
||||||
118 | * @see registerAssetBundle() |
||||||
119 | */ |
||||||
120 | public $assetBundles = []; |
||||||
121 | /** |
||||||
122 | * @var string the page title |
||||||
123 | */ |
||||||
124 | public $title; |
||||||
125 | /** |
||||||
126 | * @var array the registered meta tags. |
||||||
127 | * @see registerMetaTag() |
||||||
128 | */ |
||||||
129 | public $metaTags = []; |
||||||
130 | /** |
||||||
131 | * @var array the registered link tags. |
||||||
132 | * @see registerLinkTag() |
||||||
133 | */ |
||||||
134 | public $linkTags = []; |
||||||
135 | /** |
||||||
136 | * @var array the registered CSS code blocks. |
||||||
137 | * @see registerCss() |
||||||
138 | */ |
||||||
139 | public $css = []; |
||||||
140 | /** |
||||||
141 | * @var array the registered CSS files. |
||||||
142 | * @see registerCssFile() |
||||||
143 | */ |
||||||
144 | public $cssFiles = []; |
||||||
145 | |||||||
146 | /** |
||||||
147 | * @since 2.0.53 |
||||||
148 | * @var array the style tag options. |
||||||
149 | */ |
||||||
150 | public $styleOptions = []; |
||||||
151 | |||||||
152 | /** |
||||||
153 | * @var array the registered JS code blocks |
||||||
154 | * @see registerJs() |
||||||
155 | */ |
||||||
156 | public $js = []; |
||||||
157 | /** |
||||||
158 | * @var array the registered JS files. |
||||||
159 | * @see registerJsFile() |
||||||
160 | */ |
||||||
161 | public $jsFiles = []; |
||||||
162 | /** |
||||||
163 | * @since 2.0.50 |
||||||
164 | * @var array the script tag options. |
||||||
165 | */ |
||||||
166 | public $scriptOptions = []; |
||||||
167 | |||||||
168 | private $_assetManager; |
||||||
169 | |||||||
170 | |||||||
171 | /** |
||||||
172 | * Whether [[endPage()]] has been called and all files have been registered |
||||||
173 | * @var bool |
||||||
174 | * @since 2.0.44 |
||||||
175 | */ |
||||||
176 | protected $isPageEnded = false; |
||||||
177 | |||||||
178 | /** |
||||||
179 | * Marks the position of an HTML head section. |
||||||
180 | */ |
||||||
181 | 52 | public function head() |
|||||
182 | { |
||||||
183 | 52 | echo self::PH_HEAD; |
|||||
184 | } |
||||||
185 | |||||||
186 | /** |
||||||
187 | * Marks the beginning of an HTML body section. |
||||||
188 | */ |
||||||
189 | 52 | public function beginBody() |
|||||
190 | { |
||||||
191 | 52 | echo self::PH_BODY_BEGIN; |
|||||
192 | 52 | $this->trigger(self::EVENT_BEGIN_BODY); |
|||||
193 | } |
||||||
194 | |||||||
195 | /** |
||||||
196 | * Marks the ending of an HTML body section. |
||||||
197 | */ |
||||||
198 | 56 | public function endBody() |
|||||
199 | { |
||||||
200 | 56 | $this->trigger(self::EVENT_END_BODY); |
|||||
201 | 56 | echo self::PH_BODY_END; |
|||||
202 | |||||||
203 | 56 | foreach (array_keys($this->assetBundles) as $bundle) { |
|||||
204 | 15 | $this->registerAssetFiles($bundle); |
|||||
205 | } |
||||||
206 | } |
||||||
207 | |||||||
208 | /** |
||||||
209 | * Marks the ending of an HTML page. |
||||||
210 | * @param bool $ajaxMode whether the view is rendering in AJAX mode. |
||||||
211 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||||||
212 | * will be rendered at the end of the view like normal scripts. |
||||||
213 | */ |
||||||
214 | 56 | public function endPage($ajaxMode = false) |
|||||
215 | { |
||||||
216 | 56 | $this->trigger(self::EVENT_END_PAGE); |
|||||
217 | |||||||
218 | 56 | $this->isPageEnded = true; |
|||||
219 | |||||||
220 | 56 | $content = ob_get_clean(); |
|||||
221 | |||||||
222 | 56 | echo strtr($content, [ |
|||||
223 | 56 | self::PH_HEAD => $this->renderHeadHtml(), |
|||||
224 | 56 | self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), |
|||||
225 | 56 | self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), |
|||||
226 | 56 | ]); |
|||||
227 | |||||||
228 | 56 | $this->clear(); |
|||||
229 | } |
||||||
230 | |||||||
231 | /** |
||||||
232 | * Renders a view in response to an AJAX request. |
||||||
233 | * |
||||||
234 | * This method is similar to [[render()]] except that it will surround the view being rendered |
||||||
235 | * with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]]. |
||||||
236 | * By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files |
||||||
237 | * that are registered with the view. |
||||||
238 | * |
||||||
239 | * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. |
||||||
240 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||||||
241 | * @param object|null $context the context that the view should use for rendering the view. If null, |
||||||
242 | * existing [[context]] will be used. |
||||||
243 | * @return string the rendering result |
||||||
244 | * @see render() |
||||||
245 | */ |
||||||
246 | public function renderAjax($view, $params = [], $context = null) |
||||||
247 | { |
||||||
248 | $viewFile = $this->findViewFile($view, $context); |
||||||
249 | |||||||
250 | ob_start(); |
||||||
251 | ob_implicit_flush(false); |
||||||
252 | |||||||
253 | $this->beginPage(); |
||||||
254 | $this->head(); |
||||||
255 | $this->beginBody(); |
||||||
256 | echo $this->renderFile($viewFile, $params, $context); |
||||||
257 | $this->endBody(); |
||||||
258 | $this->endPage(true); |
||||||
259 | |||||||
260 | return ob_get_clean(); |
||||||
261 | } |
||||||
262 | |||||||
263 | /** |
||||||
264 | * Registers the asset manager being used by this view object. |
||||||
265 | * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component. |
||||||
266 | */ |
||||||
267 | 59 | public function getAssetManager() |
|||||
268 | { |
||||||
269 | 59 | return $this->_assetManager ?: Yii::$app->getAssetManager(); |
|||||
270 | } |
||||||
271 | |||||||
272 | /** |
||||||
273 | * Sets the asset manager. |
||||||
274 | * @param \yii\web\AssetManager $value the asset manager |
||||||
275 | */ |
||||||
276 | 82 | public function setAssetManager($value) |
|||||
277 | { |
||||||
278 | 82 | $this->_assetManager = $value; |
|||||
279 | } |
||||||
280 | |||||||
281 | /** |
||||||
282 | * Clears up the registered meta tags, link tags, css/js scripts and files. |
||||||
283 | */ |
||||||
284 | 58 | public function clear() |
|||||
285 | { |
||||||
286 | 58 | $this->metaTags = []; |
|||||
287 | 58 | $this->linkTags = []; |
|||||
288 | 58 | $this->css = []; |
|||||
289 | 58 | $this->cssFiles = []; |
|||||
290 | 58 | $this->js = []; |
|||||
291 | 58 | $this->jsFiles = []; |
|||||
292 | 58 | $this->assetBundles = []; |
|||||
293 | } |
||||||
294 | |||||||
295 | /** |
||||||
296 | * Registers all files provided by an asset bundle including depending bundles files. |
||||||
297 | * Removes a bundle from [[assetBundles]] once files are registered. |
||||||
298 | * @param string $name name of the bundle to register |
||||||
299 | */ |
||||||
300 | 15 | protected function registerAssetFiles($name) |
|||||
301 | { |
||||||
302 | 15 | if (!isset($this->assetBundles[$name])) { |
|||||
303 | 12 | return; |
|||||
304 | } |
||||||
305 | 15 | $bundle = $this->assetBundles[$name]; |
|||||
306 | 15 | if ($bundle) { |
|||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||
307 | 15 | foreach ($bundle->depends as $dep) { |
|||||
308 | 12 | $this->registerAssetFiles($dep); |
|||||
309 | } |
||||||
310 | 15 | $bundle->registerAssetFiles($this); |
|||||
311 | } |
||||||
312 | 15 | unset($this->assetBundles[$name]); |
|||||
313 | } |
||||||
314 | |||||||
315 | /** |
||||||
316 | * Registers the named asset bundle. |
||||||
317 | * All dependent asset bundles will be registered. |
||||||
318 | * @param string $name the class name of the asset bundle (without the leading backslash) |
||||||
319 | * @param int|null $position if set, this forces a minimum position for javascript files. |
||||||
320 | * This will adjust depending assets javascript file position or fail if requirement can not be met. |
||||||
321 | * If this is null, asset bundles position settings will not be changed. |
||||||
322 | * See [[registerJsFile]] for more details on javascript position. |
||||||
323 | * @return AssetBundle the registered asset bundle instance |
||||||
324 | * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected |
||||||
325 | */ |
||||||
326 | 35 | public function registerAssetBundle($name, $position = null) |
|||||
327 | { |
||||||
328 | 35 | if (!isset($this->assetBundles[$name])) { |
|||||
329 | 34 | $am = $this->getAssetManager(); |
|||||
330 | 34 | $bundle = $am->getBundle($name); |
|||||
331 | 34 | $this->assetBundles[$name] = false; |
|||||
332 | // register dependencies |
||||||
333 | 34 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
|||||
334 | 34 | foreach ($bundle->depends as $dep) { |
|||||
335 | 24 | $this->registerAssetBundle($dep, $pos); |
|||||
336 | } |
||||||
337 | 33 | $this->assetBundles[$name] = $bundle; |
|||||
338 | 19 | } elseif ($this->assetBundles[$name] === false) { |
|||||
0 ignored issues
–
show
|
|||||||
339 | 1 | throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); |
|||||
340 | } else { |
||||||
341 | 18 | $bundle = $this->assetBundles[$name]; |
|||||
342 | } |
||||||
343 | |||||||
344 | 34 | if ($position !== null) { |
|||||
345 | 12 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
|||||
346 | 12 | if ($pos === null) { |
|||||
347 | 12 | $bundle->jsOptions['position'] = $pos = $position; |
|||||
348 | 6 | } elseif ($pos > $position) { |
|||||
349 | 6 | throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); |
|||||
350 | } |
||||||
351 | // update position for all dependencies |
||||||
352 | 12 | foreach ($bundle->depends as $dep) { |
|||||
353 | 6 | $this->registerAssetBundle($dep, $pos); |
|||||
354 | } |
||||||
355 | } |
||||||
356 | |||||||
357 | 34 | return $bundle; |
|||||
358 | } |
||||||
359 | |||||||
360 | /** |
||||||
361 | * Registers a meta tag. |
||||||
362 | * |
||||||
363 | * For example, a description meta tag can be added like the following: |
||||||
364 | * |
||||||
365 | * ```php |
||||||
366 | * $view->registerMetaTag([ |
||||||
367 | * 'name' => 'description', |
||||||
368 | * 'content' => 'This website is about funny raccoons.' |
||||||
369 | * ]); |
||||||
370 | * ``` |
||||||
371 | * |
||||||
372 | * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`. |
||||||
373 | * |
||||||
374 | * @param array $options the HTML attributes for the meta tag. |
||||||
375 | * @param string|null $key the key that identifies the meta tag. If two meta tags are registered |
||||||
376 | * with the same key, the latter will overwrite the former. If this is null, the new meta tag |
||||||
377 | * will be appended to the existing ones. |
||||||
378 | */ |
||||||
379 | public function registerMetaTag($options, $key = null) |
||||||
380 | { |
||||||
381 | if ($key === null) { |
||||||
382 | $this->metaTags[] = Html::tag('meta', '', $options); |
||||||
383 | } else { |
||||||
384 | $this->metaTags[$key] = Html::tag('meta', '', $options); |
||||||
385 | } |
||||||
386 | } |
||||||
387 | |||||||
388 | /** |
||||||
389 | * Registers CSRF meta tags. |
||||||
390 | * They are rendered dynamically to retrieve a new CSRF token for each request. |
||||||
391 | * |
||||||
392 | * ```php |
||||||
393 | * $view->registerCsrfMetaTags(); |
||||||
394 | * ``` |
||||||
395 | * |
||||||
396 | * The above code will result in `<meta name="csrf-param" content="[yii\web\Request::$csrfParam]">` |
||||||
397 | * and `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page. |
||||||
398 | * |
||||||
399 | * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()` |
||||||
400 | * from `yii.js`. |
||||||
401 | * |
||||||
402 | * @since 2.0.13 |
||||||
403 | */ |
||||||
404 | 1 | public function registerCsrfMetaTags() |
|||||
405 | { |
||||||
406 | 1 | $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return yii\helpers\Html::csrfMetaTags();'); |
|||||
407 | } |
||||||
408 | |||||||
409 | /** |
||||||
410 | * Registers a link tag. |
||||||
411 | * |
||||||
412 | * For example, a link tag for a custom [favicon](https://www.w3.org/2005/10/howto-favicon) |
||||||
413 | * can be added like the following: |
||||||
414 | * |
||||||
415 | * ```php |
||||||
416 | * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
||||||
417 | * ``` |
||||||
418 | * |
||||||
419 | * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`. |
||||||
420 | * |
||||||
421 | * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which |
||||||
422 | * has more options for this kind of link tag. |
||||||
423 | * |
||||||
424 | * @param array $options the HTML attributes for the link tag. |
||||||
425 | * @param string|null $key the key that identifies the link tag. If two link tags are registered |
||||||
426 | * with the same key, the latter will overwrite the former. If this is null, the new link tag |
||||||
427 | * will be appended to the existing ones. |
||||||
428 | */ |
||||||
429 | public function registerLinkTag($options, $key = null) |
||||||
430 | { |
||||||
431 | if ($key === null) { |
||||||
432 | $this->linkTags[] = Html::tag('link', '', $options); |
||||||
433 | } else { |
||||||
434 | $this->linkTags[$key] = Html::tag('link', '', $options); |
||||||
435 | } |
||||||
436 | } |
||||||
437 | |||||||
438 | /** |
||||||
439 | * Registers a CSS code block. |
||||||
440 | * @param string $css the content of the CSS code block to be registered |
||||||
441 | * @param array $options the HTML attributes for the `<style>`-tag. |
||||||
442 | * @param string|null $key the key that identifies the CSS code block. If null, it will use |
||||||
443 | * $css as the key. If two CSS code blocks are registered with the same key, the latter |
||||||
444 | * will overwrite the former. |
||||||
445 | */ |
||||||
446 | public function registerCss($css, $options = [], $key = null) |
||||||
447 | { |
||||||
448 | $key = $key ?: md5($css); |
||||||
449 | $this->css[$key] = Html::style($css, $options); |
||||||
450 | } |
||||||
451 | |||||||
452 | /** |
||||||
453 | * Registers a CSS file. |
||||||
454 | * |
||||||
455 | * This method should be used for simple registration of CSS files. If you want to use features of |
||||||
456 | * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]] |
||||||
457 | * and [[registerAssetBundle()]] instead. |
||||||
458 | * |
||||||
459 | * @param string $url the CSS file to be registered. |
||||||
460 | * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for |
||||||
461 | * the supported options. The following options are specially handled and are not treated as HTML attributes: |
||||||
462 | * |
||||||
463 | * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on. |
||||||
464 | * - `appendTimestamp`: bool whether to append a timestamp to the URL. |
||||||
465 | * |
||||||
466 | * @param string|null $key the key that identifies the CSS script file. If null, it will use |
||||||
467 | * $url as the key. If two CSS files are registered with the same key, the latter |
||||||
468 | * will overwrite the former. |
||||||
469 | * @throws InvalidConfigException |
||||||
470 | * |
||||||
471 | * @phpstan-param RegisterCssFileOptions $options |
||||||
472 | * @psalm-param RegisterCssFileOptions $options |
||||||
473 | */ |
||||||
474 | 20 | public function registerCssFile($url, $options = [], $key = null) |
|||||
475 | { |
||||||
476 | 20 | $this->registerFile('css', $url, $options, $key); |
|||||
477 | } |
||||||
478 | |||||||
479 | /** |
||||||
480 | * Registers a JS code block. |
||||||
481 | * @param string $js the JS code block to be registered |
||||||
482 | * @param int $position the position at which the JS script tag should be inserted |
||||||
483 | * in a page. The possible values are: |
||||||
484 | * |
||||||
485 | * - [[POS_HEAD]]: in the head section |
||||||
486 | * - [[POS_BEGIN]]: at the beginning of the body section |
||||||
487 | * - [[POS_END]]: at the end of the body section |
||||||
488 | * - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
||||||
489 | * Note that by using this position, the method will automatically register the jQuery js file. |
||||||
490 | * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. |
||||||
491 | * Note that by using this position, the method will automatically register the jQuery js file. |
||||||
492 | * |
||||||
493 | * @param string|null $key the key that identifies the JS code block. If null, it will use |
||||||
494 | * $js as the key. If two JS code blocks are registered with the same key, the latter |
||||||
495 | * will overwrite the former. |
||||||
496 | */ |
||||||
497 | 10 | public function registerJs($js, $position = self::POS_READY, $key = null) |
|||||
498 | { |
||||||
499 | 10 | $key = $key ?: md5($js); |
|||||
500 | 10 | $this->js[$position][$key] = $js; |
|||||
501 | 10 | if ($position === self::POS_READY || $position === self::POS_LOAD) { |
|||||
502 | 8 | JqueryAsset::register($this); |
|||||
503 | } |
||||||
504 | } |
||||||
505 | |||||||
506 | /** |
||||||
507 | * Registers a JS or CSS file. |
||||||
508 | * |
||||||
509 | * @param string $url the JS file to be registered. |
||||||
510 | * @param string $type type (js or css) of the file. |
||||||
511 | * @param array $options the HTML attributes for the script tag. The following options are specially handled |
||||||
512 | * and are not treated as HTML attributes: |
||||||
513 | * |
||||||
514 | * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on. |
||||||
515 | * - `appendTimestamp`: bool whether to append a timestamp to the URL. |
||||||
516 | * |
||||||
517 | * @param string|null $key the key that identifies the JS script file. If null, it will use |
||||||
518 | * $url as the key. If two JS files are registered with the same key at the same position, the latter |
||||||
519 | * will overwrite the former. Note that position option takes precedence, thus files registered with the same key, |
||||||
520 | * but different position option will not override each other. |
||||||
521 | * @throws InvalidConfigException |
||||||
522 | */ |
||||||
523 | 36 | private function registerFile($type, $url, $options = [], $key = null) |
|||||
524 | { |
||||||
525 | 36 | $url = Yii::getAlias($url); |
|||||
526 | 36 | $key = $key ?: $url; |
|||||
527 | 36 | $depends = ArrayHelper::remove($options, 'depends', []); |
|||||
528 | 36 | $originalOptions = $options; |
|||||
529 | 36 | $position = ArrayHelper::remove($options, 'position', self::POS_END); |
|||||
530 | |||||||
531 | try { |
||||||
532 | 36 | $assetManagerAppendTimestamp = $this->getAssetManager()->appendTimestamp; |
|||||
533 | } catch (InvalidConfigException $e) { |
||||||
534 | $depends = null; // the AssetManager is not available |
||||||
535 | $assetManagerAppendTimestamp = false; |
||||||
536 | } |
||||||
537 | 36 | $appendTimestamp = ArrayHelper::remove($options, 'appendTimestamp', $assetManagerAppendTimestamp); |
|||||
538 | |||||||
539 | 36 | if ($this->isPageEnded) { |
|||||
540 | Yii::warning('You\'re trying to register a file after View::endPage() has been called.'); |
||||||
541 | } |
||||||
542 | |||||||
543 | 36 | if (empty($depends)) { |
|||||
544 | // register directly without AssetManager |
||||||
545 | 36 | if ($appendTimestamp && Url::isRelative($url)) { |
|||||
0 ignored issues
–
show
It seems like
$url can also be of type false ; however, parameter $url of yii\helpers\BaseUrl::isRelative() 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
![]() |
|||||||
546 | 5 | $prefix = Yii::getAlias('@web'); |
|||||
547 | 5 | $prefixLength = strlen($prefix); |
|||||
0 ignored issues
–
show
It seems like
$prefix can also be of type false ; however, parameter $string of strlen() 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
![]() |
|||||||
548 | 5 | $trimmedUrl = ltrim((substr($url, 0, $prefixLength) === $prefix) ? substr($url, $prefixLength) : $url, '/'); |
|||||
0 ignored issues
–
show
It seems like
substr($url, 0, $prefixL..., $prefixLength) : $url can also be of type false ; however, parameter $string of ltrim() 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
![]() It seems like
$url can also be of type false ; however, parameter $string of substr() 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
![]() |
|||||||
549 | 5 | $timestamp = @filemtime(Yii::getAlias('@webroot/' . $trimmedUrl, false)); |
|||||
0 ignored issues
–
show
It seems like
Yii::getAlias('@webroot/' . $trimmedUrl, false) can also be of type false ; however, parameter $filename of filemtime() 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
![]() |
|||||||
550 | 5 | if ($timestamp > 0) { |
|||||
551 | 2 | $url = $timestamp ? "$url?v=$timestamp" : $url; |
|||||
552 | } |
||||||
553 | } |
||||||
554 | 36 | if ($type === 'js') { |
|||||
555 | 24 | $this->jsFiles[$position][$key] = Html::jsFile($url, $options); |
|||||
0 ignored issues
–
show
It seems like
$url can also be of type false ; however, parameter $url of yii\helpers\BaseHtml::jsFile() 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
![]() |
|||||||
556 | } else { |
||||||
557 | 36 | $this->cssFiles[$key] = Html::cssFile($url, $options); |
|||||
0 ignored issues
–
show
It seems like
$url can also be of type false ; however, parameter $url of yii\helpers\BaseHtml::cssFile() does only seem to accept array|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
![]() |
|||||||
558 | } |
||||||
559 | } else { |
||||||
560 | 4 | $this->getAssetManager()->bundles[$key] = Yii::createObject([ |
|||||
561 | 4 | 'class' => AssetBundle::class, |
|||||
562 | 4 | 'baseUrl' => '', |
|||||
563 | 4 | 'basePath' => '@webroot', |
|||||
564 | 4 | (string)$type => [ArrayHelper::merge([!Url::isRelative($url) ? $url : ltrim($url, '/')], $originalOptions)], |
|||||
565 | 4 | "{$type}Options" => $options, |
|||||
566 | 4 | 'depends' => (array)$depends, |
|||||
567 | 4 | ]); |
|||||
568 | 4 | $this->registerAssetBundle($key); |
|||||
0 ignored issues
–
show
It seems like
$key can also be of type false ; however, parameter $name of yii\web\View::registerAssetBundle() 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
![]() |
|||||||
569 | } |
||||||
570 | } |
||||||
571 | |||||||
572 | /** |
||||||
573 | * Registers a JS file. |
||||||
574 | * |
||||||
575 | * This method should be used for simple registration of JS files. If you want to use features of |
||||||
576 | * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]] |
||||||
577 | * and [[registerAssetBundle()]] instead. |
||||||
578 | * |
||||||
579 | * @param string $url the JS file to be registered. |
||||||
580 | * @param array $options the HTML attributes for the script tag. The following options are specially handled |
||||||
581 | * and are not treated as HTML attributes: |
||||||
582 | * |
||||||
583 | * - `depends`: array, specifies the names of the asset bundles that this JS file depends on. |
||||||
584 | * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
||||||
585 | * * [[POS_HEAD]]: in the head section |
||||||
586 | * * [[POS_BEGIN]]: at the beginning of the body section |
||||||
587 | * * [[POS_END]]: at the end of the body section. This is the default value. |
||||||
588 | * - `appendTimestamp`: bool whether to append a timestamp to the URL. |
||||||
589 | * |
||||||
590 | * Please refer to [[Html::jsFile()]] for other supported options. |
||||||
591 | * |
||||||
592 | * @param string|null $key the key that identifies the JS script file. If null, it will use |
||||||
593 | * $url as the key. If two JS files are registered with the same key at the same position, the latter |
||||||
594 | * will overwrite the former. Note that position option takes precedence, thus files registered with the same key, |
||||||
595 | * but different position option will not override each other. |
||||||
596 | * @throws InvalidConfigException |
||||||
597 | * |
||||||
598 | * @phpstan-param RegisterJsFileOptions $options |
||||||
599 | * @psalm-param RegisterJsFileOptions $options |
||||||
600 | */ |
||||||
601 | 24 | public function registerJsFile($url, $options = [], $key = null) |
|||||
602 | { |
||||||
603 | 24 | $this->registerFile('js', $url, $options, $key); |
|||||
604 | } |
||||||
605 | |||||||
606 | /** |
||||||
607 | * Registers a JS code block defining a variable. The name of variable will be |
||||||
608 | * used as key, preventing duplicated variable names. |
||||||
609 | * |
||||||
610 | * @param string $name Name of the variable |
||||||
611 | * @param array|string $value Value of the variable |
||||||
612 | * @param int $position the position in a page at which the JavaScript variable should be inserted. |
||||||
613 | * The possible values are: |
||||||
614 | * |
||||||
615 | * - [[POS_HEAD]]: in the head section. This is the default value. |
||||||
616 | * - [[POS_BEGIN]]: at the beginning of the body section. |
||||||
617 | * - [[POS_END]]: at the end of the body section. |
||||||
618 | * - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
||||||
619 | * Note that by using this position, the method will automatically register the jQuery js file. |
||||||
620 | * - [[POS_READY]]: enclosed within jQuery(document).ready(). |
||||||
621 | * Note that by using this position, the method will automatically register the jQuery js file. |
||||||
622 | * |
||||||
623 | * @since 2.0.14 |
||||||
624 | */ |
||||||
625 | 1 | public function registerJsVar($name, $value, $position = self::POS_HEAD) |
|||||
626 | { |
||||||
627 | 1 | $js = sprintf('var %s = %s;', $name, \yii\helpers\Json::htmlEncode($value)); |
|||||
628 | 1 | $this->registerJs($js, $position, $name); |
|||||
629 | } |
||||||
630 | |||||||
631 | /** |
||||||
632 | * Renders the content to be inserted in the head section. |
||||||
633 | * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
||||||
634 | * @return string the rendered content |
||||||
635 | */ |
||||||
636 | 56 | protected function renderHeadHtml() |
|||||
637 | { |
||||||
638 | 56 | $lines = []; |
|||||
639 | 56 | if (!empty($this->metaTags)) { |
|||||
640 | 1 | $lines[] = implode("\n", $this->metaTags); |
|||||
641 | } |
||||||
642 | |||||||
643 | 56 | if (!empty($this->linkTags)) { |
|||||
644 | $lines[] = implode("\n", $this->linkTags); |
||||||
645 | } |
||||||
646 | 56 | if (!empty($this->cssFiles)) { |
|||||
647 | 20 | $lines[] = implode("\n", $this->cssFiles); |
|||||
648 | } |
||||||
649 | 56 | if (!empty($this->css)) { |
|||||
650 | $lines[] = implode("\n", $this->css); |
||||||
651 | } |
||||||
652 | 56 | if (!empty($this->jsFiles[self::POS_HEAD])) { |
|||||
653 | 3 | $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); |
|||||
654 | } |
||||||
655 | 56 | if (!empty($this->js[self::POS_HEAD])) { |
|||||
656 | 1 | $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD])); |
|||||
657 | } |
||||||
658 | |||||||
659 | 56 | return empty($lines) ? '' : implode("\n", $lines); |
|||||
660 | } |
||||||
661 | |||||||
662 | /** |
||||||
663 | * Renders the content to be inserted at the beginning of the body section. |
||||||
664 | * The content is rendered using the registered JS code blocks and files. |
||||||
665 | * @return string the rendered content |
||||||
666 | */ |
||||||
667 | 56 | protected function renderBodyBeginHtml() |
|||||
668 | { |
||||||
669 | 56 | $lines = []; |
|||||
670 | 56 | if (!empty($this->jsFiles[self::POS_BEGIN])) { |
|||||
671 | 3 | $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]); |
|||||
672 | } |
||||||
673 | 56 | if (!empty($this->js[self::POS_BEGIN])) { |
|||||
674 | $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN])); |
||||||
675 | } |
||||||
676 | |||||||
677 | 56 | return empty($lines) ? '' : implode("\n", $lines); |
|||||
678 | } |
||||||
679 | |||||||
680 | /** |
||||||
681 | * Renders the content to be inserted at the end of the body section. |
||||||
682 | * The content is rendered using the registered JS code blocks and files. |
||||||
683 | * @param bool $ajaxMode whether the view is rendering in AJAX mode. |
||||||
684 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||||||
685 | * will be rendered at the end of the view like normal scripts. |
||||||
686 | * @return string the rendered content |
||||||
687 | */ |
||||||
688 | 56 | protected function renderBodyEndHtml($ajaxMode) |
|||||
689 | { |
||||||
690 | 56 | $lines = []; |
|||||
691 | |||||||
692 | 56 | if (!empty($this->jsFiles[self::POS_END])) { |
|||||
693 | 19 | $lines[] = implode("\n", $this->jsFiles[self::POS_END]); |
|||||
694 | } |
||||||
695 | |||||||
696 | 56 | if ($ajaxMode) { |
|||||
697 | $scripts = []; |
||||||
698 | if (!empty($this->js[self::POS_END])) { |
||||||
699 | $scripts[] = implode("\n", $this->js[self::POS_END]); |
||||||
700 | } |
||||||
701 | if (!empty($this->js[self::POS_READY])) { |
||||||
702 | $scripts[] = implode("\n", $this->js[self::POS_READY]); |
||||||
703 | } |
||||||
704 | if (!empty($this->js[self::POS_LOAD])) { |
||||||
705 | $scripts[] = implode("\n", $this->js[self::POS_LOAD]); |
||||||
706 | } |
||||||
707 | if (!empty($scripts)) { |
||||||
708 | $lines[] = Html::script(implode("\n", $scripts)); |
||||||
709 | } |
||||||
710 | } else { |
||||||
711 | 56 | if (!empty($this->js[self::POS_END])) { |
|||||
712 | $lines[] = Html::script(implode("\n", $this->js[self::POS_END])); |
||||||
713 | } |
||||||
714 | 56 | if (!empty($this->js[self::POS_READY])) { |
|||||
715 | $js = "jQuery(function ($) {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});"; |
||||||
716 | $lines[] = Html::script($js); |
||||||
717 | } |
||||||
718 | 56 | if (!empty($this->js[self::POS_LOAD])) { |
|||||
719 | $js = "jQuery(window).on('load', function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});"; |
||||||
720 | $lines[] = Html::script($js); |
||||||
721 | } |
||||||
722 | } |
||||||
723 | |||||||
724 | 56 | return empty($lines) ? '' : implode("\n", $lines); |
|||||
725 | } |
||||||
726 | } |
||||||
727 |