Mercurial > SimpleWebPresenter
changeset 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 | 62b9d7a35658 218a58bb2f34 |
| files | CacheTimeCheck.inc FileCacheSet.inc InputParser.inc ScriptIncludeCache.inc common-functions.inc flag.php sitemap.php |
| diffstat | 7 files changed, 140 insertions(+), 127 deletions(-) [+] |
line wrap: on
line diff
--- a/CacheTimeCheck.inc Thu Oct 11 23:16:53 2012 +0200 +++ b/CacheTimeCheck.inc Thu Oct 11 23:32:14 2012 +0200 @@ -1,123 +1,10 @@ <?php $baseDir = dirname(__FILE__); -include_once 'Logger.inc'; include_once 'common-functions.inc'; +include_once 'FileCacheSet.inc'; $cache = ScriptIncludeCache::instance(__FILE__); -$cache->cache_time("${baseDir}/Logger.inc"); -$cache->cache_time("${baseDir}/common-functions.inc"); - -class FileCacheSet { - private $newest = 0; - private $files = array(); - private $parentCaches = array(); - - protected function __construct($parent = null) { - if ($parent) { - $this->addParent($parent); - } - } - - protected function addParent($parent) - { - array_push($this->parentCaches, $parent); - } - - /** - * List a set of files which contributes to this pages cacheset. - * - * @param $humanReadable If the timestamp should be humand readable. - * - * @return an associative array of file, and timestamp - */ - function cacheSet($humanReadable = False) - { - $retVal = array(); - foreach ($this->parentCaches as $parent) { - $retVal = array_merge($retVal, $parent->cacheSet($humanReadable)); - } - foreach($this->files as $file) { - $mtime = filemtime($file); - if ($humanReadable) - $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; - - $retVal[$file] = $mtime; - } - return $retVal; - } - - /** - * Include a file in the cacheset - * - * @param $path the path of the file - */ - function cache_time($path) - { - if (!file_exists($path)) { - Logger::warn("${path} does not exist"); - errorPage("Resource is not available"); - } - - array_push($this->files, $path); - $mtime = filemtime($path); - if ($mtime > $this->newest) { - $this->newest = $mtime; - } - } - - function getNewest() - { - $newest = 0; - foreach ($this->parentCaches as $parent) { - $newest = max($newest, $parent->getNewest()); - } - $newest = max($newest, $this->newest); - return $newest; - } -} - -class ScriptIncludeCache extends FileCacheSet -{ - private static $myInstance = 0; - - protected function __construct($filename = False) - { - parent::__construct(); - if ($filename) - $this->cache_time($filename); - $this->cache_time(__FILE__); - } - - /** - * Generates a singleton instance of this CacheTimeCheck - * - * @param $filename an optional file to include in the cacheset - * - * @return a CacheTimeCheck object - */ - function instance($filename = False) - { - if (! self::$myInstance) - self::$myInstance = new self($filename); - elseif ($filename) - self::$myInstance->cache_time($filename); - return self::$myInstance; - } - - /** - * Convenience function to include a file, and add it to the cacheset. - * - * @param $path path of the file - * @param $basedir a directory to prepend to the path - */ - function includeOnce($path, $basedir = false) - { - if ($basedir) - $path = $basedir . "/" . $path; - $this->cache_time($path); - include_once($path); - } -} +$cache->cache_time("${baseDir}/FileCacheSet.inc"); /** * CacheTimeCheck provides a set of functions to enable generating a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FileCacheSet.inc Thu Oct 11 23:32:14 2012 +0200 @@ -0,0 +1,119 @@ +<?php +$baseDir = dirname(__FILE__); + +include_once 'Logger.inc'; +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->cache_time("${baseDir}/Logger.inc"); + +class FileCacheSet { + private $newest = 0; + private $files = array(); + private $parentCaches = array(); + + protected function __construct($parent = null) { + if ($parent) { + $this->addParent($parent); + } + } + + protected function addParent($parent) + { + array_push($this->parentCaches, $parent); + } + + /** + * List a set of files which contributes to this pages cacheset. + * + * @param $humanReadable If the timestamp should be humand readable. + * + * @return an associative array of file, and timestamp + */ + function cacheSet($humanReadable = False) + { + $retVal = array(); + foreach ($this->parentCaches as $parent) { + $retVal = array_merge($retVal, $parent->cacheSet($humanReadable)); + } + foreach($this->files as $file) { + $mtime = filemtime($file); + if ($humanReadable) + $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; + + $retVal[$file] = $mtime; + } + return $retVal; + } + + /** + * Include a file in the cacheset + * + * @param $path the path of the file + */ + function cache_time($path) + { + if (!file_exists($path)) { + Logger::warn("${path} does not exist"); + errorPage("Resource is not available"); + } + + array_push($this->files, $path); + $mtime = filemtime($path); + if ($mtime > $this->newest) { + $this->newest = $mtime; + } + } + + function getNewest() + { + $newest = 0; + foreach ($this->parentCaches as $parent) { + $newest = max($newest, $parent->getNewest()); + } + $newest = max($newest, $this->newest); + return $newest; + } +} + +class ScriptIncludeCache extends FileCacheSet +{ + private static $myInstance = 0; + + protected function __construct($filename = False) + { + parent::__construct(); + if ($filename) + $this->cache_time($filename); + $this->cache_time(__FILE__); + } + + /** + * Generates a singleton instance of this CacheTimeCheck + * + * @param $filename an optional file to include in the cacheset + * + * @return a CacheTimeCheck object + */ + function instance($filename = False) + { + if (! self::$myInstance) + self::$myInstance = new self($filename); + elseif ($filename) + self::$myInstance->cache_time($filename); + return self::$myInstance; + } + + /** + * Convenience function to include a file, and add it to the cacheset. + * + * @param $path path of the file + * @param $basedir a directory to prepend to the path + */ + function includeOnce($path, $basedir = false) + { + if ($basedir) + $path = $basedir . "/" . $path; + $this->cache_time($path); + include_once($path); + } +} +?> \ No newline at end of file
--- a/InputParser.inc Thu Oct 11 23:16:53 2012 +0200 +++ b/InputParser.inc Thu Oct 11 23:32:14 2012 +0200 @@ -10,10 +10,9 @@ * Constructs a new InputParser object * * @param $name name of file to read configuration from - * @param $masterCache CacheTimeCheck cache object to use as parent for this inputParsercache * - * @todo masterCache is currently used as is, fix so linked caches - * may be used. + * @param $masterCache CacheTimeCheck cache object to use as parent + * for this inputParsercache */ function __construct($name, $masterCache) { $this->master = new DOMDocument();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ScriptIncludeCache.inc Thu Oct 11 23:32:14 2012 +0200 @@ -0,0 +1,9 @@ +<?php +/** + * @file + * + * Since ScriptIncludeCache is defined in FileCacheSet.inc, we simply + * include it here to provide a common class include interface + */ +include_once 'FileCacheSet.inc'; +?> \ No newline at end of file
--- a/common-functions.inc Thu Oct 11 23:16:53 2012 +0200 +++ b/common-functions.inc Thu Oct 11 23:32:14 2012 +0200 @@ -3,7 +3,7 @@ * @file * Functionality which doesn't belong anywhere else */ -include_once 'CacheTimeCheck.inc'; +include_once 'ScriptIncludeCache.inc'; /// @cond $baseDir = dirname(__FILE__);
--- a/flag.php Thu Oct 11 23:16:53 2012 +0200 +++ b/flag.php Thu Oct 11 23:32:14 2012 +0200 @@ -7,9 +7,9 @@ include_once 'CacheTimeCheck.inc'; -$cache = CacheTimeCheck::instance(__FILE__); -$cache->includeOnce('Language.inc'); -$cache->includeOnce('common-functions.inc'); +$scriptCache = ScriptIncludeCache::instance(__FILE__); +$scriptCache->includeOnce('Language.inc'); +$scriptCache->includeOnce('common-functions.inc'); $active = $_GET['active']; $lang = $_GET['lang']; @@ -30,6 +30,8 @@ $name .= "-active"; $name .= ".png"; +$cache = new CacheTimeCheck($name); +$cache->addParent($scriptCache); $cache->cache_time($name); $cache->CheckHttpModified();
--- a/sitemap.php Thu Oct 11 23:16:53 2012 +0200 +++ b/sitemap.php Thu Oct 11 23:32:14 2012 +0200 @@ -14,15 +14,12 @@ xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> '; -include_once 'CacheTimeCheck.inc'; +include_once 'ScriptIncludeCache.inc'; /// @cond $baseDir = dirname(__FILE__); -$cache = CacheTimeCheck::instance(__FILE__); +$cache = ScriptIncludeCache::instance(__FILE__); $cache->includeOnce('common-functions.inc', $baseDir); - - -$cache = CacheTimeCheck::instance(__FILE__); $cache->includeOnce('Options.inc', dirname(__FILE__)); /// @endcond
