Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ObjectCache.php
Go to the documentation of this file.
1<?php
3
7use Ubiquity\utils\base\UArray;
8
18class ObjectCache extends AbstractDataCache {
19
24 private $_fileMode;
25
26 private $_cacheDirectory;
27
38 public function __construct($root, $postfix = "", $cacheParams = []) {
39 parent::__construct($root, $postfix);
40 $this->_cacheDirectory = \basename($root);
41 $this->_fileMode = (isset($cacheParams["fileMode"])) ? $cacheParams["fileMode"] : 0755;
42 if (! is_dir($root)) {
43 \mkdir($root, $this->_fileMode, true);
44 }
45 }
46
54 public function exists($key) {
55 return file_exists($this->_getPath($key));
56 }
57
58 public function store($key, $content, $tag = null) {
59 $path = $this->_getPath($key);
60 $dir = pathinfo($path, PATHINFO_DIRNAME);
61 if (UFileSystem::safeMkdir($dir)) {
62 $content = "<?php\n" . UArray::asPhpClass($content, $this->getClassname($key), $this->getNamespace($key), true);
63 if (@\file_put_contents($path, $content, LOCK_EX) === false) {
64 throw new CacheException("Unable to write cache file: {$path}");
65 }
66 if (@\chmod($path, $this->_fileMode) === false) {
67 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 protected function getCompleteClassname($key) {
75 $key = \str_replace([
76 '.',
77 '/'
78 ], [
79 '_',
80 '\\'
81 ], $key);
82 return "\\{$this->_cacheDirectory}\\{$key}_cache";
83 }
84
85 protected function getNamespace($key) {
86 $key = \str_replace([
87 '.',
88 '/'
89 ], [
90 '_',
91 '\\'
92 ], $key);
93 return $this->_cacheDirectory . '\\' . \substr($key, 0, \strrpos($key, '\\'));
94 }
95
96 protected function getClassname($key) {
97 $key = \str_replace([
98 '.',
99 '/'
100 ], [
101 '_',
102 '\\'
103 ], $key);
104 return \substr($key, \strrpos($key, '\\') + 1) . '_cache';
105 }
106
114 public function fetch($key) {
115 $class = $this->getCompleteClassname($key);
116 return $class::$value;
117 }
118
126 public function file_get_contents($key) {
127 return \file_get_contents($this->_getPath($key));
128 }
129
137 public function getTimestamp($key) {
138 return \filemtime($this->_getPath($key));
139 }
140
148 private function _getPath($key) {
149 $key = \str_replace('.', '_', $key);
150 return $this->_root . $key . '_cache.php';
151 }
152
158 public function remove($key) {
159 $file = $this->_getPath($key);
160 if (\file_exists($file))
161 return \unlink($file);
162 return false;
163 }
164
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
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
196 public function clearCache($type) {
197 CacheFile::delete($this->_root . \strtolower($type));
198 }
199
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
216 public function getEntryKey($key) {
217 return UFileSystem::cleanFilePathname($this->_getPath($key));
218 }
219
225 public function setRoot($root) {
226 $this->_root = \rtrim($root, \DS) . \DS;
227 }
228}
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.