Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
improved ClientScript for nginx-http-concat module

#Usage

Update your config file, following the the lines below

return array(
  ...
    
  	'components' => array(
 	...
	
		'clientScript' => array(
			'class' => 'ClientScript',
			'cssConcatPrefix' => array(
				'/css/',
			),
			'scriptConcatPrefix' => array(
				'/scripts/',
			),
 		),
	),
);

Be advise, each cssConcatPrefix and scriptConcatPrefix must end with /.

Update your nginx config following your config, each lines in cssConcatPrefix and scriptConcatPrefix must have a location {} config in server {} block, like the lines below

location /css/ {
	concat on;
}

location /scripts/ {
	concat on;
}

Bugs

in CClientScript::registerCssFile($url, $media) method, the $media property is not supported now

<?php
class ClientScript extends CClientScript
{
public $cssConcatPrefix = array();
public $scriptConcatPrefix = array();
private $_cssFileConcatList = array();
private $_scriptFileConcatList = array();
public function registerCssFile($url, $media = '')
{
if(in_array($url, $this->_cssFileConcatList)) {
return $this;
}
$baseUrl = Yii::app()->baseUrl;
foreach($this->cssConcatPrefix as $concatPrefix) {
$concatPrefix = $baseUrl . $concatPrefix;
if(strpos($url, $concatPrefix) === 0) {
$realUrl = substr($url, strlen($concatPrefix));
if(!isset($this->_cssFileConcatList[$concatPrefix])) {
$this->_cssFileConcatList[$concatPrefix] = array();
}
$this->_cssFileConcatList[$concatPrefix][] = $realUrl;
return $this;
}
}
return parent::registerCssFile($url, $media);
}
public function registerScriptFile($url, $position = null)
{
if($position == null) {
$position=$this->defaultScriptFilePosition;
}
if(!isset($this->_scriptFileConcatList[$position])) {
$this->_scriptFileConcatList[$position] = array();
}
$baseUrl = Yii::app()->baseUrl;
foreach($this->scriptConcatPrefix as $concatPrefix) {
$concatPrefix = $baseUrl . $concatPrefix;
if(strpos($url, $concatPrefix) === 0) {
$realUrl = substr($url, strlen($concatPrefix));
if(!isset($this->_scriptFileConcatList[$position])) {
$this->_scriptFileConcatList[$position] = array();
}
if(!isset($this->_scriptFileConcatList[$position][$concatPrefix])) {
$this->_scriptFileConcatList[$position][$concatPrefix] = array();
}
if(in_array($realUrl, $this->_scriptFileConcatList[$position][$concatPrefix])) {
return $this;
}
$this->_scriptFileConcatList[$position][$concatPrefix][] = $realUrl;
return $this;
}
}
return parent::registerScriptFile($url, $position);
}
public function renderHead(&$output)
{
foreach($this->_cssFileConcatList as $urlPrefix => $items) {
$url = implode(',', $items);
parent::registerCssFile($urlPrefix . '??' . $url);
}
foreach($this->_scriptFileConcatList as $position => $concatList) {
foreach($concatList as $urlPrefix => $items) {
$url = implode(',', $items);
parent::registerScriptFile($urlPrefix . '??' . $url, $position);
}
}
return parent::renderHead($output);
}
}
@Desc1943

This comment has been minimized.

Show comment
Hide comment
@Desc1943

Desc1943 Apr 11, 2013

我只想说,下次要用到了。可以上这抄了。。哈哈

Desc1943 commented Apr 11, 2013

我只想说,下次要用到了。可以上这抄了。。哈哈

@francistm

This comment has been minimized.

Show comment
Hide comment
@francistm

francistm Apr 12, 2013

@Desc1943 那我先恭喜利哥发财啊!

Owner

francistm commented Apr 12, 2013

@Desc1943 那我先恭喜利哥发财啊!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment