From 62135070a9726b29fdddace174a62ef248873810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nick=20Dil=C3=9Fner?= Date: Mon, 6 Nov 2017 17:11:44 +0100 Subject: [PATCH 1/4] Code Version 1.0.0 + (add) source code --- .gitignore | 48 ++++++++ README.md | 79 ++++++++++++- .../UrlCacheControl/Helper/Data.php | 7 ++ .../Mage/Core/Model/Design/Package.php | 108 ++++++++++++++++++ .../UrlCacheControl/etc/config.xml | 35 ++++++ .../Kirchbergerknorr_UrlCacheControl.xml | 18 +++ composer.json | 9 ++ modman | 2 + 8 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 app/code/community/Kirchbergerknorr/UrlCacheControl/Helper/Data.php create mode 100644 app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php create mode 100644 app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml create mode 100644 app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml create mode 100644 composer.json create mode 100644 modman diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18131f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,48 @@ +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +.idea/ + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + diff --git a/README.md b/README.md index bd64d5a..1b07a5c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,80 @@ # Magento 1 - Url Cache Control -[will follow] \ No newline at end of file +Magento 1.x Extension + +## Overview + +This module appends the modified timestamp to skin url's, to automatically burst Browser Cache on changes. + +## Requirements and setup + +### Requirements + +Tested with Magento 1.9.3.6 + +This extension can be installed using [Composer](https://getcomposer.org/doc/01-basic-usage.md) + +### Setup + +Simple add repository to your composer.json + +````json +{ + "repositories": [ + {"type": "git", "url": "https://github.com/kirchbergerknorr/Magento1_UrlCacheControl.git"} + ], + "require": { + "kirchbergerknorr/magento1_url-cache-control": "^1.*" + } +} +```` + +## Details + +This Module just add a unix-timestamp parameter to each skin-url which uses + +````php +// skin +\Mage_Core_Model_Design_Package::getSkinUrl() + +// merged css +\Mage_Core_Model_Design_Package::getMergedCssUrl() + +// merged js +\Mage_Core_Model_Design_Package::getMergedJsUrl() +```` + +to get url's like that example: + +````html + + + + + + + + +```` + +By default there is no need to update `.htaccess`. + +### Configuration + +enable Module + +## Support + +If you have any problems with this extension, please open an [issue](https://github.com/kirchbergerknorr/Magento1_UrlCacheControl/issues). + +## Contribution + +Any contribution is highly appreciated. The best way to contribute is to [open a pull request](https://help.github.com/articles/about-pull-requests/). + +## Authors + +Nick Dilssner [nd@kirchbergerknorr.de](mailto:nd@kirchbergerknorr.de) + +## License + +[Open Software License (OSL 3.0)](http://opensource.org/licenses/osl-3.0.php) \ No newline at end of file diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/Helper/Data.php b/app/code/community/Kirchbergerknorr/UrlCacheControl/Helper/Data.php new file mode 100644 index 0000000..6e0f962 --- /dev/null +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/Helper/Data.php @@ -0,0 +1,7 @@ + + * @copyright Copyright (c) 2017 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +class Kirchbergerknorr_UrlCacheControl_Helper_Data extends Mage_Core_Helper_Abstract {} \ No newline at end of file diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php b/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php new file mode 100644 index 0000000..1d0d272 --- /dev/null +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php @@ -0,0 +1,108 @@ + + * @copyright Copyright (c) 2017 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +class Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package extends Mage_Core_Model_Design_Package +{ + /** + * parameter _type + */ + const SKIN = 'skin'; + + /** + * add modified timestamp to url + * + * {@inheritdoc} + */ + public function getSkinUrl($file = null, array $params = array()) + { + if (empty($params['_type'])) { + // not sure if this is really the best, because that means if i would set a type, this method also get other url's the skin + $params['_type'] = self::SKIN; + } + + $this->updateParamDefaults($params); + $filename = $this->getFilename($file, $params); + + return $this->addModifiedParam( + parent::getSkinUrl($file, $params), + $filename + ); + } + + /** + * add modified timestamp to url + * + * {@inheritdoc} + */ + public function getMergedCssUrl($files) + { + // getRequest()->isSecure(); + $mergerDir = $isSecure ? 'css_secure' : 'css'; + $targetDir = $this->_initMergerDir($mergerDir); + if (!$targetDir) { + return ''; + } + + // base hostname & port + $baseMediaUrl = Mage::getBaseUrl('media', $isSecure); + $hostname = parse_url($baseMediaUrl, PHP_URL_HOST); + $port = parse_url($baseMediaUrl, PHP_URL_PORT); + if (false === $port) { + $port = $isSecure ? 443 : 80; + } + + // merge into target file + $targetFilename = md5(implode(',', $files) . "|{$hostname}|{$port}") . '.css'; + // COPY> + + $filename = $targetDir . DS . $targetFilename; + return $this->addModifiedParam( + parent::getMergedCssUrl($files), + $filename + ); + } + + /** + * add modified timestamp to url + * + * {@inheritdoc} + */ + public function getMergedJsUrl($files) + { + // _initMergerDir('js'); + if (!$targetDir) { + return ''; + } + // COPY> + + $filename = $targetDir . DS . $targetFilename; + return $this->addModifiedParam( + parent::getMergedJsUrl($files), + $filename + ); + } + + /** + * add modified timestamp of existing file to url + * + * @param string $url url to add + * @param string $filename path to file of url + * + * @return string url with modified timestamp of file, if exists + */ + protected function addModifiedParam($url, $filename) + { + // TODO: validate if filemtime is also usable with url and if this is not calling the web-server + // file_exists and filemtime uses the same cache, so it is better to validate instead of using @filemtime + if (file_exists($filename) && is_file($filename) && ($time = filemtime($filename)) !== false) { + return $url . ((strpos($url, '?') === false) ? '?' : '&' ) . $time; + } + return $url; + } +} \ No newline at end of file diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml b/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml new file mode 100644 index 0000000..211c257 --- /dev/null +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml @@ -0,0 +1,35 @@ + + + + + + + 1.0.0 + + + + + + Kirchbergerknorr_UrlCacheControl_Helper + + + + Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package + + + + + + + Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package + + + + + \ No newline at end of file diff --git a/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml b/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml new file mode 100644 index 0000000..61f795f --- /dev/null +++ b/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml @@ -0,0 +1,18 @@ + + + + + + + true + community + + + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9640303 --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "kirchbergerknorr/magento1_url-cache-control", + "type": "magento-module", + "description": "Append last modified timestamp to skin url's to automatically burst cache on changes.", + "minimum-stability": "dev", + "authors": [ + {"name": "Nick Dilßner", "email": "nd@kirchbergerknorr.de"} + ] +} \ No newline at end of file diff --git a/modman b/modman new file mode 100644 index 0000000..2abacc1 --- /dev/null +++ b/modman @@ -0,0 +1,2 @@ +app/code/community/Kirchbergerknorr/UrlCacheControl app/code/community/Kirchbergerknorr/UrlCacheControl +app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml \ No newline at end of file From c1020d6f18e71a7eb4742cb7b50788a0e90260a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nick=20Dil=C3=9Fner?= Date: Wed, 8 Nov 2017 12:14:23 +0100 Subject: [PATCH 2/4] Code Version 1.0.0 + (add) copy comment to getSkinUrl + (optimized) unneeded duplicated call of updateParamDefaults + (changed) renaming module - (fix) typos in readme --- README.md | 18 ++---------------- .../Mage/Core/Model/Design/Package.php | 4 +++- .../UrlCacheControl/etc/config.xml | 13 ++++--------- .../Kirchbergerknorr_UrlCacheControl.xml | 5 ++--- 4 files changed, 11 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 1b07a5c..672a692 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Magento 1.x Extension ## Overview -This module appends the modified timestamp to skin url's, to automatically burst Browser Cache on changes. +This module appends the modified timestamp to skin URLs, to automatically bust Browser Cache on changes. ## Requirements and setup @@ -14,20 +14,6 @@ Tested with Magento 1.9.3.6 This extension can be installed using [Composer](https://getcomposer.org/doc/01-basic-usage.md) -### Setup - -Simple add repository to your composer.json - -````json -{ - "repositories": [ - {"type": "git", "url": "https://github.com/kirchbergerknorr/Magento1_UrlCacheControl.git"} - ], - "require": { - "kirchbergerknorr/magento1_url-cache-control": "^1.*" - } -} -```` ## Details @@ -44,7 +30,7 @@ This Module just add a unix-timestamp parameter to each skin-url which uses \Mage_Core_Model_Design_Package::getMergedJsUrl() ```` -to get url's like that example: +to get URLs like that example: ````html diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php b/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php index 1d0d272..b09d4b6 100644 --- a/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php @@ -18,13 +18,15 @@ class Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package extends Ma */ public function getSkinUrl($file = null, array $params = array()) { + // updateParamDefaults($params); $filename = $this->getFilename($file, $params); + // COPY> return $this->addModifiedParam( parent::getSkinUrl($file, $params), diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml b/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml index 211c257..c6463d2 100644 --- a/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml @@ -9,20 +9,15 @@ - + 1.0.0 - + - + Kirchbergerknorr_UrlCacheControl_Helper - - - - Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package - - + diff --git a/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml b/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml index 61f795f..448a696 100644 --- a/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml +++ b/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml @@ -1,7 +1,6 @@ - + 1.0.0 - + - + Kirchbergerknorr_UrlCacheControl_Helper - + diff --git a/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml b/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml index 448a696..dc78d31 100644 --- a/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml +++ b/app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml @@ -2,16 +2,16 @@ - + true community - + \ No newline at end of file From 4039c8272352a148917ed9c5ee3ba674afdcee0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nick=20Dil=C3=9Fner?= Date: Fri, 12 Jan 2018 12:47:03 +0100 Subject: [PATCH 4/4] Code Version 1.0.0 - review changes + (renamed) class and scope + (changed) ' }/Model/Design/Package.php | 22 +++++++++++-------- .../UrlCacheControl/etc/config.xml | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) rename app/code/community/Kirchbergerknorr/UrlCacheControl/{Mage/Core => }/Model/Design/Package.php (76%) diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php b/app/code/community/Kirchbergerknorr/UrlCacheControl/Model/Design/Package.php similarity index 76% rename from app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php rename to app/code/community/Kirchbergerknorr/UrlCacheControl/Model/Design/Package.php index 16c6dcd..db1cb1b 100644 --- a/app/code/community/Kirchbergerknorr/UrlCacheControl/Mage/Core/Model/Design/Package.php +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/Model/Design/Package.php @@ -4,7 +4,7 @@ * @copyright Copyright (c) 2018 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ -class Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package extends Mage_Core_Model_Design_Package +class Kirchbergerknorr_UrlCacheControl_Model_Design_Package extends Mage_Core_Model_Design_Package { /** * parameter _type @@ -18,20 +18,21 @@ class Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package extends Ma */ public function getSkinUrl($file = null, array $params = array()) { - // getFilename($file, $params); - // COPY> + // kk> + // addModifiedParam( parent::getSkinUrl($file, $params), $filename ); + // kk> } /** @@ -41,9 +42,10 @@ public function getSkinUrl($file = null, array $params = array()) */ public function getMergedCssUrl($files) { - // getRequest()->isSecure(); + // $targetDir = $this->_initMergerDir($mergerDir); if (!$targetDir) { return ''; @@ -59,13 +61,15 @@ public function getMergedCssUrl($files) // merge into target file $targetFilename = md5(implode(',', $files) . "|{$hostname}|{$port}") . '.css'; - // COPY> + // addModifiedParam( parent::getMergedCssUrl($files), $filename ); + // kk> } /** @@ -75,19 +79,19 @@ public function getMergedCssUrl($files) */ public function getMergedJsUrl($files) { - // _initMergerDir('js'); if (!$targetDir) { return ''; } - // COPY> + // addModifiedParam( parent::getMergedJsUrl($files), $filename ); + // kk> } /** diff --git a/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml b/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml index 93d7878..bca53f2 100644 --- a/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml +++ b/app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml @@ -22,7 +22,7 @@ - Kirchbergerknorr_UrlCacheControl_Mage_Core_Model_Design_Package + Kirchbergerknorr_UrlCacheControl_Model_Design_Package