Browse Source

[FileCache] Add property to define cache folder

logmanoriginal 7 years ago
parent
commit
2d56b717cf
2 changed files with 30 additions and 9 deletions
  1. 29 9
      caches/FileCache.php
  2. 1 0
      index.php

+ 29 - 9
caches/FileCache.php

@@ -4,6 +4,7 @@
 */
 class FileCache implements CacheInterface {
 
+	protected $path;
 	protected $param;
 
 	public function loadData(){
@@ -32,7 +33,7 @@ class FileCache implements CacheInterface {
 
 	public function purgeCache(){
 		$cacheTimeLimit = time() - 86400; // 86400 -> 24h
-		$cachePath = $this->getCachePath();
+		$cachePath = $this->getPath();
 		if(file_exists($cachePath)){
 			$cacheIterator = new RecursiveIteratorIterator(
 			new RecursiveDirectoryIterator($cachePath),
@@ -51,6 +52,28 @@ class FileCache implements CacheInterface {
 	}
 
 	/**
+	* Set cache path
+	* @return self
+	*/
+	public function setPath($path){
+		if(is_null($path) || !is_string($path)){
+			throw new \Exception('The given path is invalid!');
+		}
+
+		$this->path = $path;
+
+		// Make sure path ends with '/' or '\'
+		$lastchar = substr($this->path, -1, 1);
+		if($lastchar !== '/' && $lastchar !== '\\')
+			$this->path .= '/';
+
+		if(!is_dir($this->path))
+			mkdir($this->path, 0755, true);
+
+		return $this;
+	}
+
+	/**
 	* Set HTTP GET parameters
 	* @return self
 	*/
@@ -64,15 +87,12 @@ class FileCache implements CacheInterface {
 	* Return cache path (and create if not exist)
 	* @return string Cache path
 	*/
-	protected function getCachePath(){
-		$cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ?
-
-		if(!is_dir($cacheDir)){
-			mkdir($cacheDir, 0755, true);
-			chmod($cacheDir, 0755);
+	protected function getPath(){
+		if(is_null($this->path)){
+			throw new \Exception('Call "setPath" first!');
 		}
 
-		return $cacheDir;
+		return $this->path;
 	}
 
 	/**
@@ -80,7 +100,7 @@ class FileCache implements CacheInterface {
 	* @return string Path to the file cache
 	*/
 	protected function getCacheFile(){
-		return $this->getCachePath() . $this->getCacheName();
+		return $this->getPath() . $this->getCacheName();
 	}
 
 	/**

+ 1 - 0
index.php

@@ -130,6 +130,7 @@ try {
 
 		// Initialize cache
 		$cache = Cache::create('FileCache');
+		$cache->setPath(__DIR__ . '/cache');
 		$cache->purgeCache();
 		$cache->setParameters($params);