| Total Complexity | 66 |
| Total Lines | 673 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WebView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class WebView extends View |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * The location of registered JavaScript code block or files. |
||
| 38 | * This means the location is in the head section. |
||
| 39 | */ |
||
| 40 | public const POS_HEAD = 1; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The location of registered JavaScript code block or files. |
||
| 44 | * This means the location is at the beginning of the body section. |
||
| 45 | */ |
||
| 46 | public const POS_BEGIN = 2; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The location of registered JavaScript code block or files. |
||
| 50 | * This means the location is at the end of the body section. |
||
| 51 | */ |
||
| 52 | public const POS_END = 3; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The location of registered JavaScript code block. |
||
| 56 | * This means the JavaScript code block will be executed when HTML document composition is ready. |
||
| 57 | */ |
||
| 58 | public const POS_READY = 4; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The location of registered JavaScript code block. |
||
| 62 | * This means the JavaScript code block will be executed when HTML page is completely loaded. |
||
| 63 | */ |
||
| 64 | public const POS_LOAD = 5; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * This is internally used as the placeholder for receiving the content registered for the head section. |
||
| 68 | */ |
||
| 69 | private const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * This is internally used as the placeholder for receiving the content registered for the beginning of the body |
||
| 73 | * section. |
||
| 74 | */ |
||
| 75 | private const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * This is internally used as the placeholder for receiving the content registered for the end of the body section. |
||
| 79 | */ |
||
| 80 | private const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values |
||
| 84 | * are the registered {@see AssetBundle} objects. |
||
| 85 | * |
||
| 86 | * {@see registerAssetBundle()} |
||
| 87 | */ |
||
| 88 | private $assetBundles = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Undocumented variable |
||
| 92 | * |
||
| 93 | * @var AssetManager $assetManager |
||
| 94 | */ |
||
| 95 | private $assetManager; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string the page title |
||
| 99 | */ |
||
| 100 | private $title; |
||
|
|
|||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array the registered meta tags. |
||
| 104 | * |
||
| 105 | * {@see registerMetaTag()} |
||
| 106 | */ |
||
| 107 | private $metaTags = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array the registered link tags. |
||
| 111 | * |
||
| 112 | * {@see registerLinkTag()} |
||
| 113 | */ |
||
| 114 | private $linkTags = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array the registered CSS code blocks. |
||
| 118 | * |
||
| 119 | * {@see registerCss()} |
||
| 120 | */ |
||
| 121 | private $css = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var array the registered CSS files. |
||
| 125 | * |
||
| 126 | * {@see registerCssFile()} |
||
| 127 | */ |
||
| 128 | private $cssFiles = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var array the registered JS code blocks |
||
| 132 | * |
||
| 133 | * {@see registerJs()} |
||
| 134 | */ |
||
| 135 | private $js = []; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var array the registered JS files. |
||
| 139 | * |
||
| 140 | * {@see registerJsFile()} |
||
| 141 | */ |
||
| 142 | private $jsFiles = []; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Marks the position of an HTML head section. |
||
| 146 | */ |
||
| 147 | public function head(): void |
||
| 148 | { |
||
| 149 | echo self::PH_HEAD; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Marks the beginning of an HTML body section. |
||
| 154 | */ |
||
| 155 | public function beginBody(): void |
||
| 156 | { |
||
| 157 | echo self::PH_BODY_BEGIN; |
||
| 158 | $this->eventDispatcher->dispatch(new BodyBegin($this->getViewFile())); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Marks the ending of an HTML body section. |
||
| 163 | */ |
||
| 164 | public function endBody(): void |
||
| 165 | { |
||
| 166 | $this->eventDispatcher->dispatch(new BodyEnd($this->getViewFile())); |
||
| 167 | echo self::PH_BODY_END; |
||
| 168 | |||
| 169 | foreach (array_keys($this->assetBundles) as $bundle) { |
||
| 170 | $this->registerAssetFiles($bundle); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Marks the ending of an HTML page. |
||
| 176 | * |
||
| 177 | * @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at |
||
| 178 | * {@see POS_READY} and {@see POS_LOAD} positions will be rendered at the end of the view like |
||
| 179 | * normal scripts. |
||
| 180 | */ |
||
| 181 | public function endPage($ajaxMode = false): void |
||
| 182 | { |
||
| 183 | $this->eventDispatcher->dispatch(new PageEnd($this->getViewFile())); |
||
| 184 | |||
| 185 | $content = ob_get_clean(); |
||
| 186 | |||
| 187 | echo strtr($content, [ |
||
| 188 | self::PH_HEAD => $this->renderHeadHtml(), |
||
| 189 | self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), |
||
| 190 | self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), |
||
| 191 | ]); |
||
| 192 | |||
| 193 | $this->clear(); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Renders a view in response to an AJAX request. |
||
| 198 | * |
||
| 199 | * This method is similar to {@see render()} except that it will surround the view being rendered with the calls of |
||
| 200 | * {@see beginPage()}, {@see head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the |
||
| 201 | * method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the |
||
| 202 | * view. |
||
| 203 | * |
||
| 204 | * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. |
||
| 205 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view |
||
| 206 | * file. |
||
| 207 | * @param object $context the context that the view should use for rendering the view. If null, existing [[context]] |
||
| 208 | * will be used. |
||
| 209 | * |
||
| 210 | * @return string the rendering result |
||
| 211 | * |
||
| 212 | * {@see render()} |
||
| 213 | */ |
||
| 214 | public function renderAjax(string $view, array $params = [], $context = null): string |
||
| 215 | { |
||
| 216 | $viewFile = $this->findViewFile($view, $context); |
||
| 217 | |||
| 218 | ob_start(); |
||
| 219 | ob_implicit_flush(0); |
||
| 220 | |||
| 221 | $this->beginPage(); |
||
| 222 | $this->head(); |
||
| 223 | $this->beginBody(); |
||
| 224 | echo $this->renderFile($viewFile, $params, $context); |
||
| 225 | $this->endBody(); |
||
| 226 | $this->endPage(true); |
||
| 227 | |||
| 228 | return ob_get_clean(); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Registers the asset manager being used by this view object. |
||
| 233 | * |
||
| 234 | * @return array the asset manager. Defaults to the "assetManager" application component. |
||
| 235 | */ |
||
| 236 | public function getAssetBundles(): array |
||
| 237 | { |
||
| 238 | return $this->assetBundles; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Registers the asset manager being used by this view object. |
||
| 243 | * |
||
| 244 | * @return AssetManager the asset manager. Defaults to the "assetManager" application component. |
||
| 245 | */ |
||
| 246 | public function getAssetManager(): AssetManager |
||
| 247 | { |
||
| 248 | return $this->assetManager; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Sets the asset manager. |
||
| 253 | * |
||
| 254 | * @param AssetManager $value the asset manager |
||
| 255 | * |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | public function setAssetManager(AssetManager $value): void |
||
| 259 | { |
||
| 260 | $this->assetManager = $value; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Clears up the registered meta tags, link tags, css/js scripts and files. |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | public function clear(): void |
||
| 269 | { |
||
| 270 | $this->metaTags = []; |
||
| 271 | $this->linkTags = []; |
||
| 272 | $this->css = []; |
||
| 273 | $this->cssFiles = []; |
||
| 274 | $this->js = []; |
||
| 275 | $this->jsFiles = []; |
||
| 276 | $this->assetBundles = []; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Registers all files provided by an asset bundle including depending bundles files. |
||
| 281 | * |
||
| 282 | * Removes a bundle from {@see assetBundles} once files are registered. |
||
| 283 | * |
||
| 284 | * @param string $name name of the bundle to register |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | protected function registerAssetFiles(string $name): void |
||
| 289 | { |
||
| 290 | if (!isset($this->assetBundles[$name])) { |
||
| 291 | return; |
||
| 292 | } |
||
| 293 | |||
| 294 | $bundle = $this->assetBundles[$name]; |
||
| 295 | |||
| 296 | if ($bundle) { |
||
| 297 | foreach ($bundle->depends as $dep) { |
||
| 298 | $this->registerAssetFiles($dep); |
||
| 299 | } |
||
| 300 | $bundle->registerAssetFiles($this); |
||
| 301 | } |
||
| 302 | |||
| 303 | unset($this->assetBundles[$name]); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Registers the named asset bundle. |
||
| 308 | * |
||
| 309 | * All dependent asset bundles will be registered. |
||
| 310 | * |
||
| 311 | * @param string $name the class name of the asset bundle (without the leading backslash) |
||
| 312 | * @param int|null $position if set, this forces a minimum position for javascript files. This will adjust depending |
||
| 313 | * assets javascript file position or fail if requirement can not be met. If this is null, asset |
||
| 314 | * bundles position settings will not be changed. |
||
| 315 | * |
||
| 316 | * {@see registerJsFile()} for more details on javascript position. |
||
| 317 | * |
||
| 318 | * @throws \RuntimeException if the asset bundle does not exist or a circular dependency is detected |
||
| 319 | * |
||
| 320 | * @return AssetBundle the registered asset bundle instance |
||
| 321 | */ |
||
| 322 | public function registerAssetBundle(string $name, ?int $position = null): AssetBundle |
||
| 323 | { |
||
| 324 | if (!isset($this->assetBundles[$name])) { |
||
| 325 | $am = $this->getAssetManager(); |
||
| 326 | $bundle = $am->getBundle($name); |
||
| 327 | |||
| 328 | $this->assetBundles[$name] = false; |
||
| 329 | |||
| 330 | // register dependencies |
||
| 331 | |||
| 332 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
||
| 333 | |||
| 334 | foreach ($bundle->depends as $dep) { |
||
| 335 | $this->registerAssetBundle($dep, $pos); |
||
| 336 | } |
||
| 337 | |||
| 338 | $this->assetBundles[$name] = $bundle; |
||
| 339 | } elseif ($this->assetBundles[$name] === false) { |
||
| 340 | throw new \RuntimeException("A circular dependency is detected for bundle '$name'."); |
||
| 341 | } else { |
||
| 342 | $bundle = $this->assetBundles[$name]; |
||
| 343 | } |
||
| 344 | |||
| 345 | if ($position !== null) { |
||
| 346 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
||
| 347 | |||
| 348 | if ($pos === null) { |
||
| 349 | $bundle->jsOptions['position'] = $pos = $position; |
||
| 350 | } elseif ($pos > $position) { |
||
| 351 | throw new \RuntimeException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); |
||
| 352 | } |
||
| 353 | |||
| 354 | // update position for all dependencies |
||
| 355 | foreach ($bundle->depends as $dep) { |
||
| 356 | $this->registerAssetBundle($dep, $pos); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | return $bundle; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Registers a meta tag. |
||
| 365 | * |
||
| 366 | * For example, a description meta tag can be added like the following: |
||
| 367 | * |
||
| 368 | * ```php |
||
| 369 | * $view->registerMetaTag([ |
||
| 370 | * 'name' => 'description', |
||
| 371 | * 'content' => 'This website is about funny raccoons.' |
||
| 372 | * ]); |
||
| 373 | * ``` |
||
| 374 | * |
||
| 375 | * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`. |
||
| 376 | * |
||
| 377 | * @param array $options the HTML attributes for the meta tag. |
||
| 378 | * @param string $key the key that identifies the meta tag. If two meta tags are registered with the same key, the |
||
| 379 | * latter will overwrite the former. If this is null, the new meta tag will be appended to the |
||
| 380 | * existing ones. |
||
| 381 | * |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | public function registerMetaTag(array $options, string $key = null): void |
||
| 385 | { |
||
| 386 | if ($key === null) { |
||
| 387 | $this->metaTags[] = Html::tag('meta', '', $options); |
||
| 388 | } else { |
||
| 389 | $this->metaTags[$key] = Html::tag('meta', '', $options); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Registers a link tag. |
||
| 395 | * |
||
| 396 | * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) can be added like the |
||
| 397 | * following: |
||
| 398 | * |
||
| 399 | * ```php |
||
| 400 | * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
||
| 401 | * ``` |
||
| 402 | * |
||
| 403 | * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`. |
||
| 404 | * |
||
| 405 | * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which has more options |
||
| 406 | * for this kind of link tag. |
||
| 407 | * |
||
| 408 | * @param array $options the HTML attributes for the link tag. |
||
| 409 | * @param string|null $key the key that identifies the link tag. If two link tags are registered with the same |
||
| 410 | * key, the latter will overwrite the former. If this is null, the new link tag will be appended |
||
| 411 | * to the existing ones. |
||
| 412 | */ |
||
| 413 | public function registerLinkTag(array $options, ?string $key = null): void |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Registers CSRF meta tags. |
||
| 424 | * |
||
| 425 | * They are rendered dynamically to retrieve a new CSRF token for each request. |
||
| 426 | * |
||
| 427 | * ```php |
||
| 428 | * $view->registerCsrfMetaTags(); |
||
| 429 | * ``` |
||
| 430 | * |
||
| 431 | * The above code will result in `<meta name="csrf-param" content="[Yiisoft\Web\Request::$csrfParam]">` and |
||
| 432 | * `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page. |
||
| 433 | * |
||
| 434 | * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()` |
||
| 435 | * from `yii.js`. |
||
| 436 | */ |
||
| 437 | public function registerCsrfMetaTags(): void |
||
| 438 | { |
||
| 439 | $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return Yiisoft\Html\Html::csrfMetaTags();'); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Registers a CSS code block. |
||
| 444 | * |
||
| 445 | * @param string $css the content of the CSS code block to be registered |
||
| 446 | * @param array $options the HTML attributes for the `<style>`-tag. |
||
| 447 | * @param string $key the key that identifies the CSS code block. If null, it will use $css as the key. If two CSS |
||
| 448 | * code blocks are registered with the same key, the latter will overwrite the former. |
||
| 449 | */ |
||
| 450 | public function registerCss(string $css, array $options = [], string $key = null): void |
||
| 451 | { |
||
| 452 | $key = $key ?: md5($css); |
||
| 453 | $this->css[$key] = Html::style($css, $options); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Registers a CSS file. |
||
| 458 | * |
||
| 459 | * This method should be used for simple registration of CSS files. If you want to use features of |
||
| 460 | * {@see AssetManager} like appending timestamps to the URL and file publishing options, use {@see AssetBundle} |
||
| 461 | * and {@see registerAssetBundle()} instead. |
||
| 462 | * |
||
| 463 | * @param string $url the CSS file to be registered. |
||
| 464 | * @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} |
||
| 465 | * for the supported options. The following options are specially handled and are not treated as HTML |
||
| 466 | * attributes: |
||
| 467 | * |
||
| 468 | * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on. |
||
| 469 | * @param string $key the key that identifies the CSS script file. If null, it will use $url as the key. If two CSS |
||
| 470 | * files are registered with the same key, the latter will overwrite the former. |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function registerCssFile(string $url, array $options = [], string $key = null): void |
||
| 475 | { |
||
| 476 | $key = $key ?: $url; |
||
| 477 | |||
| 478 | $depends = ArrayHelper::remove($options, 'depends', []); |
||
| 479 | |||
| 480 | if (empty($depends)) { |
||
| 481 | $this->cssFiles[$key] = Html::cssFile($url, $options); |
||
| 482 | } else { |
||
| 483 | $bundle = $this->createBundle([ |
||
| 484 | 'baseUrl' => '', |
||
| 485 | 'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
||
| 486 | 'cssOptions' => $options, |
||
| 487 | 'depends' => (array) $depends, |
||
| 488 | ]); |
||
| 489 | $bundles[$key] = $bundle; |
||
| 490 | |||
| 491 | $this->registerAssetBundle($key); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Registers a JS code block. |
||
| 497 | * |
||
| 498 | * @param string $js the JS code block to be registered |
||
| 499 | * @param int $position the position at which the JS script tag should be inserted in a page. |
||
| 500 | * |
||
| 501 | * The possible values are: |
||
| 502 | * |
||
| 503 | * - [[POS_HEAD]]: in the head section |
||
| 504 | * - [[POS_BEGIN]]: at the beginning of the body section |
||
| 505 | * - [[POS_END]]: at the end of the body section. This is the default value. |
||
| 506 | * - [[POS_LOAD]]: executed when HTML page is completely loaded. |
||
| 507 | * - [[POS_READY]]: executed when HTML document composition is ready. |
||
| 508 | * |
||
| 509 | * @param string $key the key that identifies the JS code block. If null, it will use $js as the key. If two JS code |
||
| 510 | * blocks are registered with the same key, the latter will overwrite the former. |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | */ |
||
| 514 | public function registerJs(string $js, int $position = self::POS_END, string $key = null): void |
||
| 515 | { |
||
| 516 | $key = $key ?: md5($js); |
||
| 517 | $this->js[$position][$key] = $js; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Registers a JS file. |
||
| 522 | * |
||
| 523 | * This method should be used for simple registration of JS files. If you want to use features of |
||
| 524 | * {@see AssetManager} like appending timestamps to the URL and file publishing options, use {@see AssetBundle} |
||
| 525 | * and {@see registerAssetBundle()} instead. |
||
| 526 | * |
||
| 527 | * @param string $url the JS file to be registered. |
||
| 528 | * @param array $options the HTML attributes for the script tag. The following options are specially handled and |
||
| 529 | * are not treated as HTML attributes: |
||
| 530 | * |
||
| 531 | * - `depends`: array, specifies the names of the asset bundles that this JS file depends on. |
||
| 532 | * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
||
| 533 | * * [[POS_HEAD]]: in the head section |
||
| 534 | * * [[POS_BEGIN]]: at the beginning of the body section |
||
| 535 | * * [[POS_END]]: at the end of the body section. This is the default value. |
||
| 536 | * |
||
| 537 | * Please refer to {@see \Yiisoft\Html\Html::jsFile()} for other supported options. |
||
| 538 | * |
||
| 539 | * @param string $key the key that identifies the JS script file. If null, it will use $url as the key. If two JS |
||
| 540 | * files are registered with the same key at the same position, the latter will overwrite the former. |
||
| 541 | * Note that position option takes precedence, thus files registered with the same key, but different |
||
| 542 | * position option will not override each other. |
||
| 543 | * |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | public function registerJsFile(string $url, array $options = [], string $key = null): void |
||
| 547 | { |
||
| 548 | $key = $key ?: $url; |
||
| 549 | |||
| 550 | $depends = ArrayHelper::remove($options, 'depends', []); |
||
| 551 | |||
| 552 | if (empty($depends)) { |
||
| 553 | $position = ArrayHelper::remove($options, 'position', self::POS_END); |
||
| 554 | $this->jsFiles[$position][$key] = Html::jsFile($url, $options); |
||
| 555 | } else { |
||
| 556 | $bundle = $this->createBundle([ |
||
| 557 | 'baseUrl' => '', |
||
| 558 | 'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
||
| 559 | 'jsOptions' => $options, |
||
| 560 | 'depends' => (array) $depends, |
||
| 561 | ]); |
||
| 562 | $bundles[$key] = $bundle; |
||
| 563 | $this->registerAssetBundle($key); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated |
||
| 569 | * variable names. |
||
| 570 | * |
||
| 571 | * @param string $name Name of the variable |
||
| 572 | * @param array|string $value Value of the variable |
||
| 573 | * @param int $position the position in a page at which the JavaScript variable should be inserted. |
||
| 574 | * |
||
| 575 | * The possible values are: |
||
| 576 | * |
||
| 577 | * - [[POS_HEAD]]: in the head section. This is the default value. |
||
| 578 | * - [[POS_BEGIN]]: at the beginning of the body section. |
||
| 579 | * - [[POS_END]]: at the end of the body section. |
||
| 580 | * - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
||
| 581 | * Note that by using this position, the method will automatically register the jQuery js file. |
||
| 582 | * - [[POS_READY]]: enclosed within jQuery(document).ready(). |
||
| 583 | * Note that by using this position, the method will automatically register the jQuery js file. |
||
| 584 | */ |
||
| 585 | public function registerJsVar(string $name, $value, int $position = self::POS_HEAD): void |
||
| 586 | { |
||
| 587 | $js = sprintf('var %s = %s;', $name, \Yiisoft\Json\Json::htmlEncode($value)); |
||
| 588 | $this->registerJs($js, $position, $name); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Renders the content to be inserted in the head section. |
||
| 593 | * |
||
| 594 | * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
||
| 595 | * |
||
| 596 | * @return string the rendered content |
||
| 597 | */ |
||
| 598 | protected function renderHeadHtml(): string |
||
| 599 | { |
||
| 600 | $lines = []; |
||
| 601 | if (!empty($this->metaTags)) { |
||
| 602 | $lines[] = implode("\n", $this->metaTags); |
||
| 603 | } |
||
| 604 | |||
| 605 | if (!empty($this->linkTags)) { |
||
| 606 | $lines[] = implode("\n", $this->linkTags); |
||
| 607 | } |
||
| 608 | if (!empty($this->cssFiles)) { |
||
| 609 | $lines[] = implode("\n", $this->cssFiles); |
||
| 610 | } |
||
| 611 | if (!empty($this->css)) { |
||
| 612 | $lines[] = implode("\n", $this->css); |
||
| 613 | } |
||
| 614 | if (!empty($this->jsFiles[self::POS_HEAD])) { |
||
| 615 | $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]); |
||
| 616 | } |
||
| 617 | if (!empty($this->js[self::POS_HEAD])) { |
||
| 618 | $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD])); |
||
| 619 | } |
||
| 620 | |||
| 621 | return empty($lines) ? '' : implode("\n", $lines); |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Renders the content to be inserted at the beginning of the body section. |
||
| 626 | * |
||
| 627 | * The content is rendered using the registered JS code blocks and files. |
||
| 628 | * |
||
| 629 | * @return string the rendered content |
||
| 630 | */ |
||
| 631 | protected function renderBodyBeginHtml(): string |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Renders the content to be inserted at the end of the body section. |
||
| 646 | * |
||
| 647 | * The content is rendered using the registered JS code blocks and files. |
||
| 648 | * |
||
| 649 | * @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at |
||
| 650 | * [[POS_READY]] and [[POS_LOAD]] positions will be rendered at the end of the view like normal scripts. |
||
| 651 | * |
||
| 652 | * @return string the rendered content |
||
| 653 | */ |
||
| 654 | protected function renderBodyEndHtml(bool $ajaxMode): string |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @param array $options |
||
| 695 | * |
||
| 696 | * @return AssetBundle |
||
| 697 | */ |
||
| 698 | private function createBundle(array $options): AssetBundle |
||
| 699 | { |
||
| 707 | } |
||
| 708 | } |
||
| 709 |