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