Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed May 6, 2024
0 parents commit 2951d97
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: fballiano
buy_me_a_coffee: fballiano
custom: ["https://paypal.me/fabrizioballiano"]
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
CSS/JS Minify module for OpenMage/Magento1
=============================

<table><tr><td align=center>
<strong>If you find my work valuable, please consider sponsoring</strong><br />
<a href="https://github.com/sponsors/fballiano" target=_blank title="Sponsor me on GitHub"><img src="https://img.shields.io/badge/sponsor-30363D?style=for-the-badge&logo=GitHub-Sponsors&logoColor=#white" alt="Sponsor me on GitHub" /></a>
<a href="https://www.buymeacoffee.com/fballiano" target=_blank title="Buy me a coffee"><img src="https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black" alt="Buy me a coffee" /></a>
<a href="https://www.paypal.com/paypalme/fabrizioballiano" target=_blank title="Donate via PayPal"><img src="https://img.shields.io/badge/PayPal-00457C?style=for-the-badge&logo=paypal&logoColor=white" alt="Donate via PayPal" /></a>
</td></tr></table>

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
89 changes: 89 additions & 0 deletions app/code/community/Fballiano/CssjsMinify/Model/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* @category FBalliano
* @package Fballiano_CssjsMinify
* @copyright Copyright (c) Fabrizio Balliano (http://fabrizioballiano.com)
* @license https://opensource.org/license/osl-3 Open Software License (OSL 3.0)
*/
class Fballiano_CssjsMinify_Model_Observer
{
public const MINIFIED_FILES_FOLDER = 'fbminify';

public function httpResponseSendBefore(Varien_Event_Observer $observer): void
{
$response = $observer->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 = '/(<script.+src\s*=\s*["\'])(.*\.js)(["\'].*>)/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 = '/(<link.+href\s*=\s*["\'])(.*\.css)(["\'].*>)/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;
}
}
}
39 changes: 39 additions & 0 deletions app/code/community/Fballiano/CssjsMinify/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<config>
<modules>
<Fballiano_CssjsMinify>
<version>0.1.0</version>
</Fballiano_CssjsMinify>
</modules>
<global>
<models>
<fballiano_cssjsminify>
<class>Fballiano_CssjsMinify_Model</class>
</fballiano_cssjsminify>
</models>
</global>
<frontend>
<events>
<http_response_send_before>
<observers>
<fballiano_cssjsminify>
<class>fballiano_cssjsminify/observer</class>
<method>httpResponseSendBefore</method>
</fballiano_cssjsminify>
</observers>
</http_response_send_before>
</events>
</frontend>
<crontab>
<jobs>
<fballiano_cssjsminify>
<schedule>
<cron_expr>30 3 * * *</cron_expr>
</schedule>
<run>
<model>fballiano_cssjsminify/observer::dailyCron</model>
</run>
</fballiano_cssjsminify>
</jobs>
</crontab>
</config>
9 changes: 9 additions & 0 deletions app/etc/modules/Fballiano_CssjsMinify.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config>
<modules>
<Fballiano_CssjsMinify>
<active>true</active>
<codePool>community</codePool>
</Fballiano_CssjsMinify>
</modules>
</config>
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "http://fabrizioballiano.com",
"role": "Developer"
}
]
}
2 changes: 2 additions & 0 deletions modman
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2951d97

Please sign in to comment.