diff --git a/CHANGELOG b/CHANGELOG index 11bcea5a7..830df5c5e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ Version 1.1.8 work in progress ------------------------------ - Bug: Fixed a typo that may cause issue when setting custom script packages with baseUrl option for CClientScript (Qiang) - Enh #2262: Added warning log when a session fails to start by CHttpSession (Qiang) +- Enh #2268: Added CClientScript::getPackageBaseUrl() (Qiang) Version 1.1.7 March 27, 2011 ---------------------------- diff --git a/framework/web/CClientScript.php b/framework/web/CClientScript.php index 28d325aab..5910cb932 100644 --- a/framework/web/CClientScript.php +++ b/framework/web/CClientScript.php @@ -301,20 +301,9 @@ class CClientScript extends CApplicationComponent return; $cssFiles=array(); $jsFiles=array(); - $am=Yii::app()->getAssetManager(); foreach($this->coreScripts as $name=>$package) { - if(isset($package['baseUrl'])) - { - $baseUrl=$package['baseUrl']; - if($baseUrl==='' || $baseUrl[0]!=='/' && strpos($baseUrl,'://')===false) - $baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl; - $baseUrl=rtrim($baseUrl,'/'); - } - else if(isset($package['basePath'])) - $baseUrl=$am->publish(Yii::getPathOfAlias($package['basePath'])); - else - $baseUrl=$this->getCoreScriptUrl(); + $baseUrl=$this->getPackageBaseUrl($name); if(!empty($package['js'])) { foreach($package['js'] as $js) @@ -475,6 +464,34 @@ class CClientScript extends CApplicationComponent $this->_baseUrl=$value; } + /** + * Returns the base URL for a registered package with the specified name. + * If needed, this method may publish the assets of the package and returns the published base URL. + * @param string $name the package name + * @return string the base URL for the named package. False is returned if the package is not registered yet. + * @see registerPackage + * @since 1.1.8 + */ + public function getPackageBaseUrl($name) + { + if(!isset($this->coreScripts[$name])) + return false; + $package=$this->coreScripts[$name]; + if(isset($package['baseUrl'])) + { + $baseUrl=$package['baseUrl']; + if($baseUrl==='' || $baseUrl[0]!=='/' && strpos($baseUrl,'://')===false) + $baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl; + $baseUrl=rtrim($baseUrl,'/'); + } + else if(isset($package['basePath'])) + $baseUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias($package['basePath'])); + else + $baseUrl=$this->getCoreScriptUrl(); + + return $this->coreScripts[$name]['baseUrl']=$baseUrl; + } + /** * Registers a script package that is listed in {@link packages}. * This method is the same as {@link registerCoreScript}.