PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/PhpFastCache/Drivers/files.php

https://gitlab.com/webbroteam/satisfaction-mvc
PHP | 306 lines | 276 code | 6 blank | 24 comment | 16 complexity | e49a2fda0bc43d934ba0992666491165 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This file is part of phpFastCache.
  5. *
  6. * @license MIT License (MIT)
  7. *
  8. * For full copyright and license information, please see the docs/CREDITS.txt file.
  9. *
  10. * @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
  11. * @author Georges.L (Geolim4) <contact@geolim4.com>
  12. *
  13. */
  14. namespace phpFastCache\Drivers;
  15. use phpFastCache\Core\DriverAbstract;
  16. use phpFastCache\Exceptions\phpFastCacheDriverException;
  17. /**
  18. * Class files
  19. * @package phpFastCache\Drivers
  20. */
  21. class files extends DriverAbstract
  22. {
  23. /**
  24. * Init Cache Path
  25. * phpFastCache_files constructor.
  26. * @param array $config
  27. * @throws phpFastCacheDriverException
  28. */
  29. public function __construct($config = array())
  30. {
  31. $this->setup($config);
  32. $this->getPath(); // force create path
  33. if (!$this->checkdriver() && !isset($config[ 'skipError' ])) {
  34. throw new phpFastCacheDriverException("Can't use this driver for your website!");
  35. }
  36. }
  37. /**
  38. * @return bool
  39. */
  40. public function checkdriver()
  41. {
  42. if (is_writable($this->getPath())) {
  43. return true;
  44. }/* else {
  45. }*/
  46. return false;
  47. }
  48. /**
  49. * @param $keyword
  50. * @param bool $skip
  51. * @return string
  52. * @throws phpFastCacheDriverException
  53. */
  54. private function getFilePath($keyword, $skip = false)
  55. {
  56. $path = $this->getPath();
  57. $filename = $this->encodeFilename($keyword);
  58. $folder = substr($filename, 0, 2);
  59. $path = rtrim($path, '/') . '/' . $folder;
  60. /**
  61. * Skip Create Sub Folders;
  62. */
  63. if ($skip == false) {
  64. if (!file_exists($path)) {
  65. if (!mkdir($path, $this->__setChmodAuto())) {
  66. throw new phpFastCacheDriverException('PLEASE CHMOD ' . $this->getPath() . ' - 0777 OR ANY WRITABLE PERMISSION!', 92);
  67. }
  68. }
  69. }
  70. return $path . '/' . $filename . '.txt';
  71. }
  72. /**
  73. * @param $keyword
  74. * @param string $value
  75. * @param int $time
  76. * @param array $option
  77. * @return bool
  78. * @throws \Exception
  79. */
  80. public function driver_set($keyword, $value = '', $time = 300, $option = array())
  81. {
  82. $file_path = $this->getFilePath($keyword);
  83. $data = $this->encode($value);
  84. $toWrite = true;
  85. /*
  86. * Skip if Existing Caching in Options
  87. */
  88. if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true && file_exists($file_path)) {
  89. $content = $this->readfile($file_path);
  90. $old = $this->decode($content);
  91. $toWrite = false;
  92. if ($this->isExpired($old)) {
  93. $toWrite = true;
  94. }
  95. }
  96. // Force write
  97. try {
  98. if ($toWrite == true) {
  99. $f = fopen($file_path, 'w+');
  100. fwrite($f, $data);
  101. fclose($f);
  102. return true;
  103. }
  104. } catch (\Exception $e) {
  105. return false;
  106. }
  107. return false;
  108. }
  109. /**
  110. * @param $keyword
  111. * @param array $option
  112. * @return mixed|null
  113. * @throws \Exception
  114. */
  115. public function driver_get($keyword, $option = array())
  116. {
  117. $file_path = $this->getFilePath($keyword);
  118. if (!file_exists($file_path)) {
  119. return null;
  120. }
  121. $content = $this->readfile($file_path);
  122. $object = $this->decode($content);
  123. if ($this->isExpired($object)) {
  124. @unlink($file_path);
  125. $this->autoCleanExpired();
  126. return null;
  127. }
  128. return $object;
  129. }
  130. /**
  131. * @param $keyword
  132. * @param array $option
  133. * @return bool
  134. * @throws \Exception
  135. */
  136. public function driver_delete($keyword, $option = array())
  137. {
  138. $file_path = $this->getFilePath($keyword, true);
  139. if (file_exists($file_path) && @unlink($file_path)) {
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. }
  145. /**
  146. * Return total cache size + auto removed expired files
  147. * @param array $option
  148. * @return array
  149. * @throws \Exception
  150. */
  151. public function driver_stats($option = array())
  152. {
  153. $res = array(
  154. 'info' => '',
  155. 'size' => '',
  156. 'data' => '',
  157. );
  158. $path = $this->getPath();
  159. $dir = @opendir($path);
  160. if (!$dir) {
  161. throw new phpFastCacheDriverException("Can't read PATH:" . $path, 94);
  162. }
  163. $total = 0;
  164. $removed = 0;
  165. while ($file = readdir($dir)) {
  166. if ($file != '.' && $file != '..' && is_dir($path . '/' . $file)) {
  167. // read sub dir
  168. $subdir = opendir($path . "/" . $file);
  169. if (!$subdir) {
  170. throw new phpFastCacheDriverException("Can't read path:" . $path . '/' . $file, 93);
  171. }
  172. while ($f = readdir($subdir)) {
  173. if ($f != '.' && $f != '..') {
  174. $file_path = $path . '/' . $file . '/' . $f;
  175. $size = filesize($file_path);
  176. $object = $this->decode($this->readfile($file_path));
  177. if (strpos($f, '.') === false) {
  178. $key = $f;
  179. } else {
  180. //Because PHP 5.3, this cannot be written in single line
  181. $key = explode('.', $f);
  182. $key = $key[ 0 ];
  183. }
  184. $content[ $key ] = array(
  185. 'size' => $size,
  186. 'write_time' => (isset($object[ 'write_time' ]) ? $object[ 'write_time' ] : null),
  187. );
  188. if ($this->isExpired($object)) {
  189. @unlink($file_path);
  190. $removed += $size;
  191. }
  192. $total += $size;
  193. }
  194. }
  195. }
  196. }
  197. $res[ 'size' ] = $total - $removed;
  198. $res[ 'info' ] = array(
  199. 'Total [bytes]' => $total,
  200. 'Expired and removed [bytes]' => $removed,
  201. 'Current [bytes]' => $res[ 'size' ],
  202. );
  203. $res[ "data" ] = $content;
  204. return $res;
  205. }
  206. /**
  207. * @param int $time
  208. */
  209. public function autoCleanExpired($time = 3600)
  210. {
  211. $autoclean = $this->get('keyword_clean_up_driver_files');
  212. if ($autoclean == null) {
  213. $this->set('keyword_clean_up_driver_files', $time);
  214. $res = $this->stats();
  215. }
  216. }
  217. /**
  218. * @param array $option
  219. * @throws \Exception
  220. * @return void
  221. */
  222. public function driver_clean($option = array())
  223. {
  224. $path = $this->getPath();
  225. $dir = @opendir($path);
  226. if (!$dir) {
  227. throw new phpFastCacheDriverException("Can't read PATH:" . $path, 94);
  228. }
  229. while ($file = readdir($dir)) {
  230. if ($file != '.' && $file != '..' && is_dir($path . '/' . $file)) {
  231. // read sub dir
  232. $subdir = @opendir($path . '/' . $file);
  233. if (!$subdir) {
  234. throw new phpFastCacheDriverException("Can't read path:" . $path . '/' . $file, 93);
  235. }
  236. while ($f = readdir($subdir)) {
  237. if ($f != '.' && $f != '..') {
  238. $file_path = $path . '/' . $file . '/' . $f;
  239. @unlink($file_path);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /**
  246. * @param $keyword
  247. * @return bool
  248. * @throws \Exception
  249. */
  250. public function driver_isExisting($keyword)
  251. {
  252. $file_path = $this->getFilePath($keyword, true);
  253. if (!file_exists($file_path)) {
  254. return false;
  255. } else {
  256. // check expired or not
  257. $value = $this->get($keyword);
  258. return !($value == null);
  259. }
  260. }
  261. /**
  262. * @param $object
  263. * @return bool
  264. */
  265. public function isExpired($object)
  266. {
  267. if (isset($object[ 'expired_time' ]) && time() >= $object[ 'expired_time' ]) {
  268. return true;
  269. } else {
  270. return false;
  271. }
  272. }
  273. }