'./cache'); // Read-only private $name; private $key; private $lifetime; // Private private $templateFile; private $template; private $path; private $validPath; public function __construct ($template) { // Accept both a string to the template file, or a XIRE_Template if ($template instanceof XIRE_Template) { $this->template = $template; $this->templateFile = $this->template->filename; } else { $this->templateFile = (string)$template; } $this->validPath = false; // Initialise name to the template file's base name $this->name = basename($this->templateFile); } public function __get ($name) { if ($name === 'template') { return $this->getTemplate(); } } public function __set ($name, $value) { switch ($name) { case 'name': if (!isset($this->name)) { $this->name = basename($this->templateFile); } else { strpos($name, '\\') === false or $name = str_replace('\\', '/', (string)$value); $this->name = (string)$value; } $this->validPath = false; break; case 'key': $this->key = (string)$value; $this->validPath = false; break; case 'lifetime': $this->lifetime = (int)$value; break; } } private function update () { if (strpos($this->name, '/') === false) { $this->path = self::$directories[''] . "/$this->name"; } else { list($cache, $path) = explode('/', $this->name, 2); isset(self::$directories[$cache]) or self::$directories[$cache] = self::$directories[''] . "/$cache"; $this->path = self::$directories[''] . "/$path"; } isset($this->key) and $this->path .= "/{$this->key}"; $this->path .= '.xml'; } /** * Check if a valid, i.e. cache exists and has not expired */ public function exists () { $this->validPath or $this->update(); // Return false unless file exists and either lifetime is unset or has not expired if (file_exists($this->path) && (!isset($this->lifetime) || time() <= filemtime($this->path) + $this->lifetime)) { return true; } return false; } /** * Get the filename of a valid cache file */ public function getFile () { $this->validPath or $this->update(); $this->exists() or $this->make(); return $this->path; } /** * @return the XIRE_Template object used by this instance */ public function getTemplate () { if (!isset($this->template)) { // Create a template object if it does not exist require_once '../lib/Template.class.php'; $this->template = new XIRE_Template($this->templateFile); } return $this->template; } /** * Create the cache version (ignores whethor or not the cache is valid) */ public function make () { // Make sure that the environment is good $this->validPath or $this->update(); if (!is_dir(dirname($this->path)) && @mkdir(dirname($this->path), 0755, true) === false) { throw new XIRE_CouldNotCreateCacheException('Unable to create parent folder'); } isset($this->template) or $this->getTemplate(); $this->template->process(); $this->template->save($this->path); } /** * Load the cache file into the template's DOM document */ public function load() { isset($this->template) or $this->getTemplate(); if(!@$this->template->document->load($this->getFile()) { throw new XIRE_InvalidFileException('Unable to load cache'); } } /** * Set a directory to store caches. * @param string $cache caches with names that begin with '$cache/' will be stored in * $directory * @param string $directory */ public static function setCacheDirectory ($cache, $directory) { self::$directories[$cache] = $directory; } } class XIRE_CouldNotCreateCacheException extends XIRE_Exception{} ?>