Skip to content

Instantly share code, notes, and snippets.

@mmodrow
Created October 19, 2017 11:33
Show Gist options
  • Save mmodrow/760daa967a92d029a496ebfdcab25e09 to your computer and use it in GitHub Desktop.
Save mmodrow/760daa967a92d029a496ebfdcab25e09 to your computer and use it in GitHub Desktop.
Cache breaker for gitted projects
/**
* add a cache breaking get parameter to an url, that's based on the local git dir timestamp
* @return string
*/
public function getCachebrokenUrl($url = "") {
$version = $this->getVersionHash();
$cachebrokenUrl = preg_replace("/((?:href|src)=\"[^?\"]*)(\")/", "$1?version=".$version."$2", $url);
return $cachebrokenUrl;
}
/**
* gets the .git directory's timestamp as unique id of a shop version
* @return bool|int
*/
protected function getGitFolderDate(){
return filemtime(self::LOCAL_PROJECT_BASE_PATH.DS.".git");
}
/**
* get hashed version of the git timestamp
* @return string
*/
protected function getVersionHash(){
return hash("sha256", $this->getGitFolderDate());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment