Location: PHPKode > scripts > XML Cacher > xml-cacher/xmlCacher.class.php
<?php

/**
* Xml Cacher class
*
* It manage the cache for XML and xslt transformation
*
* @autor David Bigand <hide@address.com>;
* @version 1.0
* @access public
* @package xmlCacher
*/



//Includes
require_once("Cache.php");
//require_once("benchTimer.class.php");

class xmlCacher {
	
	private $xmlData;
	private $xsltData;
	private $xsltFile;
	private $cahedOutputData;
	private $cahedOutputGroup = "";
	private $xmlCacheDir = "/tmp/cache/xml/";
	private $outputCacheDir = "/tmp/cache/output/";
	
	private $xsltDir;
	private $group;
	
	private $xmlTimeLimit = 3600;
	private $outputTimeLimit = 3600;
	
	protected $objCacheXml;
	protected $objCacheOutput;
	
	const CACHETYPE = "file";
	private $idCache;
	
	
	
	/**
    * Constructor
    *
    * Build DomXml object with require path for xml caching, and output Caching
    *
    * @param string $xmlPath - Path of the cache directory for xmlFiles (optional)
    * @param string $outputPath - Path of the cache directory for ouput Files (optional)
    * @return void
    * @access public
    * @see 
    */
	public function __construct($xmlPath=false, $outputPath=false) {
		if($xmlPath) $this->setXmlCacheDir($xmlPath);
		if($outputPath) $this->setOutputCacheDir($outputPath);
		$this->objCacheXml = new Cache(self::CACHETYPE, array("cache_dir"=>$this->xmlCacheDir));
		$this->objCacheOutput = new Cache(self::CACHETYPE, array("cache_dir"=>$this->outputCacheDir));
	}
	
	/**
    * setXmlTimeLimit
    *
    * Set the cache time limit only for xml Files
    *
    * @param integer - number of seconde to be cahed
    * @return void, throw Exception on error
    * @access public
    * @see 
    */
	public function setxmlTimeLimit($sec) {
		if(!empty($sec)) {
			$this->xmlTimeLimit = $sec;
		} else {
			$except = new Exception("No param given",1001);
			throw $except;
		}
	}
	/**
    * getxmlTimeLimit
    *
    * Return the xml cache Time limit 
    *
    * @param 
    * @return xmlTimeLimit
    * @access public
    * @see 
    */
	public function getxmlTimeLimit() {
		return $this->xmlTimeLimit;
	}
	
	/**
    * setGroup
    *
    * Set cache group
    *
    * @param string $group - a name for group caching
    * @return void, throw Exception on error
    * @access public
    * @see 
    */
	protected  function setGroup($group) {
		if(!empty($group)) {
			$this->group = $group;
		} else {
			$except = new Exception("No param given",1001);
			throw $except;
		}
	}
	/**
    * getGroup
    *
    * Return the cache group
    *
    * @param 
    * @return string group
    * @access public
    * @see 
    */
	protected function getGroup() {
		return $this->group;
	}
	
	
	/**
    * setXsltDir
    *
    * Set xlst directory
    *
    * @param string - the path
    * @return void
    * @access public
    * @see 
    */
	protected function setXsltDir($path) {
		try {
			$path = ereg("\/$",$path)?$path:$path."/";
			if($this->checkIsDirectory($path)) {
				$this->xsltDir = $path;
			}
		} catch (Exception $e) {
			throw $e;
		}
	}
	/**
    * getxmlTimeLimit
    *
    * Return the xslt directory
    *
    * @param 
    * @return string - the path to xslt files
    * @access public
    * @see 
    */
	public function getXsltDir() {
		return $this->xsltDir;
	}
	
