/src/Ubiquity/cache/system/ArrayCache.php

https://github.com/phpMv/ubiquity · PHP · 202 lines · 86 code · 19 blank · 97 comment · 8 complexity · c99bd67b48251f2f7d2fe4b0ef249783 MD5 · raw file

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