Completed
Push — master ( 4684e6...35e6c1 )
by Jean-Christophe
01:54
created

ArrayCache::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Inspired by (c) Rasmus Schultz <[email protected]>
4
 * <https://github.com/mindplay-dk/php-annotations>
5
 */
6
namespace micro\cache;
7
/**
8
 * This class is responsible for storing Arrays in PHP files.
9
 */
10
class ArrayCache{
11
	/**
12
	 * @var string The PHP opening tag (used when writing cache files)
13
	 */
14
	const PHP_TAG = "<?php\n";
15
16
	/**
17
	 * @var int The file mode used when creating new cache files
18
	 */
19
	private $_fileMode;
20
21
	/**
22
	 * @var string Absolute path to a folder where cache files will be created
23
	 */
24
	private $_root;
25
26
	/**
27
	 * @var string Termination of file names
28
	 */
29
	private $postfix;
30
31
	/**
32
	 * Initializes the file cache-provider
33
	 * @param string $root absolute path to the root-folder where cache-files will be stored
34
	 * @param string Termination of file names
35
	 * @param int $fileMode file creation mode; defaults to 0777
36
	 */
37
	public function __construct($root, $postfix="",$fileMode = 0777){
38
		$this->_root = $root;
39
		$this->_fileMode = $fileMode;
40
		$this->postfix=$postfix;
41
		if(!is_dir($root))
42
			return mkdir($root,$fileMode,true);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
43
	}
44
45
	/**
46
	 * Check if annotation-data for the key has been stored.
47
	 * @param string $key cache key
48
	 * @return bool true if data with the given key has been stored; otherwise false
49
	 */
50
	public function exists($key){
51
		return file_exists($this->_getPath($key));
52
	}
53
54
	/**
55
	 * Caches the given data with the given key.
56
	 * @param string $key cache key
57
	 * @param array $code the source-code to be cached
58
	 * @throws AnnotationException if file could not be written
59
	 */
60
	public function store($key, $code){
61
		$path = $this->_getPath($key);
62
		$content = self::PHP_TAG . $code . "\n";
63
		if (@file_put_contents($path, $content, LOCK_EX) === false) {
64
			throw new \Exception("Unable to write cache file: {$path}");
65
		}
66
		if (@chmod($path, $this->_fileMode) === false) {
67
			throw new \Exception("Unable to set permissions of cache file: {$path}");
68
		}
69
	}
70
71
	/**
72
	 * Fetches data stored for the given key.
73
	 * @param string $key cache key
74
	 * @return mixed the cached data
75
	 */
76
	public function fetch($key){
77
		return include($this->_getPath($key));
78
	}
79
80
	/**
81
	 * Returns the timestamp of the last cache update for the given key.
82
	 *
83
	 * @param string $key cache key
84
	 * @return int unix timestamp
85
	 */
86
	public function getTimestamp($key){
87
		return filemtime($this->_getPath($key));
88
	}
89
90
	/**
91
	 * Maps a cache-key to the absolute path of a PHP file
92
	 *
93
	 * @param string $key cache key
94
	 * @return string absolute path of the PHP file
95
	 */
96
	private function _getPath($key){
97
		return $this->_root . DIRECTORY_SEPARATOR . $key .$this->postfix.'.php';
98
	}
99
100
	public function remove($key){
101
		$file=$this->_getPath($key);
102
		if(\file_exists($file))
103
			unlink($file);
104
	}
105
106
	public function clear(){
107
		$files = glob($this->_root.'/*');
108
		foreach($files as $file){
109
			if(is_file($file))
110
				unlink($file);
111
		}
112
	}
113
114
	/**
115
	 * @return string absolute path of the folder where cache files are created
116
	 */
117
	public function getRoot(){
118
		return $this->_root;
119
	}
120
}