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