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