Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ArrayCache.php
Go to the documentation of this file.
1<?php
3
7use Ubiquity\utils\base\UArray;
8
18class ArrayCache extends AbstractDataCache {
19
24 const PHP_TAG = "<?php\n";
25
30 private $_fileMode;
31
42 public function __construct($root, $postfix = '', $cacheParams = []) {
43 parent::__construct($root, $postfix);
44 $this->_fileMode = $cacheParams['fileMode'] ?? 0755;
45 }
46
47 public function init() {
48 if (! is_dir($this->_root)) {
49 \mkdir($this->_root, $this->_fileMode, true);
50 }
51 }
52
60 public function exists($key) {
61 return \file_exists($this->_getPath($key));
62 }
63
64 public function store($key, $code, $tag = null) {
65 $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 $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 } else {
79 throw new CacheException("Unable to create folder : {$dir}");
80 }
81 }
82
90 public function fetch($key) {
91 return include ($this->_getPath($key));
92 }
93
101 public function file_get_contents($key) {
102 return \file_get_contents($this->_getPath($key));
103 }
104
112 public function getTimestamp($key) {
113 return \filemtime($this->_getPath($key));
114 }
115
123 private function _getPath($key) {
124 return $this->_root . $key . $this->postfix . '.php';
125 }
126
132 public function remove($key) {
133 $file = $this->_getPath($key);
134 if (\file_exists($file))
135 return \unlink($file);
136 return false;
137 }
138
144 public function clear($matches = "") {
145 $files = glob($this->_root . $matches . '*');
146 foreach ($files as $file) {
147 if (\is_file($file))
148 \unlink($file);
149 }
150 }
151
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
170 public function clearCache($type) {
171 CacheFile::delete($this->_root . \strtolower($type));
172 }
173
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
190 public function getEntryKey($key) {
191 return UFileSystem::cleanFilePathname($this->_getPath($key));
192 }
193
199 public function setRoot($root) {
200 $this->_root = rtrim($root, \DS) . \DS;
201 }
202}
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.