Skip to content

Commit

Permalink
Added fallback to timestamp if git approach is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Apr 17, 2024
1 parent 6d17aa9 commit b41dd69
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ Features

**How to gen the version number to use in the `v=xxx` parameter?**

This module uses the last `Git commit hash` (actually only the first 6 characters) as the version number.
This module supports 2 approaches:
1. using the last `Git commit hash` (only the first 6 characters) as the version number
2. if that fails then a timestamp is used

In order to do that your OpenMage base dir must also be the Git project root directory and the .git forlder
has to be present in all servers you need this functionality to work.
The git method is preferred because the generated version number won't change unless a new change is pushed
on the repository. However, in order to work, OpenMage base dir must also be the Git project root directory
and the .git folder has to be present in all servers you need this functionality to work.

**Resuming:**
- Automatically reads the last Git commit hash, extracts the first 6 characters
- Saves this version number in OpenMage config cache for 1hour
(if you flush config cache you also flush this value) in order to have a little
impact on the filesystem as possible
- If the git approach doesn't work, the version number because the current timestamp
(and gets cached for 24 hours)
- Parsed the output HTML and intercepts all `<script` and `<link` tags, extracts the URLs
and adds the `v=xxx` parameter (using `?v` or `&v` accordingly)
- It only catches `<script` tags with `src` parameter and `<link` tags with `href` parameter
Expand Down
67 changes: 44 additions & 23 deletions app/code/community/Fballiano/CssjsVersioning/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,11 @@ public function httpResponseSendBefore(Varien_Event_Observer $observer)
if (stripos($html, "</body>") === false) return;

$version = Mage::app()->loadCache(self::CACHE_ID);

if (!$version) {
$baseDir = Mage::getBaseDir();

// Read the contents of the HEAD file to get the current branch reference
$headContent = @file_get_contents("{$baseDir}/.git/HEAD");
if (!$headContent) {
return;
}

// Extract the branch name from the HEAD content
$parts = explode('/', trim($headContent));
$branchName = end($parts);
if (!$branchName) {
return;
$version = $this->getVersionByGit();
if ($version === false) {
$version = $this->getVersionByTime();
}

// Extract last commit hash
$version = file_get_contents("{$baseDir}/.git/refs/heads/{$branchName}");
if (!$version) {
return;
}

$version = substr($version, 0, 6); // Only using 6 chars of the hash
Mage::app()->saveCache($version, self::CACHE_ID, [Mage_Core_Model_Config::CACHE_TAG], 3600); // Cache for 1 hour
}

// Process the script tags
Expand All @@ -68,4 +48,45 @@ public function httpResponseSendBefore(Varien_Event_Observer $observer)

$response->setBody($html);
}

/**
* @return false|string
*/
protected function getVersionByGit()
{
$baseDir = Mage::getBaseDir();

// Read the contents of the HEAD file to get the current branch reference
$headContent = @file_get_contents("{$baseDir}/.git/HEAD");
if (!$headContent) {
return false;
}

// Extract the branch name from the HEAD content
$parts = explode('/', trim($headContent));
$branchName = end($parts);
if (!$branchName) {
return false;
}

// Extract last commit hash
$version = file_get_contents("{$baseDir}/.git/refs/heads/{$branchName}");
if (!$version) {
return false;
}

$version = substr($version, 0, 6); // Only using 6 chars of the hash
Mage::app()->saveCache($version, self::CACHE_ID, [Mage_Core_Model_Config::CACHE_TAG], 3600); // Cache for 1 hour
return $version;
}

/**
* @return int
*/
protected function getVersionByTime()
{
$version = time();
Mage::app()->saveCache($version, self::CACHE_ID, [Mage_Core_Model_Config::CACHE_TAG], 86400); // Cache for 24 hours
return $version;
}
}

0 comments on commit b41dd69

Please sign in to comment.