Mercurial > SimpleWebPresenter
comparison CacheTimeCheck.inc @ 69:dd4ddedca4c5
Add convenience header ScriptIncludeCache
Split CacheTimeCheck and FileCacheSet
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 11 Oct 2012 23:32:14 +0200 |
| parents | 4dfa3f6a2dc1 |
| children | d98e315308cd |
comparison
equal
deleted
inserted
replaced
| 68:4dfa3f6a2dc1 | 69:dd4ddedca4c5 |
|---|---|
| 1 <?php | 1 <?php |
| 2 $baseDir = dirname(__FILE__); | 2 $baseDir = dirname(__FILE__); |
| 3 | 3 |
| 4 include_once 'Logger.inc'; | |
| 5 include_once 'common-functions.inc'; | 4 include_once 'common-functions.inc'; |
| 5 include_once 'FileCacheSet.inc'; | |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | 6 $cache = ScriptIncludeCache::instance(__FILE__); |
| 7 $cache->cache_time("${baseDir}/Logger.inc"); | 7 $cache->cache_time("${baseDir}/FileCacheSet.inc"); |
| 8 $cache->cache_time("${baseDir}/common-functions.inc"); | |
| 9 | |
| 10 class FileCacheSet { | |
| 11 private $newest = 0; | |
| 12 private $files = array(); | |
| 13 private $parentCaches = array(); | |
| 14 | |
| 15 protected function __construct($parent = null) { | |
| 16 if ($parent) { | |
| 17 $this->addParent($parent); | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 protected function addParent($parent) | |
| 22 { | |
| 23 array_push($this->parentCaches, $parent); | |
| 24 } | |
| 25 | |
| 26 /** | |
| 27 * List a set of files which contributes to this pages cacheset. | |
| 28 * | |
| 29 * @param $humanReadable If the timestamp should be humand readable. | |
| 30 * | |
| 31 * @return an associative array of file, and timestamp | |
| 32 */ | |
| 33 function cacheSet($humanReadable = False) | |
| 34 { | |
| 35 $retVal = array(); | |
| 36 foreach ($this->parentCaches as $parent) { | |
| 37 $retVal = array_merge($retVal, $parent->cacheSet($humanReadable)); | |
| 38 } | |
| 39 foreach($this->files as $file) { | |
| 40 $mtime = filemtime($file); | |
| 41 if ($humanReadable) | |
| 42 $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; | |
| 43 | |
| 44 $retVal[$file] = $mtime; | |
| 45 } | |
| 46 return $retVal; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Include a file in the cacheset | |
| 51 * | |
| 52 * @param $path the path of the file | |
| 53 */ | |
| 54 function cache_time($path) | |
| 55 { | |
| 56 if (!file_exists($path)) { | |
| 57 Logger::warn("${path} does not exist"); | |
| 58 errorPage("Resource is not available"); | |
| 59 } | |
| 60 | |
| 61 array_push($this->files, $path); | |
| 62 $mtime = filemtime($path); | |
| 63 if ($mtime > $this->newest) { | |
| 64 $this->newest = $mtime; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 function getNewest() | |
| 69 { | |
| 70 $newest = 0; | |
| 71 foreach ($this->parentCaches as $parent) { | |
| 72 $newest = max($newest, $parent->getNewest()); | |
| 73 } | |
| 74 $newest = max($newest, $this->newest); | |
| 75 return $newest; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 class ScriptIncludeCache extends FileCacheSet | |
| 80 { | |
| 81 private static $myInstance = 0; | |
| 82 | |
| 83 protected function __construct($filename = False) | |
| 84 { | |
| 85 parent::__construct(); | |
| 86 if ($filename) | |
| 87 $this->cache_time($filename); | |
| 88 $this->cache_time(__FILE__); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Generates a singleton instance of this CacheTimeCheck | |
| 93 * | |
| 94 * @param $filename an optional file to include in the cacheset | |
| 95 * | |
| 96 * @return a CacheTimeCheck object | |
| 97 */ | |
| 98 function instance($filename = False) | |
| 99 { | |
| 100 if (! self::$myInstance) | |
| 101 self::$myInstance = new self($filename); | |
| 102 elseif ($filename) | |
| 103 self::$myInstance->cache_time($filename); | |
| 104 return self::$myInstance; | |
| 105 } | |
| 106 | |
| 107 /** | |
| 108 * Convenience function to include a file, and add it to the cacheset. | |
| 109 * | |
| 110 * @param $path path of the file | |
| 111 * @param $basedir a directory to prepend to the path | |
| 112 */ | |
| 113 function includeOnce($path, $basedir = false) | |
| 114 { | |
| 115 if ($basedir) | |
| 116 $path = $basedir . "/" . $path; | |
| 117 $this->cache_time($path); | |
| 118 include_once($path); | |
| 119 } | |
| 120 } | |
| 121 | 8 |
| 122 /** | 9 /** |
| 123 * CacheTimeCheck provides a set of functions to enable generating a | 10 * CacheTimeCheck provides a set of functions to enable generating a |
| 124 * correct time for the latest update for a given file. | 11 * correct time for the latest update for a given file. |
| 125 * | 12 * |
