Completed
Push — master ( 4304d8...a737ee )
by Jean-Christophe
01:37
created

ArrayCache::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace micro\cache\system;
3
4
use micro\controllers\admin\popo\CacheFile;
5
use micro\cache\CacheManager;
6
7
/**
8
 * This class is responsible for storing Arrays in PHP files.
9
 */
10
class ArrayCache extends AbstractDataCache{
11
	/**
12
	 *
13
	 * @var int The file mode used when creating new cache files
14
	 */
15
	private $_fileMode;
16
17
	/**
18
	 * Initializes the file cache-provider
19
	 * @param string $root absolute path to the root-folder where cache-files will be stored
20
	 * @param string Termination of file names
21
	 * @param int $fileMode file creation mode; defaults to 0777
22
	 */
23
	public function __construct($root, $postfix="", $fileMode=0777) {
24
		parent::__construct($root,$postfix);
25
		$this->_fileMode=$fileMode;
26
		if (!is_dir($root))
27
			\mkdir($root, $fileMode, true);
28
	}
29
30
	/**
31
	 * Check if annotation-data for the key has been stored.
32
	 * @param string $key cache key
33
	 * @return boolean true if data with the given key has been stored; otherwise false
34
	 */
35
	public function exists($key) {
36
		return file_exists($this->_getPath($key));
37
	}
38
39
	/**
40
	 * Caches the given data with the given key.
41
	 * @param string $key cache key
42
	 * @param string $content the source-code to be cached
43
	 * @throws AnnotationException if file could not be written
44
	 */
45
	protected function storeContent($key,$content,$tag) {
46
		$path=$this->_getPath($key);
47
		if (@\file_put_contents($path, $content, LOCK_EX) === false) {
48
			throw new \Exception("Unable to write cache file: {$path}");
49
		}
50
		if (@\chmod($path, $this->_fileMode) === false) {
51
			throw new \Exception("Unable to set permissions of cache file: {$path}");
52
		}
53
	}
54
55
	/**
56
	 * Fetches data stored for the given key.
57
	 * @param string $key cache key
58
	 * @return mixed the cached data
59
	 */
60
	public function fetch($key) {
61
		return include ($this->_getPath($key));
62
	}
63
64
	/**
65
	 * return data stored for the given key.
66
	 * @param string $key cache key
67
	 * @return mixed the cached data
68
	 */
69
	public function file_get_contents($key) {
70
		return \file_get_contents($this->_getPath($key));
71
	}
72
73
	/**
74
	 * Returns the timestamp of the last cache update for the given key.
75
	 *
76
	 * @param string $key cache key
77
	 * @return int unix timestamp
78
	 */
79
	public function getTimestamp($key) {
80
		return \filemtime($this->_getPath($key));
81
	}
82
83
	/**
84
	 * Maps a cache-key to the absolute path of a PHP file
85
	 *
86
	 * @param string $key cache key
87
	 * @return string absolute path of the PHP file
88
	 */
89
	private function _getPath($key) {
90
		return $this->_root . DIRECTORY_SEPARATOR . $key . $this->postfix . '.php';
91
	}
92
93
	public function remove($key) {
94
		$file=$this->_getPath($key);
95
		if (\file_exists($file))
96
			\unlink($file);
97
	}
98
99
	public function clear() {
100
		$files=glob($this->_root . '/*');
101
		foreach ( $files as $file ) {
102
			if (\is_file($file))
103
				\unlink($file);
104
		}
105
	}
106
107
	public function getCacheFiles($type){
108
		return CacheFile::initFromFiles(ROOT . DS .CacheManager::getCacheDirectory().$type, \ucfirst($type),function($file) use($type){$file=\basename($file);return $type."/".substr($file, 0, strpos($file, $this->postfix.'.php'));});
109
	}
110
111
	public function clearCache($type){
112
		CacheFile::delete(ROOT . DS .CacheManager::getCacheDirectory().\strtolower($type));
113
	}
114
115
	public function getCacheInfo(){
116
		$result=parent::getCacheInfo();
117
		$result.="<br>Root cache directory is <b>".$this->_root."</b>.";
118
		return $result;
119
	}
120
}
121