<?php
namespace gnomephp\cache;
/**
* Used to cache views.
*
* @author peec
*
*/
class ViewCache{
private $viewName;
private $suffix;
private $cachePath;
private $urlAppendix;
/**
* Constructs a view cache object.
* @param string $viewName The view name ( relative or absolute path )
* @param string $suffix File extension
* @param string $cachePath Where to store cache files.
* @param string $urlAppendix Salt or url appendix.
*/
public function __construct($viewName, $suffix, $cachePath, $urlAppendix){
$this->viewName = $viewName;
$this->suffix = $suffix;
$this->cachePath = $cachePath;
$this->urlAppendix = $urlAppendix;
}
/**
* Gets the cache path ( can be both absolute and non-absolute )
*/
public function getViewName(){
return $this->viewName;
}
/**
* Gets the suffix ( file extension ) of the cache file.
*/
public function getSuffix(){
return $this->suffix;
}
/**
* Gets the cache path.
*/
public function getCachePath(){
return $this->cachePath;
}
/**
* Gets the salt of the cache file.
*/
public function getUrlAppendix(){
return $this->urlAppendix;
}
/**
* Gets the cache file name
*/
public function getCacheFile(){
return (string)($this->cachePath . DIRECTORY_SEPARATOR . md5((string)$this->urlAppendix). '_' . md5($this->viewName) . $this->suffix);
}
/**
* Puts content into this file.
* Contents are overridden.
* @param string $content The content to put in the file.
*/
public function put($content){
return file_put_contents($this->getCacheFile(), $content);
}
/**
* Appends content to the cached file.
* @param string $content The content to append to the file.
*/
public function append($content){
return file_put_contents($this->getCacheFile(), $content, FILE_APPEND);
}
/**
* Prepends content to the file.
* @param string $content The content to prepend to the file.
*/
public function prepend($content){
return file_put_contents($this->getCacheFile(), @file_get_contents($this->getCacheFile()) . $content);
}
/**
* Delets the cache file.
*/
public function delete(){
return @unlink($this->getCacheFile());
}
}