From 2951d97d8ff3f2fb1ee7e3d1e9ab0d41baf69304 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Mon, 6 May 2024 23:59:47 +0100 Subject: [PATCH] Initial commit --- .github/FUNDING.yml | 3 + README.md | 53 +++++++++++ .../Fballiano/CssjsMinify/Model/Observer.php | 89 +++++++++++++++++++ .../Fballiano/CssjsMinify/etc/config.xml | 39 ++++++++ app/etc/modules/Fballiano_CssjsMinify.xml | 9 ++ composer.json | 26 ++++++ modman | 2 + 7 files changed, 221 insertions(+) create mode 100644 .github/FUNDING.yml create mode 100644 README.md create mode 100644 app/code/community/Fballiano/CssjsMinify/Model/Observer.php create mode 100644 app/code/community/Fballiano/CssjsMinify/etc/config.xml create mode 100644 app/etc/modules/Fballiano_CssjsMinify.xml create mode 100644 composer.json create mode 100644 modman diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..d2eb54f --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +github: fballiano +buy_me_a_coffee: fballiano +custom: ["https://paypal.me/fabrizioballiano"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cab5766 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +CSS/JS Minify module for OpenMage/Magento1 +============================= + +
+If you find my work valuable, please consider sponsoring
+Sponsor me on GitHub +Buy me a coffee +Donate via PayPal +
+ +Quick description +--------- + +A modern solution to minify CSS/JS in your OpenMage project. + +Features +--------- + + + +Warning +--------- + +This module is provided "as is" and I'll not be responsible for any problem or damage. + +Installation +------------ + +Only composer installation is supported: +`composer require fballiano/openmage-cssjs-minify` + +Support +------- +If you have any issues with this extension, open an issue on GitHub. + +Contribution +------------ +Any contributions are highly appreciated. The best way to contribute code is to open a +[pull request on GitHub](https://help.github.com/articles/using-pull-requests). + +Developer +--------- +Fabrizio Balliano +[http://fabrizioballiano.com](http://fabrizioballiano.com) +[@fballiano](https://twitter.com/fballiano) + +Licence +------- +[OSL - Open Software Licence 3.0](https://opensource.org/license/osl-3) + +Copyright +--------- +(c) Fabrizio Balliano \ No newline at end of file diff --git a/app/code/community/Fballiano/CssjsMinify/Model/Observer.php b/app/code/community/Fballiano/CssjsMinify/Model/Observer.php new file mode 100644 index 0000000..f422e61 --- /dev/null +++ b/app/code/community/Fballiano/CssjsMinify/Model/Observer.php @@ -0,0 +1,89 @@ +getResponse(); + $html = $response->getBody(); + $baseDir = Mage::getBaseDir(); + $mediaDir = Mage::getBaseDir('media'); + $mediaUrl = Mage::getBaseUrl('media'); + $minifiedDir = "{$mediaDir}/" . self::MINIFIED_FILES_FOLDER . '/'; + $minifiedUrl = "{$mediaUrl}" . self::MINIFIED_FILES_FOLDER . '/'; + + if (!file_exists($minifiedDir)) { + mkdir($minifiedDir, 0755, true); + } + + // Process JS + $pattern = '/()/iU'; + $html = preg_replace_callback($pattern, function($matches) use ($baseDir, $minifiedDir, $minifiedUrl) { + $url = $matches[2]; + $urlComponents = parse_url($url); + $path = $urlComponents['path']; + if (file_exists($baseDir . $path)) { + $time = filemtime($baseDir . $path); + $hash = md5($path) . "-$time.js"; + if (!file_exists($minifiedDir . $hash)) { + $minifier = new \MatthiasMullie\Minify\JS($baseDir . $path); + $minifier->minify("$minifiedDir/$hash"); + } + $matches[2] = $minifiedUrl . $hash; + } + return $matches[1] . $matches[2] . $matches[3]; + }, $html); + + // Process CSS + $pattern = '/()/iU'; + $html = preg_replace_callback($pattern, function($matches) use ($baseDir, $minifiedDir, $minifiedUrl) { + $url = $matches[2]; + $urlComponents = parse_url($url); + $path = $urlComponents['path']; + if (file_exists($baseDir . $path)) { + $time = filemtime($baseDir . $path); + $hash = md5($path) . "-$time.css"; + if (!file_exists($minifiedDir . $hash)) { + $minifier = new \MatthiasMullie\Minify\CSS($baseDir . $path); + $minifier->minify("$minifiedDir/$hash"); + } + $matches[2] = $minifiedUrl . $hash; + } + return $matches[1] . $matches[2] . $matches[3]; + }, $html); + + $response->setBody($html); + } + + public function dailyCron(): void + { + $mediaDir = Mage::getBaseDir('media'); + $minifiedDir = "{$mediaDir}/" . self::MINIFIED_FILES_FOLDER; + + $files = scandir($minifiedDir, SCANDIR_SORT_DESCENDING); + $lastHash = null; + foreach ($files as $file) { + if ($file === '.' || $file === '..') { + continue; + } + + $fileName = preg_replace('/\.(js|css)$/', '', $file); + $parts = explode('-', $fileName); + $hash = $parts[0]; + + if ($hash == $lastHash) { + unlink("{$minifiedDir}/{$file}"); + continue; + } + + $lastHash = $hash; + } + } +} \ No newline at end of file diff --git a/app/code/community/Fballiano/CssjsMinify/etc/config.xml b/app/code/community/Fballiano/CssjsMinify/etc/config.xml new file mode 100644 index 0000000..76d2a9c --- /dev/null +++ b/app/code/community/Fballiano/CssjsMinify/etc/config.xml @@ -0,0 +1,39 @@ + + + + + 0.1.0 + + + + + + Fballiano_CssjsMinify_Model + + + + + + + + + fballiano_cssjsminify/observer + httpResponseSendBefore + + + + + + + + + + 30 3 * * * + + + fballiano_cssjsminify/observer::dailyCron + + + + + \ No newline at end of file diff --git a/app/etc/modules/Fballiano_CssjsMinify.xml b/app/etc/modules/Fballiano_CssjsMinify.xml new file mode 100644 index 0000000..890acb0 --- /dev/null +++ b/app/etc/modules/Fballiano_CssjsMinify.xml @@ -0,0 +1,9 @@ + + + + + true + community + + + \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..951c497 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "fballiano/openmage-cssjs-minify", + "license": "OSL-3.0", + "type": "magento-module", + "description": "CSS/JS minify module for OpenMage (and Magento 1.9)", + "keywords": [ + "magento", + "magento1", + "openmage", + "minify" + ], + "homepage": "https://github.com/fballiano/openmage-cssjs-minify", + "require": { + "php": ">=7.4.0", + "magento-hackathon/magento-composer-installer": "*", + "matthiasmullie/minify": "^1.3" + }, + "authors": [ + { + "name": "Fabrizio Balliano", + "email": "fabrizio@fabrizioballiano.com", + "homepage": "http://fabrizioballiano.com", + "role": "Developer" + } + ] +} \ No newline at end of file diff --git a/modman b/modman new file mode 100644 index 0000000..0207692 --- /dev/null +++ b/modman @@ -0,0 +1,2 @@ +app/code/community/Fballiano/CssjsMinify/ app/code/community/Fballiano/CssjsMinify/ +app/etc/modules/Fballiano_CssjsMinify.xml app/etc/modules/Fballiano_CssjsMinify.xml \ No newline at end of file