From fda123fdc197f7d1d054fa66d99d1acba5813817 Mon Sep 17 00:00:00 2001 From: ngolechinhan Date: Wed, 8 Jun 2016 18:22:58 +0700 Subject: [PATCH 1/5] Set 'auto' for appVersion will allow auto regenerate nls when sources changed --- src/nlac/NLSClientScript.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/nlac/NLSClientScript.php b/src/nlac/NLSClientScript.php index 8643f19..e592d05 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 = $this->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,8 @@ protected function _putnlscode() { $this->registerScript('fixDuplicateResources', $js, $this->nlsScriptPosition); } -} \ No newline at end of file + + private function getFileMTime($path){ + return filemtime($_SERVER['DOCUMENT_ROOT'].$path); + } +} From 8c341e6612afedff849a077caac80b2e9e75d562 Mon Sep 17 00:00:00 2001 From: ngolechinhan Date: Thu, 9 Jun 2016 08:44:30 +0700 Subject: [PATCH 2/5] Move to Utils --- src/nlac/NLSUtils.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/nlac/NLSUtils.php b/src/nlac/NLSUtils.php index adeb70d..aa72aae 100644 --- a/src/nlac/NLSUtils.php +++ b/src/nlac/NLSUtils.php @@ -19,5 +19,9 @@ public static function addUrlParams($url, $params) { public static function isAbsoluteUrl($url) { return preg_match('@^https?://@', $url); } + + public static function getFileMTime($path){ + return filemtime($_SERVER['DOCUMENT_ROOT'].$path); + } -} \ No newline at end of file +} From a306d516aeb52be223f0b52f3b77ac2e0893063d Mon Sep 17 00:00:00 2001 From: ngolechinhan Date: Thu, 9 Jun 2016 08:45:27 +0700 Subject: [PATCH 3/5] Update NLSClientScript.php --- src/nlac/NLSClientScript.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/nlac/NLSClientScript.php b/src/nlac/NLSClientScript.php index e592d05..4e20b40 100644 --- a/src/nlac/NLSClientScript.php +++ b/src/nlac/NLSClientScript.php @@ -234,7 +234,7 @@ 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 = $this->getFileMTime($url); + $ver = NLSUtils::getFileMTime($url); $this->appVersion = max($this->appVersion, $ver); return NLSUtils::addUrlParams($url, array('nlsver' => $ver)); } @@ -494,7 +494,4 @@ protected function _putnlscode() { $this->registerScript('fixDuplicateResources', $js, $this->nlsScriptPosition); } - private function getFileMTime($path){ - return filemtime($_SERVER['DOCUMENT_ROOT'].$path); - } } From eb0be74d3ac3b3484ba0eeb01709cb363564b90b Mon Sep 17 00:00:00 2001 From: ngolechinhan Date: Thu, 9 Jun 2016 09:57:34 +0700 Subject: [PATCH 4/5] Fix: trim #? trailing when prepare filepath --- src/nlac/NLSUtils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nlac/NLSUtils.php b/src/nlac/NLSUtils.php index aa72aae..14f8745 100644 --- a/src/nlac/NLSUtils.php +++ b/src/nlac/NLSUtils.php @@ -21,7 +21,9 @@ public static function isAbsoluteUrl($url) { } public static function getFileMTime($path){ - return filemtime($_SERVER['DOCUMENT_ROOT'].$path); + // remove trailing + $path = preg_replace('@[\?#].*$@', '', $path); + return file_exists($path) ? filemtime($_SERVER['DOCUMENT_ROOT'].$path) : 0; } } From 6643cd9ad516683a6ad3541f8ffb356e04f0dbb6 Mon Sep 17 00:00:00 2001 From: ngolechinhan Date: Thu, 9 Jun 2016 10:06:13 +0700 Subject: [PATCH 5/5] Update NLSUtils.php --- src/nlac/NLSUtils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nlac/NLSUtils.php b/src/nlac/NLSUtils.php index 14f8745..1b36254 100644 --- a/src/nlac/NLSUtils.php +++ b/src/nlac/NLSUtils.php @@ -21,9 +21,9 @@ public static function isAbsoluteUrl($url) { } public static function getFileMTime($path){ - // remove trailing - $path = preg_replace('@[\?#].*$@', '', $path); - return file_exists($path) ? filemtime($_SERVER['DOCUMENT_ROOT'].$path) : 0; + // append doc root and remove trailing + $path = $_SERVER['DOCUMENT_ROOT'].preg_replace('@[\?#].*$@', '', $path); + return file_exists($path) ? filemtime($path) : 0; } }