Completed
Push — master ( 7c92dc...c428b0 )
by Jean-Christophe
01:30
created

ArrayCache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 97
rs 10

9 Methods

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