Files.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 0bw/QZMZrSQrLiFWAjA3TJrsMCDCl4mtl7AUUiFbW6w=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.01]
  11. */
  12. namespace Cube\Cache\Adapter;
  13. class Files extends AbstractAdapter
  14. {
  15. /**
  16. *
  17. * cache folder
  18. *
  19. * @var string
  20. */
  21. protected $_folder;
  22. /**
  23. *
  24. * class constructor
  25. *
  26. * @param array $options configuration array
  27. *
  28. * @throws \RuntimeException
  29. */
  30. public function __construct($options = array())
  31. {
  32. parent::__construct($options);
  33. if (!isset($options['folder'])) {
  34. throw new \RuntimeException("Cache folder not specified.");
  35. }
  36. $this->_folder = $options['folder'];
  37. }
  38. /**
  39. *
  40. * get cache folder
  41. *
  42. * @return string
  43. */
  44. public function getFolder()
  45. {
  46. return $this->_folder;
  47. }
  48. /**
  49. *
  50. * set cache folder
  51. *
  52. * @param string $folder
  53. *
  54. * @return $this
  55. * @throws \InvalidArgumentException
  56. */
  57. public function setFolder($folder)
  58. {
  59. if (!file_exists($folder)) {
  60. throw new \InvalidArgumentException(
  61. sprintf("The cache folder '%s' could not be found.", $folder));
  62. }
  63. $this->_folder = $folder;
  64. return $this;
  65. }
  66. /**
  67. *
  68. * reads the contents of a cache file and returns the output or false if the file could not be found
  69. *
  70. * @param string $name
  71. * @param string $type
  72. *
  73. * @return string|false
  74. */
  75. public function read($name, $type)
  76. {
  77. $cacheFile = $this->_cacheFileName($name, $type);
  78. if (!file_exists($cacheFile)) {
  79. return false;
  80. }
  81. $contents = file_get_contents($cacheFile);
  82. return ($this->_serialization === true) ? unserialize($contents) : $contents;
  83. }
  84. /**
  85. *
  86. * create/update cache file
  87. *
  88. * @param string $name
  89. * @param string $type
  90. * @param mixed $data
  91. * @param int $expires
  92. *
  93. * @return $this
  94. * @throws \RuntimeException
  95. */
  96. public function write($name, $type, $data, $expires = null)
  97. {
  98. $cacheFile = $this->_cacheFileName($name, $type);
  99. if (!$fp = fopen($cacheFile, 'w')) {
  100. throw new \RuntimeException(
  101. sprintf("Could not open cache file '%s'.", $name));
  102. }
  103. if (!flock($fp, LOCK_EX)) {
  104. throw new \RuntimeException(
  105. sprintf("Could not lock cache file '%s'.", $name));
  106. }
  107. if ($this->_serialization === true) {
  108. $data = serialize($data);
  109. }
  110. if (!fwrite($fp, $data)) {
  111. throw new \RuntimeException(
  112. sprintf("Could not write to cache file '%s'.", $name));
  113. }
  114. flock($fp, LOCK_UN);
  115. fclose($fp);
  116. return $this;
  117. }
  118. /**
  119. *
  120. * delete a variable from cache
  121. *
  122. * @param string $name
  123. * @param string $type
  124. *
  125. * @return boolean
  126. */
  127. public function delete($name, $type)
  128. {
  129. $cacheFile = $this->_cacheFileName($name, $type);
  130. if (file_exists($cacheFile)) {
  131. unlink($cacheFile);
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. *
  138. * purge cache variables
  139. *
  140. * @param string $type
  141. * @param boolean $force
  142. *
  143. * @return $this
  144. */
  145. public function purge($type, $force = false)
  146. {
  147. $cacheFolder = $this->_folder . DIRECTORY_SEPARATOR . $type;
  148. foreach (glob($cacheFolder . '/*') as $file) {
  149. if ((filemtime($file) < time() - $this->_expires) || $force === true) {
  150. @unlink($file);
  151. }
  152. }
  153. return $this;
  154. }
  155. /**
  156. *
  157. * clear cache
  158. *
  159. * @return $this
  160. */
  161. public function clear()
  162. {
  163. $this->purge(self::ROUTES, true)
  164. ->purge(self::METADATA, true)
  165. ->purge(self::QUERIES, true);
  166. return $this;
  167. }
  168. /**
  169. *
  170. * generate full cache file path
  171. *
  172. * @param string $name
  173. * @param string $type
  174. *
  175. * @return string
  176. */
  177. protected function _cacheFileName($name, $type)
  178. {
  179. return $this->_folder . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $name;
  180. }
  181. }