diff --git a/src/nlac/NLSClientScript.php b/src/nlac/NLSClientScript.php index 8643f19..4e20b40 100644 --- a/src/nlac/NLSClientScript.php +++ b/src/nlac/NLSClientScript.php @@ -145,6 +145,7 @@ class NLSClientScript extends \CClientScript { /** * @param string $appVersion * Optional, version of the application. + * Set 'auto' will allow auto regenerate when source changed * If set to not empty, will be appended to the merged js/css urls (helps to handle cached resources). **/ public $appVersion = ''; @@ -188,10 +189,16 @@ class NLSClientScript extends \CClientScript { protected $cssMerger = null; + protected $isAutoAppVersion = false; public function init() { parent::init(); + if ($this->appVersion == 'auto'){ + $this->isAutoAppVersion = true; + $this->appVersion = 0; + } + if (is_numeric($this->nlsScriptPosition)) //-> we need jquery $this->registerCoreScript('jquery'); @@ -224,6 +231,14 @@ public function init() { } protected function addAppVersion($url) { + // if auto version flag is set (appVersion='auto') + // get file modified time and use as version before hashing + if ($this->isAutoAppVersion && !NLSUtils::isAbsoluteUrl($url)) { + $ver = NLSUtils::getFileMTime($url); + $this->appVersion = max($this->appVersion, $ver); + return NLSUtils::addUrlParams($url, array('nlsver' => $ver)); + } + // original none auto if (!empty($this->appVersion) && !NLSUtils::isAbsoluteUrl($url)) $url = NLSUtils::addUrlParams($url, array('nlsver' => $this->appVersion)); return $url; @@ -478,4 +493,5 @@ protected function _putnlscode() { $this->registerScript('fixDuplicateResources', $js, $this->nlsScriptPosition); } -} \ No newline at end of file + +} diff --git a/src/nlac/NLSUtils.php b/src/nlac/NLSUtils.php index adeb70d..1b36254 100644 --- a/src/nlac/NLSUtils.php +++ b/src/nlac/NLSUtils.php @@ -19,5 +19,11 @@ public static function addUrlParams($url, $params) { public static function isAbsoluteUrl($url) { return preg_match('@^https?://@', $url); } + + public static function getFileMTime($path){ + // append doc root and remove trailing + $path = $_SERVER['DOCUMENT_ROOT'].preg_replace('@[\?#].*$@', '', $path); + return file_exists($path) ? filemtime($path) : 0; + } -} \ No newline at end of file +}