Skip to content

Instantly share code, notes, and snippets.

@pjbeardsley
Created July 11, 2012 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjbeardsley/3090589 to your computer and use it in GitHub Desktop.
Save pjbeardsley/3090589 to your computer and use it in GitHub Desktop.
fTemplating.php.patch
Index: fTemplating.php
===================================================================
--- fTemplating.php (revision 71)
+++ fTemplating.php (working copy)
@@ -100,6 +100,20 @@
* @var integer
*/
private $buffered_id;
+
+ /**
+ * If cache busting is enabled
+ *
+ * @var boolean
+ */
+ private $cache_busting;
+
+ /**
+ * The path prefix to prepend to CSS and JS paths to find them on the filesystem
+ *
+ * @var string
+ */
+ private $cache_busting_prefix;
/**
* A data store for templating
@@ -395,6 +409,30 @@
$this->__destruct();
}
+
+
+ /**
+ * Enables adding query strings to CSS and JS to break far-future cache headers
+ *
+ * Please note that this option requires that all CSS and JS paths be
+ * relative to the $_SERVER['DOCUMENT_ROOT'] and start with a `/`.
+ *
+ * This functionality will be inherited by all child fTemplating objects
+ * that do not have their own explicit cache busting settings.
+ *
+ * @param fDirectory|string $path_prefix The directory to prepend to all CSS and JS paths to load the files from the filesystem - this defaults to `$_SERVER['DOCUMENT_ROOT']`
+ * @return void
+ */
+ public function enableCacheBusting($path_prefix=NULL)
+ {
+ $path_prefix = $path_prefix instanceof fDirectory ? $path_prefix->getPath() : $path_prefix;
+ if ($path_prefix === NULL) {
+ $path_prefix = $_SERVER['DOCUMENT_ROOT'];
+ }
+
+ $this->cache_busting = TRUE;
+ $this->cache_busting_prefix = $path_prefix;
+ }
/**
@@ -710,6 +748,39 @@
return $value;
}
+
+
+ /**
+ * Adds query strings to files based on their contents to bust far-futures cache headers
+ *
+ * @param string $type The type of compilation, 'css' or 'js'
+ * @param string $element The element name
+ * @param array $values An array of file paths
+ * @return void
+ */
+ protected function handleCacheBusting($type, $element, $values)
+ {
+ foreach ($values as $value) {
+ $path = $this->cache_busting_prefix . $value;
+ $version = filemtime($path);
+ $web_path = fFilesystem::translateToWebPath($path) . '?v=' . $version;
+
+ $media = NULL;
+ if (is_array($value) && $type == 'css') {
+ $media = !empty($value['media']) ? $value['media'] : NULL;
+ }
+
+ if ($type == 'css' && $media) {
+ $web_path = array(
+ 'path' => $web_path,
+ 'media' => $media
+ );
+ }
+
+ $method = 'place' . strtoupper($type);
+ $this->$method($web_path);
+ }
+ }
/**
@@ -1158,6 +1229,9 @@
if ($this->minification_directory && in_array($value_group['type'], array('js', 'css'))) {
$this->handleMinified($value_group['type'], $element, $value_group['values']);
continue;
+ } elseif ($this->cache_busting && in_array($value_group['type'], array('js', 'css'))) {
+ $this->handleCacheBusting($value_group['type'], $element, $value_group['values']);
+ continue;
}
if ($this->short_tag_directory && $value_group['type'] == 'php') {
$value_group['values'] = $this->fixShortTags($value_group['values']);
@@ -1181,6 +1255,10 @@
$value->short_tag_directory = $this->short_tag_directory;
$value->short_tag_mode = $this->short_tag_mode;
}
+ if ($value->cache_busting === NULL) {
+ $value->cache_busting = $this->cache_busting;
+ $value->cache_busting_prefix = $this->cache_busting_prefix;
+ }
$value->place();
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment