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

/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php

https://gitlab.com/mika_22/EscuelaAnexa
PHP | 252 lines | 105 code | 36 blank | 111 comment | 6 complexity | cb3442226c24cd902d95a3012fb9061c MD5 | raw file
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Exception;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Contracts\Cache\Store;
  7. class FileStore implements Store
  8. {
  9. use RetrievesMultipleKeys;
  10. /**
  11. * The Illuminate Filesystem instance.
  12. *
  13. * @var \Illuminate\Filesystem\Filesystem
  14. */
  15. protected $files;
  16. /**
  17. * The file cache directory.
  18. *
  19. * @var string
  20. */
  21. protected $directory;
  22. /**
  23. * Create a new file cache store instance.
  24. *
  25. * @param \Illuminate\Filesystem\Filesystem $files
  26. * @param string $directory
  27. * @return void
  28. */
  29. public function __construct(Filesystem $files, $directory)
  30. {
  31. $this->files = $files;
  32. $this->directory = $directory;
  33. }
  34. /**
  35. * Retrieve an item from the cache by key.
  36. *
  37. * @param string|array $key
  38. * @return mixed
  39. */
  40. public function get($key)
  41. {
  42. return Arr::get($this->getPayload($key), 'data');
  43. }
  44. /**
  45. * Retrieve an item and expiry time from the cache by key.
  46. *
  47. * @param string $key
  48. * @return array
  49. */
  50. protected function getPayload($key)
  51. {
  52. $path = $this->path($key);
  53. // If the file doesn't exists, we obviously can't return the cache so we will
  54. // just return null. Otherwise, we'll get the contents of the file and get
  55. // the expiration UNIX timestamps from the start of the file's contents.
  56. try {
  57. $expire = substr($contents = $this->files->get($path), 0, 10);
  58. } catch (Exception $e) {
  59. return ['data' => null, 'time' => null];
  60. }
  61. // If the current time is greater than expiration timestamps we will delete
  62. // the file and return null. This helps clean up the old files and keeps
  63. // this directory much cleaner for us as old files aren't hanging out.
  64. if (time() >= $expire) {
  65. $this->forget($key);
  66. return ['data' => null, 'time' => null];
  67. }
  68. $data = unserialize(substr($contents, 10));
  69. // Next, we'll extract the number of minutes that are remaining for a cache
  70. // so that we can properly retain the time for things like the increment
  71. // operation that may be performed on the cache. We'll round this out.
  72. $time = ceil(($expire - time()) / 60);
  73. return compact('data', 'time');
  74. }
  75. /**
  76. * Store an item in the cache for a given number of minutes.
  77. *
  78. * @param string $key
  79. * @param mixed $value
  80. * @param int $minutes
  81. * @return void
  82. */
  83. public function put($key, $value, $minutes)
  84. {
  85. $value = $this->expiration($minutes).serialize($value);
  86. $this->createCacheDirectory($path = $this->path($key));
  87. $this->files->put($path, $value);
  88. }
  89. /**
  90. * Create the file cache directory if necessary.
  91. *
  92. * @param string $path
  93. * @return void
  94. */
  95. protected function createCacheDirectory($path)
  96. {
  97. if (! $this->files->exists(dirname($path))) {
  98. $this->files->makeDirectory(dirname($path), 0777, true, true);
  99. }
  100. }
  101. /**
  102. * Increment the value of an item in the cache.
  103. *
  104. * @param string $key
  105. * @param mixed $value
  106. * @return int
  107. */
  108. public function increment($key, $value = 1)
  109. {
  110. $raw = $this->getPayload($key);
  111. $int = ((int) $raw['data']) + $value;
  112. $this->put($key, $int, (int) $raw['time']);
  113. return $int;
  114. }
  115. /**
  116. * Decrement the value of an item in the cache.
  117. *
  118. * @param string $key
  119. * @param mixed $value
  120. * @return int
  121. */
  122. public function decrement($key, $value = 1)
  123. {
  124. return $this->increment($key, $value * -1);
  125. }
  126. /**
  127. * Store an item in the cache indefinitely.
  128. *
  129. * @param string $key
  130. * @param mixed $value
  131. * @return void
  132. */
  133. public function forever($key, $value)
  134. {
  135. $this->put($key, $value, 0);
  136. }
  137. /**
  138. * Remove an item from the cache.
  139. *
  140. * @param string $key
  141. * @return bool
  142. */
  143. public function forget($key)
  144. {
  145. $file = $this->path($key);
  146. if ($this->files->exists($file)) {
  147. return $this->files->delete($file);
  148. }
  149. return false;
  150. }
  151. /**
  152. * Remove all items from the cache.
  153. *
  154. * @return void
  155. */
  156. public function flush()
  157. {
  158. if ($this->files->isDirectory($this->directory)) {
  159. foreach ($this->files->directories($this->directory) as $directory) {
  160. $this->files->deleteDirectory($directory);
  161. }
  162. }
  163. }
  164. /**
  165. * Get the full path for the given cache key.
  166. *
  167. * @param string $key
  168. * @return string
  169. */
  170. protected function path($key)
  171. {
  172. $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
  173. return $this->directory.'/'.implode('/', $parts).'/'.$hash;
  174. }
  175. /**
  176. * Get the expiration time based on the given minutes.
  177. *
  178. * @param int $minutes
  179. * @return int
  180. */
  181. protected function expiration($minutes)
  182. {
  183. $time = time() + ($minutes * 60);
  184. if ($minutes === 0 || $time > 9999999999) {
  185. return 9999999999;
  186. }
  187. return $time;
  188. }
  189. /**
  190. * Get the Filesystem instance.
  191. *
  192. * @return \Illuminate\Filesystem\Filesystem
  193. */
  194. public function getFilesystem()
  195. {
  196. return $this->files;
  197. }
  198. /**
  199. * Get the working directory of the cache.
  200. *
  201. * @return string
  202. */
  203. public function getDirectory()
  204. {
  205. return $this->directory;
  206. }
  207. /**
  208. * Get the cache key prefix.
  209. *
  210. * @return string
  211. */
  212. public function getPrefix()
  213. {
  214. return '';
  215. }
  216. }