changeset 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 647b72603b7d
children 511b6514823f
files Options.inc cache_check.inc index.php inputParser.inc
diffstat 4 files changed, 61 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/Options.inc	Sun Sep 30 03:39:32 2012 +0200
+++ b/Options.inc	Thu Oct 04 19:46:11 2012 +0200
@@ -6,6 +6,7 @@
   private $name;
   private $acceptedLanguages = array();
   private $inputDefaults = array();
+  private $cache;
 
   function getDefaultLang()
   {
@@ -32,6 +33,16 @@
     return $this->name;
   }
 
+  function setCache($cache)
+  {
+    $this->cache = $cache;
+  }
+
+  function getCache()
+  {
+    return $this->cache;
+  }
+
   function getAcceptedLanguages()
   {
     return $this->acceptedLanguages;
--- 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
--- a/index.php	Sun Sep 30 03:39:32 2012 +0200
+++ b/index.php	Thu Oct 04 19:46:11 2012 +0200
@@ -13,23 +13,24 @@
   ini_set("display_errors", 1);
 }
 
-$SCRIPT_FILENAME=$_SERVER['SCRIPT_FILENAME'];
-$newest = filemtime($SCRIPT_FILENAME);
 $cacheable = true;
 
 include_once 'php/cache_check.inc';
-include_with_mtime('php/Options.inc');
-include_with_mtime('php/accept-language.inc');
-include_with_mtime('php/common-functions.inc');
-include_with_mtime('php/filters.inc');
-include_with_mtime('php/inputParser.inc');
 
-$URL_PARAMS=array('name','lang');
+$cache = new CacheTimeCheck(filemtime(__FILE__));
+$cache->includeOnce('php/Options.inc');
+$cache->includeOnce('php/accept-language.inc');
+$cache->includeOnce('php/common-functions.inc');
+$cache->includeOnce('php/filters.inc');
+$cache->includeOnce('php/inputParser.inc');
+
+$URL_PARAMS = array('name', 'lang');
 
 $master = new DOMDocument();
 $master->load("master.xml");
 
 $options = new Options($master);
+$options->setCache($cache);
 
 $lang = $_GET['lang'];
 if($lang) {
@@ -60,7 +61,7 @@
 }
 
 if (CACHING && $cacheable)
-  cache_check($newest);
+  $options->getCache()->CheckHttpModified($newest);
 
 print $master->saveXml($master);
 
--- a/inputParser.inc	Sun Sep 30 03:39:32 2012 +0200
+++ b/inputParser.inc	Thu Oct 04 19:46:11 2012 +0200
@@ -43,7 +43,7 @@
   if (!file_exists($confFile)) {
      errorPage("Resource not available");
   }
-  cache_time($confFile);
+  $options->getCache()->cache_time($confFile);
   $doc = new DOMDocument();
   $doc->load($confFile);
 
@@ -65,7 +65,7 @@
 	$subdoc = new DOMDocument();
 	$subfile = "${lang}/${src}";
 	$subdoc->load("$subfile");
-	cache_time($subfile);
+	$options->getCache()->cache_time($subfile);
 	$parent = $include->parentNode;
 	$xml = getElementByTagName($subdoc,"xml");
 	foreach($xml->childNodes as $child) {
@@ -142,7 +142,7 @@
     else {
       $src = $file->getAttribute("src");
       $fname = "${lang}/${src}";
-      cache_time($fname);
+      $options->getCache()->cache_time($fname);
       $file_content = loadFile($fname);
     }
     if(floatval($file_content)<0) {