comparison CacheTimeCheck.inc @ 48:c6d0892f81ff

Documentation.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Tue, 09 Oct 2012 20:05:12 +0200
parents 66382989353f
children 2d0cda52f43f
comparison
equal deleted inserted replaced
47:66382989353f 48:c6d0892f81ff
1 <?php 1 <?php
2 /**
3 * CacheTimeCheck provides a set of functions to enable generating a
4 * correct time for the latest update for a given file.
5 *
6 * @author Tom Fredrik Blenning Klaussen
7 */
2 class CacheTimeCheck 8 class CacheTimeCheck
3 { 9 {
4 private $newest; 10 private $newest;
5 private $files = array(); 11 private $files = array();
6 private static $myInstance = 0; 12 private static $myInstance = 0;
10 if ($filename) 16 if ($filename)
11 $this->cache_time($filename); 17 $this->cache_time($filename);
12 $this->cache_time(__FILE__); 18 $this->cache_time(__FILE__);
13 } 19 }
14 20
21 /**
22 * List a set of files which contributes to this pages cacheset.
23 *
24 * @param $humanReadable If the timestamp should be humand readable.
25 *
26 * @return an associative array of file, and timestamp
27 */
15 function cacheSet($humanReadable = False) 28 function cacheSet($humanReadable = False)
16 { 29 {
17 $retVal = array(); 30 $retVal = array();
18 foreach($this->files as $file) { 31 foreach($this->files as $file) {
19 $mtime = filemtime($file); 32 $mtime = filemtime($file);
23 $retVal[$file] = $mtime; 36 $retVal[$file] = $mtime;
24 } 37 }
25 return $retVal; 38 return $retVal;
26 } 39 }
27 40
41 /**
42 * Generates a singleton instance of this CacheTimeCheck
43 *
44 * @param $filename an optional file to include in the cacheset
45 *
46 * @return a CacheTimeCheck object
47 */
28 function instance($filename = False) 48 function instance($filename = False)
29 { 49 {
30 if (! self::$myInstance) 50 if (! self::$myInstance)
31 self::$myInstance = new self($filename); 51 self::$myInstance = new self($filename);
32 elseif ($filename) 52 elseif ($filename)
33 self::$myInstance->cache_time($filename); 53 self::$myInstance->cache_time($filename);
34 return self::$myInstance; 54 return self::$myInstance;
35 } 55 }
36 56
57 /**
58 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is
59 * newer, set a Last-Modified header, otherwise abort with an 304
60 * status code
61 */
37 function CheckHttpModified() 62 function CheckHttpModified()
38 { 63 {
39 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) 64 if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
40 var_dump($_SERVER); 65 var_dump($_SERVER);
41 66
52 } 77 }
53 78
54 header("Last-Modified: $gmdate_mod"); 79 header("Last-Modified: $gmdate_mod");
55 } 80 }
56 81
82 /**
83 * Include a file in the cacheset
84 *
85 * @param $path the path of the file
86 * @param $basedir ***REMOVE THIS***
87 */
57 function cache_time($path, $basedir = False) 88 function cache_time($path, $basedir = False)
58 { 89 {
59 if (!file_exists($path)) { 90 if (!file_exists($path)) {
60 warn("${path} does not exist"); 91 warn("${path} does not exist");
61 errorPage("Resource not available"); 92 errorPage("Resource not available");
66 if ($mtime > $this->newest) { 97 if ($mtime > $this->newest) {
67 $this->newest = $mtime; 98 $this->newest = $mtime;
68 } 99 }
69 } 100 }
70 101
102 /**
103 * Convenience function to include a file, and add it to the cacheset.
104 *
105 * @param $path path of the file
106 * @param $basedir a directory to prepend to the path
107 */
71 function includeOnce($path, $basedir = false) 108 function includeOnce($path, $basedir = false)
72 { 109 {
73 if ($basedir) 110 if ($basedir)
74 $path = $basedir . "/" . $path; 111 $path = $basedir . "/" . $path;
75 $this->cache_time($path); 112 $this->cache_time($path);
76 include_once($path); 113 include_once($path);
77 } 114 }
78 115
116 /**
117 * Convenience function to load a file, and add it to the cacheset
118 *
119 * @param $path path of the file
120 * @return the contents of the file
121 */
79 function loadFile($path) 122 function loadFile($path)
80 { 123 {
81 $this->cache_time($path); 124 $this->cache_time($path);
82 return loadFile($path); 125 return loadFile($path);
83 } 126 }