	/**
    * setXsltFile
    *
    * Set xlst directory
    *
    * @param string - the path
    * @return void
    * @access public
    * @see 
    */
    protected function setXsltFile($file) {
    	if(file_exists($this->getXsltDir().$file) && is_readable($this->getXsltDir().$file)) {
    		$this->xsltFile = $file;
    	} else {
    		$except = new Exception("Permission denied or file does not exist",1002);
    		throw $except;
    	}
    }
	/**
    * getxmlTimeLimit
    *
    * Return the xslt directory
    *
    * @param 
    * @return string - the path to xslt files
    * @access public
    * @see 
    */
	public function getXsltFile() {
		return $this->xsltFile;
	}
	
	
	/**
    * setoutputTimeLimit
    *
    * Set the cache time limit only for output Files
    *
    * @param $sec
    * @return 
    * @access public
    * @see 
    */
	public function setoutputTimeLimit($sec) {
		if(!empty($sec)) {
			$this->outputTimeLimit = $sec;
		} else {
			$except = new Exception("No param given",1001);
			throw $except;
		}
	}
	/**
    * getoutputTimeLimit
    *
    * Get the cache time limit only for output Files
    *
    * @param $sec
    * @return 
    * @access public
    * @see 
    */
	public function getoutputTimeLimit() {
		return $this->outputTimeLimit;
	}
	
