diff cache_check.inc @ 32:7b19be62ea94

Remove yet another global, replace by CacheTimeCheck class.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 04 Oct 2012 19:46:11 +0200
parents 1ec0738a1623
children
line wrap: on
line diff
--- a/cache_check.inc	Sun Sep 30 03:39:32 2012 +0200
+++ b/cache_check.inc	Thu Oct 04 19:46:11 2012 +0200
@@ -1,35 +1,47 @@
 <?php
-function cache_check($mtime)
+class CacheTimeCheck
 {
-  if (DEBUG)
-    var_dump($_SERVER);
+  private $newest;
+  private $files = array();
 
-  $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE'];
-  $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
+  function __construct($minTime = 0, $filename = False)
+  {
+    if ($filename)
+      array_push($this->files, $filename);
+    $this->newest = $minTime;
+    $this->cache_time(__FILE__);
+  }
 
-  $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
+  function CheckHttpModified()
+  {
+    if (DEBUG)
+      var_dump($_SERVER);
+
+    $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE'];
+    $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
 
-  if ($if_modified_since == $gmdate_mod) {
-    header("HTTP/1.0 304 Not Modified");
-    exit;
+    $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT';
+
+    if ($if_modified_since == $gmdate_mod) {
+      header("HTTP/1.0 304 Not Modified");
+      exit;
+    }
+    header("Last-Modified: $gmdate_mod");
   }
-  header("Last-Modified: $gmdate_mod");
-}
 
-function cache_time($path)
-{
-  global $newest;
-  $mtime = filemtime($path);
-  if ($mtime > $newest) {
-    $newest = $mtime;
+  function cache_time($path)
+  {
+    array_push($this->files, $path);
+    $mtime = filemtime($path);
+    if ($mtime > $this->newest) {
+      $this->newest = $mtime;
+    }
+  }
+
+  function includeOnce($path)
+  {
+    $this->cache_time($path);
+    include_once($path);
   }
 }
-
-function include_with_mtime($path)
-{
-  cache_time($path);
-  include_once($path);
-}
-
-cache_time(__FILE__);
 ?>
\ No newline at end of file