Passed
Pull Request — master (#201)
by Jean-Christophe
26:40
created

ObjectCache   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Test Coverage

Coverage 61.73%

Importance

Changes 0
Metric Value
wmc 25
eloc 64
c 0
b 0
f 0
dl 0
loc 209
ccs 50
cts 81
cp 0.6173
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 13 4
A clear() 0 5 3
A __construct() 0 6 3
A getCacheInfo() 0 4 1
A file_get_contents() 0 2 1
A getEntryKey() 0 2 1
A fetch() 0 3 1
A _getPath() 0 3 1
A getTimestamp() 0 2 1
A clearCache() 0 2 1
A setRoot() 0 2 1
A exists() 0 2 1
A remove() 0 5 2
A getCacheFiles() 0 5 1
A getClassname() 0 9 1
A getCompleteClassname() 0 9 1
A getNamespace() 0 9 1
1
<?php
2
namespace Ubiquity\cache\system;
3
4
use Ubiquity\utils\base\UFileSystem;
5
use Ubiquity\exceptions\CacheException;
6
use Ubiquity\cache\CacheFile;
7
use Ubiquity\utils\base\UArray;
8
9
/**
10
 * This class is responsible for storing Arrays in a PHP classe.
11
 * Ubiquity\cache\system$ObjectCache
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.2
16
 *
17
 */
18
class ObjectCache extends AbstractDataCache {
19
20
	/**
21
	 *
22
	 * @var int The file mode used when creating new cache files
23
	 */
24
	private $_fileMode;
25
26
	private $_cacheDirectory;
27
28
	/**
29
	 * Initializes the file cache-provider
30
	 *
31
	 * @param string $root
32
	 *        	absolute path to the root-folder where cache-files will be stored
33
	 * @param string $postfix
34
	 *        	Termination of file names
35
	 * @param array $cacheParams
36
	 *        	defaults to ["fileMode"=>"0755"]
37
	 */
38 29
	public function __construct($root, $postfix = "", $cacheParams = []) {
39 29
		parent::__construct($root, $postfix);
40 29
		$this->_cacheDirectory = \basename($root);
41 29
		$this->_fileMode = (isset($cacheParams["fileMode"])) ? $cacheParams["fileMode"] : 0755;
42 29
		if (! is_dir($root)) {
43
			\mkdir($root, $this->_fileMode, true);
44
		}
45
	}
46
47
	/**
48
	 * Check if annotation-data for the key has been stored.
49
	 *
50
	 * @param string $key
51
	 *        	cache key
52
	 * @return boolean true if data with the given key has been stored; otherwise false
53
	 */
54 28
	public function exists($key) {
55 28
		return file_exists($this->_getPath($key));
56
	}
57
58 28
	public function store($key, $content, $tag = null) {
59 28
		$path = $this->_getPath($key);
60 28
		$dir = pathinfo($path, PATHINFO_DIRNAME);
61 28
		if (UFileSystem::safeMkdir($dir)) {
62 28
			$content = "<?php\n" . UArray::asPhpClass($content, $this->getClassname($key), $this->getNamespace($key), true);
63 28
			if (@\file_put_contents($path, $content, LOCK_EX) === false) {
64
				throw new CacheException("Unable to write cache file: {$path}");
65
			}
66 28
			if (@\chmod($path, $this->_fileMode) === false) {
67 28
				throw new CacheException("Unable to set permissions of cache file: {$path}");
68
			}
69
		} else {
70
			throw new CacheException("Unable to create folder : {$dir}");
71
		}
72
	}
73
74 28
	protected function getCompleteClassname($key) {
75 28
		$key = \str_replace([
76 28
			'.',
77 28
			'/'
78 28
		], [
79 28
			'_',
80 28
			'\\'
81 28
		], $key);
82 28
		return "\\{$this->_cacheDirectory}\\{$key}_cache";
83
	}
84
85 28
	protected function getNamespace($key) {
86 28
		$key = \str_replace([
87 28
			'.',
88 28
			'/'
89 28
		], [
90 28
			'_',
91 28
			'\\'
92 28
		], $key);
93 28
		return $this->_cacheDirectory . '\\' . \substr($key, 0, \strrpos($key, '\\'));
94
	}
95
96 28
	protected function getClassname($key) {
97 28
		$key = \str_replace([
98 28
			'.',
99 28
			'/'
100 28
		], [
101 28
			'_',
102 28
			'\\'
103 28
		], $key);
104 28
		return \substr($key, \strrpos($key, '\\') + 1) . '_cache';
105
	}
106
107
	/**
108
	 * Fetches data stored for the given key.
109
	 *
110
	 * @param string $key
111
	 *        	cache key
112
	 * @return mixed the cached data
113
	 */
114 28
	public function fetch($key) {
115 28
		$class = $this->getCompleteClassname($key);
116 28
		return $class::$value;
117
	}
118
119
	/**
120
	 * return data stored for the given key.
121
	 *
122
	 * @param string $key
123
	 *        	cache key
124
	 * @return mixed the cached data
125
	 */
126
	public function file_get_contents($key) {
127
		return \file_get_contents($this->_getPath($key));
128
	}
129
130
	/**
131
	 * Returns the timestamp of the last cache update for the given key.
132
	 *
133
	 * @param string $key
134
	 *        	cache key
135
	 * @return int unix timestamp
136
	 */
137
	public function getTimestamp($key) {
138
		return \filemtime($this->_getPath($key));
139
	}
140
141
	/**
142
	 * Maps a cache-key to the absolute path of a PHP file
143
	 *
144
	 * @param string $key
145
	 *        	cache key
146
	 * @return string absolute path of the PHP file
147
	 */
148 28
	private function _getPath($key) {
149 28
		$key = \str_replace('.', '_', $key);
150 28
		return $this->_root . $key . '_cache.php';
151
	}
152
153
	/**
154
	 *
155
	 * {@inheritdoc}
156
	 * @see \Ubiquity\cache\system\AbstractDataCache::remove()
157
	 */
158
	public function remove($key) {
159
		$file = $this->_getPath($key);
160
		if (\file_exists($file))
161
			return \unlink($file);
162
		return false;
163
	}
164
165
	/**
166
	 *
167
	 * {@inheritdoc}
168
	 * @see \Ubiquity\cache\system\AbstractDataCache::clear()
169
	 */
170
	public function clear($matches = "") {
171
		$files = glob($this->_root . $matches . '*');
172
		foreach ($files as $file) {
173
			if (\is_file($file))
174
				\unlink($file);
175
		}
176
	}
177
178
	/**
179
	 *
180
	 * {@inheritdoc}
181
	 * @see \Ubiquity\cache\system\AbstractDataCache::getCacheFiles()
182
	 */
183
	public function getCacheFiles($type) {
184
		return CacheFile::initFromFiles($this->_root . $type, \ucfirst($type), function ($file) {
185
			$path = UFileSystem::relativePath(dirname($file), $this->_root);
186
			$file = \basename($file);
187
			return $path . \DS . substr($file, 0, strpos($file, $this->postfix . '.php'));
188
		});
189
	}
190
191
	/**
192
	 *
193
	 * {@inheritdoc}
194
	 * @see \Ubiquity\cache\system\AbstractDataCache::clearCache()
195
	 */
196
	public function clearCache($type) {
197
		CacheFile::delete($this->_root . \strtolower($type));
198
	}
199
200
	/**
201
	 *
202
	 * {@inheritdoc}
203
	 * @see \Ubiquity\cache\system\AbstractDataCache::getCacheInfo()
204
	 */
205
	public function getCacheInfo() {
206
		$result = parent::getCacheInfo();
207
		$result .= "\nRoot cache directory is <b>" . UFileSystem::cleanPathname($this->_root) . "</b>.";
208
		return $result;
209
	}
210
211
	/**
212
	 *
213
	 * {@inheritdoc}
214
	 * @see \Ubiquity\cache\system\AbstractDataCache::getEntryKey()
215
	 */
216
	public function getEntryKey($key) {
217
		return UFileSystem::cleanFilePathname($this->_getPath($key));
218
	}
219
220
	/**
221
	 *
222
	 * {@inheritdoc}
223
	 * @see \Ubiquity\cache\system\AbstractDataCache::setRoot()
224
	 */
225 29
	public function setRoot($root) {
226 29
		$this->_root = \rtrim($root, \DS) . \DS;
227
	}
228
}
229