Skip to content

Instantly share code, notes, and snippets.

@osrecio
Created July 14, 2015 15:44
Show Gist options
  • Save osrecio/af495add032f5d58336a to your computer and use it in GitHub Desktop.
Save osrecio/af495add032f5d58336a to your computer and use it in GitHub Desktop.
Magento CSS/JS Auto Versioning
<?xml version="1.0"?>
<!--
/**
* @category Interactiv4
* @package Interactiv4_MergeTimestamp
* @copyright Copyright (c) 2015 Interactiv4 SL. (http://www.interactiv4.com)
*
* @author Oscar Recio (@osrecio)
*/
-->
<!-- File: app/code/local/Interactiv4/MergeTimestamp/etc/config.xml -->
<config>
<modules>
<Interactiv4_MergeTimestamp>
<version>0.1.0</version>
</Interactiv4_MergeTimestamp>
</modules>
<global>
<models>
<i4mergetimestamp>
<class>Interactiv4_MergeTimestamp_Model</class>
</i4mergetimestamp>
<core>
<rewrite>
<design_package>Interactiv4_MergeTimestamp_Model_Core_Design_Package</design_package>
</rewrite>
</core>
</models>
<helpers>
<i4mergetimestamp>
<class>Interactiv4_MergeTimestamp_Helper</class>
</i4mergetimestamp>
</helpers>
</global>
</config>
<?php
/**
* @category Interactiv4
* @package Interactiv4_MergeTimestamp
* @copyright Copyright (c) 2015 Interactiv4 SL. (http://www.interactiv4.com)
*
* @author Oscar Recio (@osrecio)
*/
#File: app/code/local/Interactiv4/MergeTimestamp/Helper/Data.php
class Interactiv4_MergeTimestamp_Helper_Data extends Mage_Core_Helper_Abstract {
}
<?xml version="1.0"?>
<!--
/**
* @category    Interactiv4
* @package     Interactiv4
* @copyright   Copyright (c) 2015 Interactiv4 SL. (http://www.interactiv4.com)
*
* @author Oscar Recio (@osrecio)
*/
-->
<!-- File: app/etc/modules/Interactiv4_MergeTimestamp.xml -->
<config>
<modules>
<Interactiv4_MergeTimestamp>
<active>true</active>
<codePool>local</codePool>
</Interactiv4_MergeTimestamp>
</modules>
</config>
<?php
/**
* @category Interactiv4
* @package Interactiv4_MergeTimestamp
* @copyright Copyright (c) 2015 Interactiv4 SL. (http://www.interactiv4.com)
*
* @author Oscar Recio (@osrecio)
*/
#File: app/code/local/Interactiv4/MergeTimestamp/Model/Core/Design/Package.php
class Interactiv4_MergeTimestamp_Model_Core_Design_Package extends Mage_Core_Model_Design_Package
{
/**
* Get the timestamp of the newest file
*
* @param array $srcFiles
* @return int $timeStamp
*/
protected function getNewestFileTimestamp($srcFiles) {
$timeStamp = null;
foreach ($srcFiles as $file) {
if(is_null($timeStamp)) {
//if is first file, set $timeStamp to filemtime of file
$timeStamp = filemtime($file);
} else {
//get max of current files filemtime and the max so far
$timeStamp = max($timeStamp, filemtime($file));
}
}
return $timeStamp;
}
/**
* Merge specified css files and return URL to the merged file on success
*
* @param $files
* @return string
*/
public function getMergedCssUrl($files)
{
// secure or unsecure
$isSecure = Mage::app()->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;
}
//get timestamp of newest source file
$filesTimeStamp = $this->getNewestFileTimestamp($files);
// merge into target file
$targetFilename = md5(implode(',', $files) . "|{$hostname}|{$port}") . "_" . $filesTimeStamp . '.css';
//If the file with the proper timestamp as part of its filename already exists, there's no reason to check again to see if
//we need to remerge the css files
if(!file_exists($targetDir . DS . $targetFilename)) {
$mergeFilesResult = $this->_mergeFiles(
$files, $targetDir . DS . $targetFilename,
false,
array($this, 'beforeMergeCss'),
'css'
);
if ($mergeFilesResult) {
return $baseMediaUrl . $mergerDir . '/' . $targetFilename;
}
} else {
return $baseMediaUrl . $mergerDir . '/' . $targetFilename;
}
return '';
}
/**
* Merge specified javascript files and return URL to the merged file on success
*
* @param $files
* @return string
*/
public function getMergedJsUrl($files)
{
//get timestamp of newest source file
$filesTimeStamp = $this->getNewestFileTimestamp($files);
$targetFilename = md5(implode(',', $files)) . '_' . $filesTimeStamp . '.js';
$targetDir = $this->_initMergerDir('js');
if (!$targetDir) {
return '';
}
if ($this->_mergeFiles($files, $targetDir . DS . $targetFilename, false, null, 'js')) {
return Mage::getBaseUrl('media', Mage::app()->getRequest()->isSecure()) . 'js/' . $targetFilename;
}
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment