-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMultidomainClientScript.php
73 lines (64 loc) · 2.33 KB
/
MultidomainClientScript.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @author Borales <[email protected]>
*
* @property string $assetsBaseUrl Use it (Yii::app()->clientScript->assetsBaseUrl) instead of Yii::app()->request->baseUrl
*/
class MultidomainClientScript extends CClientScript {
/**
* @var bool Whether the multidomain assets are enabled
*/
public $enableMultidomainAssets = true;
/**
* @var string Subdomain name
*/
public $assetsSubdomain = "assets";
/**
* @var bool Whether to use multiple assets subdomains
*/
public $indexedAssetsSubdomain = false;
/**
* @param string $subDomainIndex
* @return string
*/
public function getAssetsBaseUrl($subDomainIndex="") {
$baseUrl = Yii::app()->request->baseUrl;
if($this->enableMultidomainAssets===false)
return $baseUrl;
$schema = Yii::app()->request->isSecureConnection?"https://":"http://";
$subDomainIndex = $this->indexedAssetsSubdomain?$subDomainIndex:"";
$baseUrl = $schema.$this->assetsSubdomain.$subDomainIndex.'.'.Yii::app()->request->serverName.$baseUrl;
return $baseUrl;
}
/**
* Renders the registered scripts.
* This method is called in {@link CController::render} when it finishes
* rendering content. CClientScript thus gets a chance to insert script tags
* at <code>head</code> and <code>body</code> sections in the HTML output.
* @param string $output the existing output that needs to be inserted with script tags
*/
public function render(&$output)
{
if($this->enableMultidomainAssets && $this->hasScripts) {
$this->renderCoreScripts();
$this->coreScripts=null;
$this->processAssetsUrl();
}
parent::render($output);
}
protected function processAssetsUrl() {
foreach($this->cssFiles as $file=>$media) {
if(strpos($file, '/') === 0) {
unset($this->cssFiles[$file]);
$this->cssFiles[$this->assetsBaseUrl.$file] = $media;
}
}
foreach($this->scriptFiles as $pos=>$scripts) {
foreach($scripts as $scriptName=>$script) {
if(strpos($script, '/') === 0) {
$this->scriptFiles[$pos][$scriptName] = $this->getAssetsBaseUrl($pos).$script;
}
}
}
}
}