Skip to content

Commit

Permalink
Version 4.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luigifab committed Jun 6, 2022
1 parent 2ba3f77 commit 80762e1
Show file tree
Hide file tree
Showing 25 changed files with 281 additions and 113 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Stop russian war. **🇺🇦 Free Ukraine!**

# minifier

A module to minify and merge CSS/JS files for [OpenMage](https://github.com/OpenMage/magento-lts).
Expand All @@ -6,7 +8,7 @@ For more information, go to https://www.luigifab.fr/openmage/minifier (IPv6 is r

This repository is a releases mirror. To install the module, please use the composer key available in the documentation.

- Current version: 4.2.1 (02/02/2022)
- Current version: 4.3.0 (03/03/2022)
- Compatibility: OpenMage 19.x / 20.x / 21.x, PHP 7.2 / 7.3 / 7.4 / 8.0 / 8.1
- Client compatibility: Firefox 36+, Chrome 32+, Opera 19+, Edge 16+, Safari 9+
- Translations: English (en), French (fr-FR/fr-CA), German (de), Italian (it), Portuguese (pt-PT/pt-BR), Spanish (es) / Chinese (zh), Czech (cs), Dutch (nl), Greek (el), Hungarian (hu), Japanese (ja), Polish (pl), Romanian (ro), Russian (ru), Slovak (sk), Turkish (tr), Ukrainian (uk)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Created S/20/06/2015
* Updated J/30/09/2021
* Updated V/20/05/2022
*
* Copyright 2011-2022 | Fabrice Creuzot (luigifab) <code~luigifab~fr>
* https://www.luigifab.fr/openmage/minifier
Expand All @@ -23,12 +23,12 @@ public function render(Varien_Data_Form_Element_Abstract $element) {

$msg = $this->checkRewrites();
if ($msg !== true)
return sprintf('<p class="box">%s %s <span class="right"><a href="https://www.%s">%3$s</a> | ⚠ IPv6</span></p><p class="box" style="margin-top:-5px; color:white; background-color:#E60000;"><strong>%s</strong><br />%s</p>',
return sprintf('<p class="box">%s %s <span class="right">Stop russian war. <b>🇺🇦 Free Ukraine!</b> | <a href="https://www.%s">%3$s</a> | ⚠ IPv6</span></p><p class="box" style="margin-top:-5px; color:white; background-color:#E60000;"><strong>%s</strong><br />%s</p>',
'Luigifab/Minifier', $this->helper('minifier')->getVersion(), 'luigifab.fr/openmage/minifier',
$this->__('INCOMPLETE MODULE INSTALLATION'),
$this->__('There is conflict (<em>%s</em>).', $msg));

return sprintf('<p class="box">%s %s <span class="right"><a href="https://www.%s">%3$s</a> | ⚠ IPv6</span></p>',
return sprintf('<p class="box">%s %s <span class="right">Stop russian war. <b>🇺🇦 Free Ukraine!</b> | <a href="https://www.%s">%3$s</a> | ⚠ IPv6</span></p>',
'Luigifab/Minifier', $this->helper('minifier')->getVersion(), 'luigifab.fr/openmage/minifier');
}

Expand All @@ -39,8 +39,9 @@ protected function checkRewrites() {
['block' => 'adminhtml/page_head'],
['block' => 'page/html'],
['block' => 'page/html_head'],
['helper' => 'core/js'],
['model' => 'core/design_package'],
['model' => 'core/translate']
['model' => 'core/translate'],
];

foreach ($rewrites as $rewrite) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Created S/03/03/2018
* Updated L/13/09/2021
* Updated L/14/02/2022
*
* Copyright 2011-2022 | Fabrice Creuzot (luigifab) <code~luigifab~fr>
* https://www.luigifab.fr/openmage/minifier
Expand All @@ -21,26 +21,38 @@ class Luigifab_Minifier_Block_Adminhtml_Config_Server extends Mage_Adminhtml_Blo

public function render(Varien_Data_Form_Element_Abstract $element) {

return str_replace("\n", '<br />', '<tr><td colspan="4"><code style="display:block; margin:1em 0; padding-left:6px; font-size:0.9em; line-height:1.4em; border-left:3px solid #AAA;"><strong>## THE REQUIRED CONFIGURATION</strong>
# for apache (virtual host or htaccess)
return str_replace(['`', "\n"], [chr(194).chr(160), '<br />'], '<tr><td colspan="4"><code style="display:block; margin:1em 0; padding-left:6px; font-size:0.9em; line-height:1.4em; border-left:3px solid #AAA;"><strong>## THE REQUIRED CONFIGURATION</strong>
## for apache (virtual host or htaccess)
RewriteRule (.*)skin/.*/favicon.ico $1favicon.ico [L]
# global
RewriteRule (.*)media/minifier-\d+/(.*) $1media/minifier/$2 [L]
RewriteRule (.*)skin/adminhtml/(\w+)-\d+/(.*) $1skin/adminhtml/$2/$3 [L]
RewriteRule (.*)skin/frontend/(\w+)-\d+/(.*) $1skin/frontend/$2/$3 [L]
RewriteRule (.*)skin/.*/favicon.ico $1favicon.ico [L]
RewriteRule (.*)js-\d+/(.*) $1js/$2 [L]
# for lighttpd
# by file
RewriteRule (.*)-\d{10,}\.(.*) $1.$2 [L]
## for lighttpd
url.rewrite-once = (
"(.*)/media/minifier-\d+/(.*)" => "$1/media/minifier/$2",
"(.*)/skin/adminhtml/(\w+)-\d+/(.*)" => "$1/skin/adminhtml/$2/$3",
"(.*)/skin/frontend/(\w+)-\d+/(.*)" => "$1/skin/frontend/$2/$3",
"(.*)/skin/.*/favicon.ico" => "$1/favicon.ico",
"(.*)/js-\d+/(.*)" => "$1/js/$2"
````"(.*)/skin/.*/favicon.ico" => "$1/favicon.ico",
````# global
````"(.*)/media/minifier-\d+/(.*)" => "$1/media/minifier/$2",
````"(.*)/skin/adminhtml/(\w+)-\d+/(.*)" => "$1/skin/adminhtml/$2/$3",
````"(.*)/skin/frontend/(\w+)-\d+/(.*)" => "$1/skin/frontend/$2/$3",
````"(.*)/js-\d+/(.*)" => "$1/js/$2",
````# by file
````"(.*)-\d{10,}\.(.*)" => "$1.$2"
)
# for nginx
## for nginx
rewrite "(.*)/skin/.*/favicon.ico" $1/favicon.ico last;
# global
rewrite "(.*)/media/minifier-\d+/(.*)" $1/media/minifier/$2 last;
rewrite "(.*)/skin/adminhtml/(\w+)-\d+/(.*)" $1/skin/adminhtml/$2/$3 last;
rewrite "(.*)/skin/frontend/(\w+)-\d+/(.*)" $1/skin/frontend/$2/$3 last;
rewrite "(.*)/skin/.*/favicon.ico" $1/favicon.ico last;
rewrite "(.*)/js-\d+/(.*)" $1/js/$2 last;</code></td></tr>');
rewrite "(.*)/js-\d+/(.*)" $1/js/$2 last;
# by file
rewrite "(.*)-\d{10,}\.(.*)" $1.$2 last;</code></td></tr>');
}
}
36 changes: 11 additions & 25 deletions src/app/code/community/Luigifab/Minifier/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Created S/20/06/2015
* Updated J/27/01/2022
* Updated S/19/02/2022
*
* Copyright 2011-2022 | Fabrice Creuzot (luigifab) <code~luigifab~fr>
* https://www.luigifab.fr/openmage/minifier
Expand Down Expand Up @@ -117,6 +117,16 @@ public function getUsername() {
}


public function getNumberOfCpuCore() {

if (empty($this->cpucore)) {
exec('nproc', $data);
$this->cpucore = max(1, (int) trim(implode($data)));
}

return $this->cpucore;
}

public function afterToHtml(string $html) {

$current = Mage::app()->getFrontController()->getAction()->getFullActionName('/');
Expand All @@ -131,30 +141,6 @@ public function afterToHtml(string $html) {
return trim($html);
}

public function getKeyForUrls() {

if (!isset($this->_urlkey)) {
$this->_urlkey = '';
if (Mage::getStoreConfigFlag('minifier/cssjs/solution')) {
$this->_urlkey = '-'.preg_replace('#\D#', '', Mage::getStoreConfig('minifier/cssjs/value'));
if (Mage::getIsDeveloperMode())
$this->_urlkey = '-00'.date('YmdHis');
}
}

return $this->_urlkey;
}

public function getNumberOfCpuCore() {

if (empty($this->cpucore)) {
exec('nproc', $data);
$this->cpucore = max(1, (int) trim(implode($data)));
}

return $this->cpucore;
}


protected function cleanWithTidy(string $html) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Created L/16/07/2018
* Updated L/16/07/2018
* Updated V/11/02/2022
*
* Copyright 2011-2022 | Fabrice Creuzot (luigifab) <code~luigifab~fr>
* https://www.luigifab.fr/openmage/minifier
Expand All @@ -17,10 +17,17 @@
* GNU General Public License (GPL) for more details.
*/

class Luigifab_Minifier_Helper_Js extends Mage_Core_Helper_Js {
class Luigifab_Minifier_Helper_Rewrite_Js extends Mage_Core_Helper_Js {

public function getTranslatorScript() {
$this->_translateData = null;
public function getJsUrl($file) {
return Mage::getDesign()->getJsUrl($file);
}

public function getJsSkinUrl($file) {
return Mage::getDesign()->getSkinUrl($file);
}

public function getTranslatorScriptContent() {
return 'var Translator = new Translate('.$this->getTranslateJson().');';
}
}
48 changes: 27 additions & 21 deletions src/app/code/community/Luigifab/Minifier/Model/Files.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
/**
* Created W/13/04/2016
* Updated D/24/10/2021
* Updated V/13/05/2022
*
* Copyright 2011-2022 | Fabrice Creuzot (luigifab) <code~luigifab~fr>
* Copyright 2022 | Fabrice Creuzot <fabrice~cellublue~com>
* https://www.luigifab.fr/openmage/minifier
*
* This program is free software, you can redistribute it or modify
Expand Down Expand Up @@ -38,8 +39,9 @@ public function getMinifiedFiles(int $storeId) {
if (!is_dir($dir) || !is_writable($dir))
Mage::throwException('Directory media/minifier does not exist or is not writable.');

$start = microtime(true);
$debug = 'load files from cache';
$start = microtime(true);
$debug = 'load files from cache';
$design = Mage::getDesign();

// cherche les fichiers sources
// depuis le cache ou depuis le layout
Expand All @@ -49,7 +51,7 @@ public function getMinifiedFiles(int $storeId) {
if (empty($items)) {

$debug = 'load files from layout';
$items = $this->searchFiles($storeId);
$items = $this->searchFiles($design, $storeId);

if (Mage::app()->useCache('layout'))
Mage::app()->saveCache(json_encode($items), 'minifier_layout_'.$storeId,
Expand All @@ -58,33 +60,33 @@ public function getMinifiedFiles(int $storeId) {

// minifie les fichiers sources (et change la clé)
$hasChange = $this->minifyFiles($items);
if ($hasChange) {
if ($hasChange && (Mage::getStoreConfig('minifier/cssjs/solution') == 1)) {
$value = Mage::getSingleton('core/date')->date('YmdHis');
Mage::getModel('core/config')->saveConfig('minifier/cssjs/value', $value);
Mage::getConfig()->reinit(); // très important
}

// génère le html
$baseurl = Mage::getBaseUrl('media').'minifier'.Mage::helper('minifier')->getKeyForUrls().'/';
foreach ($items as $file => $data) {

if ($data['merge'] && ($hasChange || !is_file($dir.$file)))
$this->mergeFiles($dir.$file, $data);
$fileName = $dir.$file;
if ($data['merge'] && ($hasChange || !is_file($fileName)))
$this->mergeFiles($fileName, $data);

$url = Mage::getBaseUrl('media').'minifier-zzyyxx/'.$file;
$url = $design->getFinalUrl($fileName, $url);

$items[$file]['html'] = $data['css'] ?
sprintf('<link rel="stylesheet" media="%s" type="text/css" href="%s" />', $data['media'], $baseurl.$file) :
sprintf('<script type="text/javascript" src="%s"></script>', $baseurl.$file);
sprintf('<link rel="stylesheet" media="%s" type="text/css" href="%s" />', $data['media'], $url) :
sprintf('<script type="text/javascript" src="%s"></script>', $url);
}

// debug
if (!empty($_COOKIE['minifier']) && Mage::getStoreConfigFlag('minifier/cssjs/debug_enabled')) {

array_unshift($items, round(microtime(true) - $start, 3).' seconds');
array_unshift($items, $debug);
array_unshift($items, (empty($value) ? Mage::getStoreConfig('minifier/cssjs/value') : $value));
array_unshift($items, gmdate('c'));
array_unshift($items, getenv('REQUEST_URI'));

Mage::getSingleton('core/session')->setData('minifier', $items);
}

Expand All @@ -94,11 +96,15 @@ public function getMinifiedFiles(int $storeId) {
// utilise uglify-js et clean-css
protected function minifyFiles(array $items) {

$core = max(1, Mage::helper('minifier')->getNumberOfCpuCore() - 1);
$core = max(1, Mage::helper('minifier')->getNumberOfCpuCore() - 2);
$pids = [];
$files = [];
$new = false;

$dir = Mage::getBaseDir('log');
if (!is_dir($dir))
@mkdir($dir, 0755);

// rassemble les fichiers à minifier
foreach ($items as $item)
$files += $item['files'];
Expand All @@ -121,16 +127,17 @@ protected function minifyFiles(array $items) {
if (!is_file($lock))
file_put_contents($lock, getenv('REMOTE_ADDR'), LOCK_EX);

$outdated = preg_replace('#\.[a-z0-9]+\.min\.#', '*.min.', $cache);
$outdated = preg_replace('#\.[a-z\d]+\.min\.#', '*.min.', $cache);
array_map('unlink', glob($outdated));

$new = true;
$cmd = sprintf('php %s %s %s %s %d >/dev/null 2>&1 & echo $!',
$cmd = sprintf('php %s %s %s %s %d >> %s 2>&1 & echo $!',
str_replace('Minifier/etc', 'Minifier/lib/minify.php', Mage::getModuleDir('etc', 'Luigifab_Minifier')),
(mb_stripos($source, '.css') === false) ? 'js' : 'css',
escapeshellarg($source),
escapeshellarg($cache),
Mage::getIsDeveloperMode() ? 1 : 0);
Mage::getIsDeveloperMode() ? 1 : 0,
$dir.'/minifier.log');

Mage::log($cmd, Zend_Log::DEBUG, 'minifier.log');
$pids[] = exec($cmd);
Expand Down Expand Up @@ -207,13 +214,12 @@ protected function getFileFromHelper(object $node) {
return call_user_func_array([Mage::helper($helperName), $helperMethod], $attributes);
}

protected function searchFiles(int $storeId) {
protected function searchFiles(object $design, int $storeId) {

//$ignores = array_filter(preg_split('#\s+#', Mage::getStoreConfig('minifier/cssjs/exclude')));
$removed = [];
$items = [];
$data = ['optionalZipCountries = '.Mage::helper('directory')->getCountriesWithOptionalZip(true).';'];
$design = Mage::getDesign();

// génération des fichiers virtuels
if (empty($storeId)) {
Expand Down Expand Up @@ -251,7 +257,7 @@ protected function searchFiles(int $storeId) {
$data1 = Mage::getBlockSingleton('minifier/calendar')->setTemplate('page/js/calendar.phtml')->getHtml($locale);
$data1 = str_replace(['<script type="text/javascript">', '<script>', '//<![CDATA[', '//]]>', '</script>'], '', $data1);
$data1 = 'if (self.Calendar) { '.$data1.' }';
$data2 = Mage::helper('minifier/js')->getTranslatorScript();
$data2 = Mage::helper('core/js')->getTranslatorScriptContent();

file_put_contents($source, trim(implode("\n", $data)."\n".trim($data2)."\n".trim($data1)), LOCK_EX);
}
Expand Down Expand Up @@ -459,7 +465,7 @@ protected function searchFiles(int $storeId) {

// nom des fichiers
protected function getFinalName(string $pack, string $media) {
return (string) preg_replace('#[^a-z0-9.]+#', '-', $pack.'-'.$media.((mb_stripos($media, 'script') === false) ? '.min.css' : '.min.js'));
return (string) preg_replace('#[^a-z\d.]+#', '-', $pack.'-'.$media.((mb_stripos($media, 'script') === false) ? '.min.css' : '.min.js'));
}

protected function getRealSource(string $file) {
Expand Down
6 changes: 3 additions & 3 deletions src/app/code/community/Luigifab/Minifier/Model/Observer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Created S/20/06/2015
* Updated J/04/11/2021
* Updated S/19/02/2022
*
* Copyright 2011-2022 | Fabrice Creuzot (luigifab) <code~luigifab~fr>
* https://www.luigifab.fr/openmage/minifier
Expand Down Expand Up @@ -37,7 +37,7 @@ public function clearMinifiedFiles(Varien_Event_Observer $observer) {
}

// EVENT admin_system_config_changed_section_minifier (adminhtml)
public function updateConfig(Varien_Event_Observer $observer) {
public function clearCache(Varien_Event_Observer $observer) {

Mage::app()->cleanCache();
Mage::dispatchEvent('adminhtml_cache_flush_system');
Expand All @@ -46,7 +46,7 @@ public function updateConfig(Varien_Event_Observer $observer) {
}

// EVENT controller_action_predispatch_adminhtml_index_changeLocale (adminhtml)
public function changeBackgendLanguage(Varien_Event_Observer $observer) {
public function updateBackgendLanguage(Varien_Event_Observer $observer) {

$locale = $observer->getData('controller_action')->getRequest()->getParam('locale');
Mage::getSingleton('core/session')->setData('locale', $locale);
Expand Down
Loading

0 comments on commit 80762e1

Please sign in to comment.