Passed
Push — master ( ed813f...432706 )
by Jean-Christophe
02:43
created

ArrayCache::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 3
dl 0
loc 5
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\cache\system;
4
5
use Ubiquity\controllers\admin\popo\CacheFile;
6
use Ubiquity\utils\base\UFileSystem;
7
use Ubiquity\exceptions\CacheException;
8
9
/**
10
 * This class is responsible for storing Arrays in PHP files.
11
 */
12
class ArrayCache extends AbstractDataCache {
13
	/**
14
	 *
15
	 * @var int The file mode used when creating new cache files
16
	 */
17
	private $_fileMode;
18
19
	/**
20
	 * Initializes the file cache-provider
21
	 *
22
	 * @param string $root
23
	 *        	absolute path to the root-folder where cache-files will be stored
24
	 * @param string $postfix
25
	 *        	Termination of file names
26
	 * @param array $cacheParams
27
	 *        	defaults to ["fileMode"=>"0755"]
28
	 */
29 1
	public function __construct($root, $postfix = "", $cacheParams = []) {
30 1
		parent::__construct ( $root, $postfix );
31 1
		$this->_fileMode = (isset ( $cacheParams ["fileMode"] )) ? $cacheParams ["fileMode"] : 0755;
32 1
		if (! is_dir ( $root ))
33
			\mkdir ( $root, $this->_fileMode, true );
34 1
	}
35
36
	/**
37
	 * Check if annotation-data for the key has been stored.
38
	 *
39
	 * @param string $key
40
	 *        	cache key
41
	 * @return boolean true if data with the given key has been stored; otherwise false
42
	 */
43 11
	public function exists($key) {
44 11
		return file_exists ( $this->_getPath ( $key ) );
45
	}
46
47
	/**
48
	 * Caches the given data with the given key.
49
	 *
50
	 * @param string $key
51
	 *        	cache key
52
	 * @param string $content
53
	 *        	the source-code to be cached
54
	 * @throws CacheException if file could not be written
55
	 */
56
	protected function storeContent($key, $content, $tag) {
57
		$path = $this->_getPath ( $key );
58
		$dir = pathinfo ( $path, PATHINFO_DIRNAME );
59
		if (UFileSystem::safeMkdir ( $dir )) {
60
			if (@\file_put_contents ( $path, $content, LOCK_EX ) === false) {
61
				throw new CacheException ( "Unable to write cache file: {$path}" );
62
			}
63
			if (@\chmod ( $path, $this->_fileMode ) === false) {
64
				throw new CacheException ( "Unable to set permissions of cache file: {$path}" );
65
			}
66
		} else {
67
			throw new CacheException ( "Unable to create folder : {$dir}" );
68
		}
69
	}
70
71
	/**
72
	 * Fetches data stored for the given key.
73
	 *
74
	 * @param string $key
75
	 *        	cache key
76
	 * @return mixed the cached data
77
	 */
78 13
	public function fetch($key) {
79 13
		return include ($this->_getPath ( $key ));
80
	}
81
82
	/**
83
	 * return data stored for the given key.
84
	 *
85
	 * @param string $key
86
	 *        	cache key
87
	 * @return mixed the cached data
88
	 */
89
	public function file_get_contents($key) {
90
		return \file_get_contents ( $this->_getPath ( $key ) );
91
	}
92
93
	/**
94
	 * Returns the timestamp of the last cache update for the given key.
95
	 *
96
	 * @param string $key
97
	 *        	cache key
98
	 * @return int unix timestamp
99
	 */
100
	public function getTimestamp($key) {
101
		return \filemtime ( $this->_getPath ( $key ) );
102
	}
103
104
	/**
105
	 * Maps a cache-key to the absolute path of a PHP file
106
	 *
107
	 * @param string $key
108
	 *        	cache key
109
	 * @return string absolute path of the PHP file
110
	 */
111 13
	private function _getPath($key) {
112 13
		return $this->_root . $key . $this->postfix . '.php';
113
	}
114
115
	/**
116
	 *
117
	 * {@inheritdoc}
118
	 * @see \Ubiquity\cache\system\AbstractDataCache::remove()
119
	 */
120
	public function remove($key) {
121
		$file = $this->_getPath ( $key );
122
		if (\file_exists ( $file ))
123
			return \unlink ( $file );
124
		return false;
125
	}
126
127
	/**
128
	 *
129
	 * {@inheritdoc}
130
	 * @see \Ubiquity\cache\system\AbstractDataCache::clear()
131
	 */
132
	public function clear($matches = "") {
133
		$files = glob ( $this->_root . $matches . '*' );
134
		foreach ( $files as $file ) {
135
			if (\is_file ( $file ))
136
				\unlink ( $file );
137
		}
138
	}
139
140
	/**
141
	 *
142
	 * {@inheritdoc}
143
	 * @see \Ubiquity\cache\system\AbstractDataCache::getCacheFiles()
144
	 */
145
	public function getCacheFiles($type) {
146
		return CacheFile::initFromFiles ( $this->_root . $type, \ucfirst ( $type ), function ($file) use ($type) {
147
			$file = \basename ( $file );
148
			return $type . \DS . substr ( $file, 0, strpos ( $file, $this->postfix . '.php' ) );
149
		} );
150
	}
151
152
	/**
153
	 *
154
	 * {@inheritdoc}
155
	 * @see \Ubiquity\cache\system\AbstractDataCache::clearCache()
156
	 */
157
	public function clearCache($type) {
158
		CacheFile::delete ( $this->_root . \strtolower ( $type ) );
159
	}
160
161
	/**
162
	 *
163
	 * {@inheritdoc}
164
	 * @see \Ubiquity\cache\system\AbstractDataCache::getCacheInfo()
165
	 */
166
	public function getCacheInfo() {
167
		$result = parent::getCacheInfo ();
168
		$result .= "<br>Root cache directory is <b>" . UFileSystem::cleanPathname ( $this->_root ) . "</b>.";
169
		return $result;
170
	}
171
172
	/**
173
	 *
174
	 * {@inheritdoc}
175
	 * @see \Ubiquity\cache\system\AbstractDataCache::getEntryKey()
176
	 */
177
	public function getEntryKey($key) {
178
		return UFileSystem::cleanFilePathname ( $this->_getPath ( $key ) );
179
	}
180
181
	/**
182
	 *
183
	 * {@inheritdoc}
184
	 * @see \Ubiquity\cache\system\AbstractDataCache::setRoot()
185
	 */
186 1
	public function setRoot($root) {
187 1
		$this->_root = rtrim ( $root, \DS ) . \DS;
188 1
	}
189
}
190