	/**
    * setXmlCacheDir
    *
    * Set the cache directory only for xml Files
    *
    * @param $path
    * @return 
    * @access public
    * @see 
    */
	public function setXmlCacheDir ($path) {
		$path = ereg("\/$",$path)?$path:$path."/";
		try {
			if($this->checkIsDirectory($path)) {
				$this->xmlCacheDir = $path;
			}
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	
	/**
    * getXmlCacheDir
    *
    * Get the cache directory only for xml Files
    *
    * @param 
    * @return string - the path
    * @access public
    * @see 
    */
	public function getXmlCacheDir () {
		return $this->xmlCacheDir;
	}
	
	/**
    * setOutputCacheDir
    *
    * Set the cache directory only for output Files
    *
    * @param $path
    * @return
    * @access public
    * @see 
    */
	public function setOutputCacheDir ($path) {
		$path = ereg("\/$",$path)?$path:$path."/";
		try {
			if($this->checkIsDirectory($path)) {
				$this->outputCacheDir = $path;
			}
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	
	/**
    * getOutputCacheDir
    *
    * Get the cache directory only for output Files
    *
    * @param 
    * @return string - the path
    * @access public
    * @see 
    */
	public function getOutputCacheDir () {
		return $this->outputCacheDir;
	}
	
	
	/**
    * setXml
    *
    * Make a domXML object
    *
    * @param array - the xml 'DATA' or 'FILE' as assoc array
    * @return 
    * @access public
    * @see 
    */
    public function setXml ($xml) {
    	$this->xmlData = $this->setDomObj($xml);
    	$test = $this->xmlData->saveXML();
    	$this->objCacheXml->save($this->idCache,$test, time() + $this->getxmlTimeLimit(),$this->getGroup());
    }
	
    
    /**
    * setXSLT
    *
    * Make a domXML object
    *
    * @param array - the xml 'DATA' or 'FILE' as assoc array
    * @return 
    * @access public
    * @see 
    */
    protected function setXSLT ($xslt) {
    	$this->xsltData = $this->setDomObj($xslt);
    }
    
    /**
    * setXml
    *
    * Make a domXML object
    *
    * @param array - the xml 'DATA' or 'FILE' as assoc array
    * @return domDocument object
    * @access public
    * @see 
    */
    protected function setDomObj ($data) {
    	if(!empty($data)) {
    		reset ($data);
    		$type = key($data);
    		$value = current($data);
    		switch ($type)	{
    			case "DATA":
    			$domXml = new DOMDocument();
    			$domXml->LoadXML($value);
    			return $domXml;
    			break;
    			case "FILE":
    			$domXml = new DOMDocument();
    			$domXml->Load($value);
    			return $domXml;
    			break;
    		}
    	}
    }
	
	/**
    * isCachedXML 
    *
    * This function will check expire date and will get the xml file
    *
    * @param $name - the name you want
    * @param $xslt - the xslt file name
    * @param $group - the name of a caching group
    * @return boolean - tell if the file is still available and store it.
    * @access public
    * @see 
    */
	
    public function isCachedXML($name, $xslt,$group) {
    	//$timer = new BenchTimer();
    	$this->setGroup($group);
    	$this->idCache = $this->objCacheXml->generateID($name.$xslt);
    	
    	if(!$this->objCacheXml->isCached($this->idCache,$this->getGroup()) || $this->objCacheXml->isExpired($this->idCache,$this->getGroup())) {
    		return false;
    	} else {
    		//
    		return true;
    	}
    	//unset($timer);
    }
    
    /**
    * display 
    *
    * This function print the output buffer
    *
    * @param $name - the name you want
    * @param $xslt - the xslt file name
    * @param $xml - the xml data dynamicly created
    * @param $group - the name of a caching group
    * @return return to standard output
    * @access public
    * @see 
    */
    public function display ($name,$xslt,$xml,$group) {
    	
    	print $this->output($name,$xslt,$xml,$group);
    }
    
    /**
    * fetch 
    *
    * It gives the output file as variable for reuse
    *
    * @param $name - the name you want
    * @param $xslt - the xslt file name
    * @param $xml - the xml data dynamicly created
    * @param $group - the name of a caching group
    * @return return output file
    * @access public
    * @see 
    */
    public function fetch ($name,$xslt,$xml,$group) {
    	return $this->output($name,$xslt,$xml,$group);
    }
	
    /**
    * output 
    *
    * It gives the output file as variable for reuse
    *
    * @param $name - the name you want
    * @param $xslt - the xslt file name
    * @param $xml - the xml data dynamicly created
    * @param $group - the name of a caching group
    * @return return output file
    * @access public
    * @see 
    */
    private function output ($name,$xslt,$xml,$group) {
    	//$timer = new BenchTimer();
    	//Chech if the class have yet been used and fullfill
    	if(!isset($this->cahedOutputData)) {
    		//Generate the cache ID
    		$this->idCache = $this->objCacheOutput->generateID($name.$xslt);
    		//Check if the output file is in the cache
    		if(!$this->cahedOutputData = $this->objCacheOutput->get($this->idCache,$this->getGroup())) {
    			
    			//set theXsltFile name
    			$this->setXsltFile($xslt);

    			//Get and save the xmlFile if need
    			
    			if(!$xmlFile = $this->objCacheXml->get($this->idCache,$this->getGroup())) {
    				if(!isset($this->xmlData)) {
    					if(!empty($xml)) {
    						$this->setXml(array("DATA"=>$xml));
    					} else {
    						$except = new Exception("No Xml Data are set", 1003);
    						throw $except;
    					}
    				}
    			} else {
    				$this->setXml(array("DATA"=>$xmlFile));
    			}


    			// Start XSLT processor
    			$processor = new xsltProcessor();
    			//set xslt dom Object
    			$this->setXSLT(array("FILE"=>($this->getXsltDir().$this->getXsltFile())));
    			//import xslt into the processor
    			$processor->importStylesheet($this->xsltData);

    			//Genere document
    			$this->cahedOutputData = $processor->transformToXml($this->xmlData);		// Stop XSLT processor
    			//Cache document
    			$this->objCacheOutput->save($this->idCache, $this->cahedOutputData, time() + $this->getoutputTimeLimit(),$this->getGroup());
    		}
    	}
    	//unset($timer);
    	return $this->cahedOutputData;
    	
    }

	
	/**
    * checkIsDirectory 
    *
    * Check if the path is really a directory and if it has all right on it
    *
    * @param $path sting $path = the path of the directory
    * @return boolean - throw error if need
    * @access public
    * @see 
    */
	protected function checkIsDirectory ($path) {
		if(!empty($path) && file_exists($path) && is_dir($path) && is_writeable($path)) {
			return true;
		} elseif (!empty($path) && !file_exists($path)) {
			$except = new Exception("This directory does not exists ",1002);
			throw $except;
			return false;
		} elseif (!empty($path) && file_exists($path) && !is_dir($path) ) {
			$except = new Exception("This is not a directory",1002);
			throw $except;
			return false;
		} elseif (!empty($path) && file_exists($path) && is_dir($path) && !is_writeable($path) ) {
			$except = new Exception("The directory is not writable",1002);
			throw $except;
			return false;
		} else {
			$except = new Exception("empty parameter",1001);
			throw $except;
			return false;
		}
	}
	/**
    * removeAllCached 
    *
    * Remove all cached files (xml and output)
    *
    * @param $name - the name you want
    * @param $xslt - the xslt file name
    * @param $group - the name of a caching group
    * @return void
    * @access public
    * @see 
    */
	public function removeAllCached ($name, $xslt,$group) {
		$this->setGroup($group);
		$this->idCache = $this->objCacheXml->generateID($name.$xslt);
		$this->objCacheOutput->remove($this->idCache,$this->getGroup());
		$this->objCacheXml->remove($this->idCache,$this->getGroup());
	}
	



}
?>
Return current item: XML Cacher